720 likes | 798 Views
Learn about creating and running Java programs, virtual data storage, statement types, and managing complexity in computer science. Understand Boolean conditions, control statements, and graphical programming.
E N D
Invitation to Computer Science 6thEdition Chapter Java Programming in Java
Objectives In this chapter, you will learn about: • Creating and running a simple Java program • Virtual data storage • Statement types • Another example Invitation to Computer Science, 6thEdition
Objectives (continued) • Managing complexity • Object-oriented programming • Graphical programming Invitation to Computer Science, 6thEdition
Introduction to Java • Figure 1 shows a simple but complete Java program • Figure 2 shows the same program with a number in front of each line • Comments • Anything appearing on a line after the double slash symbol (//) is ignored by the compiler • Prologue comment • Introductory comment that comes first Invitation to Computer Science, 6thEdition 4
Figure 1 A Simple Java Program Invitation to Computer Science, 6thEdition
Figure 2 The Program of Figure 1 (line numbers added for reference) Invitation to Computer Science, 6thEdition
Introduction to Java (continued) • Class header • Announces that a class is about to be defined • Class • Collection of sections of code called methods • Free-format language • Does not matterwhere things are placed on a line Invitation to Computer Science, 6thEdition
Creating and Running a Java Program • Three-step process • First step: type the program into a text editor • Second step: the program in the .java file must be compiled using a Java compiler for your computer • Third step: operates on the .class file; it finishes the translation to object code and links, loads, and executes your program • Integrated Development Environment • Lets the programmer perform a number of tasks within the shell of a single application program Invitation to Computer Science, 6thEdition
Virtual Data Storage • Assembly language • Does not require us to give the actual memory address of the storage location to be used for each item • Identifiers • Names in a programming language • Cannot be one of the few keywords • Java is a case-sensitive language • Uppercase letters are distinguished from lowercase letters Invitation to Computer Science, 6thEdition
Virtual Data Storage (continued) • Constants • Quantities that are fixed throughout the duration of the program • Values are known ahead of time • Variables • Values that are not known ahead of time but must be obtained from the computer user • Variable declaration • Consists of a data type followed by a list of one or more identifiers of that type Invitation to Computer Science, 6thEdition
Figure 3 Some of the Java Primitive Data Types Invitation to Computer Science, 6thEdition
Virtual Data Storage (continued) • Array • Groups together a collection of memory locations, all storing data of the same type • Allows us to talk about an entire table of values or the individual elements making up that table Invitation to Computer Science, 6thEdition
Figure 4 A 12-Element Array hits Invitation to Computer Science, 6thEdition
Statement Types • Input statement • Collects a value from the user for a variable within the program • Output statement • Writes a message or the value of a program variable to the user’s screen • Assignment statement • Assigns a value to a program variable Invitation to Computer Science, 6thEdition
Statement Types (continued) • Control statements • Affect the order in which instructions are executed • Flow of controlin the program • The path through the program that is traced by following the currently executing statement Invitation to Computer Science, 6thEdition
Input/Output Statements • Scanner class • Provides a convenient way to collect user data entered at the keyboard • Found in the Java “utility” library • Represents a new data type • Literal strings and variables • Can be concatenated together in all sorts of combinations Invitation to Computer Science, 6thEdition
The Assignment Statement • Pseudocode operation Set the value of “variable” to “arithmetic expression” • Java equivalent variable = expression; • Basic arithmetic operations + Addition - Subtraction * Multiplication / Division Invitation to Computer Science, 6thEdition
Control Statements • Control mechanisms • Sequential:instructions are executed in order • Conditional:which instruction executes next depends on some condition • Looping:a group of instructions may be executed many times • Boolean condition • Can be either true or false • Often involves comparing the values of two expressions and determining whether they are equal Invitation to Computer Science, 6thEdition
Figure 5 Sequential Flow of Control Invitation to Computer Science, 6thEdition
Figure 6 Java Comparison Operators Invitation to Computer Science, 6thEdition
Figure 7 Java Boolean Operators Invitation to Computer Science, 6thEdition
Figure 8 Conditional Flow of Control (if-else) Invitation to Computer Science, 6thEdition
Control Statements (continued) • Compound statement • Can be used anywhere a single statement is allowed • Example { System.out.println(“This is the first statement.”); System.out.println(“This is the second statement.”); System.out.println(“This is the third statement.”); } Invitation to Computer Science, 6thEdition
Figure 9 If-Else with Empty Else Invitation to Computer Science, 6thEdition
Figure 10 The TravelPlanner Program with a Conditional Statement Invitation to Computer Science, 6thEdition
Figure 10 The TravelPlanner Program with a Conditional Statement (continued) Invitation to Computer Science, 6thEdition
Figure 11 While Loop Invitation to Computer Science, 6thEdition
Control Statements (continued) • Sentinel value • One extra integer that is not part of the legitimate data but is instead a signal that there are no more data • Infinite loop • The condition, once true, would remain true forever, and the loop body would be endlessly executed Invitation to Computer Science, 6thEdition
Figure 12 The TravelPlanner Program with Looping Invitation to Computer Science, 6thEdition
Figure 12 The TravelPlanner Program with Looping (continued) Invitation to Computer Science, 6thEdition
Another Example • Example • Write a program to assist SportsWorld, a company that installs circular swimming pools • To estimate costs for swimming pool covers or for fencing • SportsWorld needs to know the area or circumference of a pool, given its radius Invitation to Computer Science, 6thEdition
Figure 13 A Pseudocode Version of the SportsWorld Program Invitation to Computer Science, 6thEdition
Figure 14 The SportsWorld Program Invitation to Computer Science, 6thEdition
Figure 14 The SportsWorld Program (continued) Invitation to Computer Science, 6thEdition
Figure 15 A Sample Session Using the Program of Figure 14 Invitation to Computer Science, 6thEdition
Figure 15 A Sample Session Using the Program of Figure 14 (continued) Invitation to Computer Science, 6thEdition
Managing Complexity • Divide and conquer • A problem-solving approach and not just a computer programming technique • Figure 16(a) • An example of a structure chart or structure diagram Invitation to Computer Science, 6thEdition
Figure 16 Structure Charts Invitation to Computer Science, 6thEdition
Using Methods • Methods • Modules of code • Named using ordinary Java identifiers, customarily starting with a lowercase letter • Void method • Carries out some task but does not pass any new values back to the main method • Nonvoid method • Returns a single new value back to the main method Invitation to Computer Science, 6thEdition
Figure 17 Methods in the Circle Class Invitation to Computer Science, 6thEdition
Figure 18 Pseudocode for the SportsWorld Main Method Using the Circle Class Invitation to Computer Science, 6thEdition
Figure 19 A Modularized Version of the SportsWorld Program Invitation to Computer Science, 6thEdition
Figure 19 A Modularized Version of the SportsWorld Program (continued) Invitation to Computer Science, 6thEdition
Writing Methods • Method header has the general form scope-indicator return-indicator identifier(parameter list) • Arguments passed by value • Method can use the argument value but cannot permanently change it • Return statement • Returns a single value to the main method • Syntax: return expression; Invitation to Computer Science, 6thEdition
Figure 20 The Outline for a Java Method Invitation to Computer Science, 6thEdition
Figure 21 The doCircumference Method Invitation to Computer Science, 6thEdition
Figure 22 The Circle Class in a Modularized Version of the SportsWorld Program Invitation to Computer Science, 6thEdition
Writing Methods (continued) • Java program • Always begins execution with the main method • Modularizing a program is useful for: • Planning • Coding • Testing • Modifying • Reading Invitation to Computer Science, 6thEdition
Figure 23 Some Java Terminology Invitation to Computer Science, 6thEdition
Object-Oriented Programming • A program is considered a simulation of some part of the world that is the domain of interest • “Objects” populate this domain • When an object-oriented program is executed • The program generates requests for services that go to the various objects • Terms associated with object-oriented programming • Encapsulation • Inheritance • Polymorphism Invitation to Computer Science, 6thEdition