1 / 72

Invitation to Computer Science 6th Edition

Invitation to Computer Science 6th Edition. 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. Objectives (continued). Managing complexity

jamesdavis
Download Presentation

Invitation to Computer Science 6th Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Invitation to Computer Science 6thEdition Chapter Java Programming in Java

  2. 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

  3. Objectives (continued) • Managing complexity • Object-oriented programming • Graphical programming Invitation to Computer Science, 6thEdition

  4. 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

  5. Figure 1 A Simple Java Program Invitation to Computer Science, 6thEdition

  6. Figure 2 The Program of Figure 1 (line numbers added for reference) Invitation to Computer Science, 6thEdition

  7. 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

  8. 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

  9. 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

  10. 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

  11. Figure 3 Some of the Java Primitive Data Types Invitation to Computer Science, 6thEdition

  12. 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

  13. Figure 4 A 12-Element Array hits Invitation to Computer Science, 6thEdition

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. Figure 5 Sequential Flow of Control Invitation to Computer Science, 6thEdition

  20. Figure 6 Java Comparison Operators Invitation to Computer Science, 6thEdition

  21. Figure 7 Java Boolean Operators Invitation to Computer Science, 6thEdition

  22. Figure 8 Conditional Flow of Control (if-else) Invitation to Computer Science, 6thEdition

  23. 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

  24. Figure 9 If-Else with Empty Else Invitation to Computer Science, 6thEdition

  25. Figure 10 The TravelPlanner Program with a Conditional Statement Invitation to Computer Science, 6thEdition

  26. Figure 10 The TravelPlanner Program with a Conditional Statement (continued) Invitation to Computer Science, 6thEdition

  27. Figure 11 While Loop Invitation to Computer Science, 6thEdition

  28. 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

  29. Figure 12 The TravelPlanner Program with Looping Invitation to Computer Science, 6thEdition

  30. Figure 12 The TravelPlanner Program with Looping (continued) Invitation to Computer Science, 6thEdition

  31. 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

  32. Figure 13 A Pseudocode Version of the SportsWorld Program Invitation to Computer Science, 6thEdition

  33. Figure 14 The SportsWorld Program Invitation to Computer Science, 6thEdition

  34. Figure 14 The SportsWorld Program (continued) Invitation to Computer Science, 6thEdition

  35. Figure 15 A Sample Session Using the Program of Figure 14 Invitation to Computer Science, 6thEdition

  36. Figure 15 A Sample Session Using the Program of Figure 14 (continued) Invitation to Computer Science, 6thEdition

  37. 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

  38. Figure 16 Structure Charts Invitation to Computer Science, 6thEdition

  39. 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

  40. Figure 17 Methods in the Circle Class Invitation to Computer Science, 6thEdition

  41. Figure 18 Pseudocode for the SportsWorld Main Method Using the Circle Class Invitation to Computer Science, 6thEdition

  42. Figure 19 A Modularized Version of the SportsWorld Program Invitation to Computer Science, 6thEdition

  43. Figure 19 A Modularized Version of the SportsWorld Program (continued) Invitation to Computer Science, 6thEdition

  44. 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

  45. Figure 20 The Outline for a Java Method Invitation to Computer Science, 6thEdition

  46. Figure 21 The doCircumference Method Invitation to Computer Science, 6thEdition

  47. Figure 22 The Circle Class in a Modularized Version of the SportsWorld Program Invitation to Computer Science, 6thEdition

  48. 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

  49. Figure 23 Some Java Terminology Invitation to Computer Science, 6thEdition

  50. 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

More Related