chapter 1 these slides for cse 110 sections are based in part on the textbook-authors’ slides,...

45
Chapter 1 Chapter 1 These slides for CSE 110 Sections are based in part on the textbook-authors’ slides, which are copyright by the authors. The authors state that instructors using their textbook may use and modify these slides for pedagogical purposes.

Post on 18-Dec-2015

225 views

Category:

Documents


3 download

TRANSCRIPT

Chapter 1Chapter 1

These slides for CSE 110 Sections are based in part on the textbook-authors’ slides, which are copyright by the authors. The authors state that instructors using their textbook may use and modify these slides for pedagogical purposes.

Chapter GoalsChapter Goals

To understand the activity of programming To understand the activity of programming To learn about the architecture of computers To learn about the architecture of computers To learn about machine code and high level To learn about machine code and high level

programming languages programming languages To become familiar with your computing To become familiar with your computing

environment and your compiler environment and your compiler To compile and run your first Java program To compile and run your first Java program To recognize syntax and logic errorsTo recognize syntax and logic errors

Things You Should Know & Things You Should Know & PrerequisitesPrerequisites

Accessing your ASU accountAccessing your ASU account file management, text editing file management, text editing Problem solving skills ???Problem solving skills ??? Time management Time management High school mathHigh school math No prior programming background No prior programming background

required required Please ask for help; Attend the weekly Please ask for help; Attend the weekly

Workshops. Great place to meet your Workshops. Great place to meet your classmates classmates

What Is Programming? What Is Programming?

Computers are programmed to Computers are programmed to perform tasks perform tasks

Different tasks = different programs Different tasks = different programs Program Program

– Sequence of basic operations executed Sequence of basic operations executed in successionin succession

– Contains instruction sequences for all Contains instruction sequences for all tasks it can execute tasks it can execute

Self CheckSelf Check

1.1. What is required to play a music CD What is required to play a music CD on a computer? on a computer?

2.2. Why is a CD player less flexible Why is a CD player less flexible than a computer? than a computer?

3.3. Can a computer program develop Can a computer program develop the initiative to execute tasks in a the initiative to execute tasks in a better way than its programmers better way than its programmers envisioned? envisioned?

AnswersAnswers

1.1. A program that reads the data on the A program that reads the data on the CD and sends output to the speakers CD and sends output to the speakers and the screen. and the screen.

2.2. A CD player can do one thing–play A CD player can do one thing–play music CDs. It cannot execute programs. music CDs. It cannot execute programs.

3.3. No–the program simply executes the No–the program simply executes the instruction sequences that the instruction sequences that the programmers have prepared in programmers have prepared in advance. advance.

Anatomy of a ComputerAnatomy of a Computer

Central processing unit Central processing unit – Chip Chip – Transistors Transistors

Storage Storage – Primary storage: Random-access Primary storage: Random-access

memory (RAM) memory (RAM) – Secondary storage: e.g. hard disk Secondary storage: e.g. hard disk – Removable storage devices: e.g.: floppy Removable storage devices: e.g.: floppy

disks, tapes, CDs disks, tapes, CDs

Anatomy of a ComputerAnatomy of a Computer

Peripherals Peripherals Executes very simple instructions Executes very simple instructions Executes instructions very rapidly Executes instructions very rapidly General purpose device General purpose device

Central Processing UnitCentral Processing Unit

Figure 1: Central Processing Unit

A Memory Module with A Memory Module with Memory ChipsMemory Chips

Figure 2: A Memory Module with Memory Chips

A Hard DiskA Hard Disk

Figure 3: A Hard Disk

A MotherboardA Motherboard

Figure 4: A Motherboard

Schematic Diagram of a Schematic Diagram of a ComputerComputer

Figure 5: Schematic Diagram of a Computer

The ENIACThe ENIAC

Figure 6: The ENIAC

Self CheckSelf Check

4.4. Where is a program stored when it Where is a program stored when it is not currently running? is not currently running?

5.5. Which part of the computer carries Which part of the computer carries out arithmetic operations, such as out arithmetic operations, such as addition and multiplication? addition and multiplication?

AnswersAnswers

4.4. In secondary storage, typically a In secondary storage, typically a hard disk. hard disk.

5.5. The central processing unit. The central processing unit.

Machine CodeMachine Code

Java Virtual Machine (JVM) – a typical Java Virtual Machine (JVM) – a typical sequence of machine instructions is:sequence of machine instructions is:1.1. Load the contents of memory location 40. Load the contents of memory location 40.

2.2. Load the value 100. Load the value 100.

3.3. If the first value is greater than the If the first value is greater than the second value, continue with the second value, continue with the instruction that is stored in memory instruction that is stored in memory location 240. location 240.

Machine instructions are encoded as Machine instructions are encoded as numbers: numbers:

21 40 16 100 163 240 Continued…

Machine CodeMachine Code

Compiler translates high-level Compiler translates high-level language to machine code language to machine code

Self CheckSelf Check

6.6. What is the code for the Java virtual What is the code for the Java virtual machine instruction "Load the machine instruction "Load the contents of memory location 100"? contents of memory location 100"?

7.7. Does a person who uses a computer Does a person who uses a computer for office work ever run a compiler? for office work ever run a compiler?

AnswersAnswers

6.6. 21 100 21 100

7.7. No–a compiler is intended for No–a compiler is intended for programmers, programmers, to translate high-level programming to translate high-level programming

instructions into machine code. instructions into machine code.

The Java Programming The Java Programming LanguageLanguage

Simple Simple Safe Safe Platform-independent ("write once, Platform-independent ("write once,

run anywhere") run anywhere") Rich library (packages) Rich library (packages) Designed for the internet Designed for the internet

Applets on a Web PageApplets on a Web Page

Figure 7: Applets on a Web Page

Self CheckSelf Check

8.8. What are the two most important What are the two most important benefits of the Java language? benefits of the Java language?

9.9. How long does it take to learn the How long does it take to learn the entire Java library? entire Java library?

AnswersAnswers

8.8. Safety and portability. Safety and portability.

9.9. No one person can learn the entire No one person can learn the entire library–it is too large. library–it is too large.

Becoming Familiar with your Becoming Familiar with your ComputerComputer

Log in Log in Locate the Java compiler Locate the Java compiler Understand files and folders Understand files and folders

– Programs are kept in files Programs are kept in files – File: a collection of items of information that File: a collection of items of information that

are kept together are kept together – Files have names, and the rules for legal Files have names, and the rules for legal

names differ from one system to another names differ from one system to another – Files are stored in folders or directories; these Files are stored in folders or directories; these

file containers can be nestedfile containers can be nested

Continued…

Becoming Familiar with your Becoming Familiar with your ComputerComputer

Write a simple program (later) Write a simple program (later) Save your work Save your work

– Develop a strategy for keeping backup Develop a strategy for keeping backup copies of your work copies of your work

A Shell WindowA Shell Window

Figure 8:A Shell Window

An Integrated Development An Integrated Development EnvironmentEnvironment

Figure 9:An Integrated Development Environment

Nested Folders Nested Folders

Figure 10:Nested Folders

Self CheckSelf Check

10.10. How are programming projects How are programming projects stored on a computer? stored on a computer?

11.11. What do you do to protect yourself What do you do to protect yourself from data loss when you work on from data loss when you work on programming projects? programming projects?

AnswersAnswers

10.10. Programs are stored in files, and Programs are stored in files, and files are stored in folders or files are stored in folders or directories. directories.

11.11. You back up your files and folders. You back up your files and folders.

File File HelloTester.javaHelloTester.java

1: public class HelloTester 2: { 3: public static void main(String[] args) 4: { 5: // Display a greeting in the console window 6: 7: System.out.println("Hello, World!"); 8: } 9: }

OutputHello, World!

HelloTesterHelloTester in a Console in a Console WindowWindow

Figure 11:Running the HelloTester Program in a Console Window

HelloTesterHelloTester in an IDE in an IDE

Figure 12:Running the HelloTester Program in an Integrated Development Environment

A Simple ProgramA Simple Program

Figure 13:Calling a Method

System ClassSystem.out Objectprintln Method

• public class ClassName • public static void main(String[] args) • // comment• Method call

Syntax 1.1: Method Call Syntax 1.1: Method Call

 object.methodName(parameters)

Example: System.out.println("Hello, Dave!");

Purpose:To invoke a method of an object and supply any additional parameters

Self Check Self Check

12.12. How would you modify the HelloTester How would you modify the HelloTester program to print the words program to print the words "Hello,""Hello," and and "World!""World!" on two lines? on two lines?

13.13. Would the program continue to work if Would the program continue to work if you omitted the line starting with you omitted the line starting with ////? ?

14.14. What does the following set of What does the following set of statements print? statements print?

System.out.print("My lucky number is");System.out.println(3 + 4 + 5);

AnswersAnswers

12.12.

13.13. Yes–the line starting with Yes–the line starting with //// is a is a comment, intended for human readers. comment, intended for human readers. The compiler ignores comments. The compiler ignores comments.

14.14. The printout is The printout is My lucky number is12My lucky number is12. .

It would be a good idea to add a space It would be a good idea to add a space after the is. after the is.

System.out.println("Hello,");System.out.println("World");

ErrorsErrors

Syntax errorsSyntax errors

Detected by the compiler Detected by the compiler Logic errorsLogic errors

Detected (hopefully) through testing Detected (hopefully) through testing

System.ouch.print(". . .");System.out.print("Hello);

System.out.print("Hell");

Self CheckSelf Check

15.15. Suppose you omit the Suppose you omit the //// characters from the characters from the HelloTester.java program but not HelloTester.java program but not the remainder of the comment. Will the remainder of the comment. Will you get a compile-time error or a you get a compile-time error or a run-time error? run-time error?

16.16. How can you find logic errors in a How can you find logic errors in a program? program?

AnswersAnswers

15.15. A compile-time error. The compiler A compile-time error. The compiler will not know what to do with the will not know what to do with the word display. word display.

16.16. You need to run the program and You need to run the program and observe its behavior. observe its behavior.

The Compilation ProcessThe Compilation Process

Figure 14:From Source Code to Running Program

The Edit—Compile—Loop The Edit—Compile—Loop TestTest

Figure 15:The Edit—Compile—Loop Test

Self CheckSelf Check

17.17. What do you expect to see when What do you expect to see when you load a class file into your text you load a class file into your text editor? editor?

18.18. Why can't you test a program for Why can't you test a program for run-time errors when it has run-time errors when it has compiler errors? compiler errors?

AnswersAnswers

17.17. A sequence of random characters, A sequence of random characters, some funny-looking. Class files some funny-looking. Class files contain virtual machine instructions contain virtual machine instructions that are encoded as binary that are encoded as binary numbers. numbers.

18.18. When a program has compiler When a program has compiler errors, no class file is produced, errors, no class file is produced, and there is nothing to run. and there is nothing to run.