1 / 103

Invitation to Computer Science 6 th Edition

Invitation to Computer Science 6 th Edition. Chapter 9 Introduction to High-Level Language Programming. Objectives. In this chapter, you will learn about: The language progression A family of languages Introduction to Java Virtual data storage Statement types. Objectives (continue).

rosannal
Download Presentation

Invitation to Computer Science 6 th 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 9 Introduction to High-Level Language Programming

  2. Objectives In this chapter, you will learn about: • The language progression • A family of languages • Introduction to Java • Virtual data storage • Statement types Invitation to Computer Science, 6th Edition

  3. Objectives (continue) In this chapter, you will learn about: • Two examples in five-part harmony • Feature analysis • Meeting expectations • Managing complexity • Object-oriented programming • Graphical programming • Software engineering Invitation to Computer Science, 6th Edition

  4. The Language Progression • Using computers to solve problems • Often involves writing programs in a high-level programming language Invitation to Computer Science, 6th Edition 4

  5. Where Do We Stand? • Early days of computing • Programmers were satisfied with assembly language • Programs mostly written by very technically oriented people • Later decades • Programmers demanded a more comfortable programming environment • Programs were now also written by “nontechie” people Invitation to Computer Science, 6th Edition

  6. Where Do We Stand and What Do We Want? • Disadvantages of assembly language • Programmer must “manually” manage the movement of data items • Programmer must take a microscopic view of a task, breaking it down into tiny subtasks at the level of what is going on in individual memory locations • Assembly language program is machine specific • Statements are not natural-language-like Invitation to Computer Science, 6th Edition 6

  7. Where Do We Stand and What Do We Want? (continued) • Expectations of a program written in a high-level language • Programmer need not manage the details of the movement of data items within memory • The programmer can take a macroscopic view of tasks, thinking at a higher level of problem solving Invitation to Computer Science, 6th Edition

  8. Getting Back to Binary • Compiler • Converts high-level language instructions into machine language instructions • Linker • Inserts requested object code from code libraries into the object code for the requesting program Invitation to Computer Science, 6th Edition

  9. Figure 9.1 Transitions of a High-Level Language Program Invitation to Computer Science, 6th Edition

  10. A Family of Languages • Procedural languages • Program consists of sequences of statements that manipulate data items • Follow directly from the Von Neumann computer architecture • Random access memory stores and fetches values to and from memory cells Invitation to Computer Science, 6th Edition

  11. Two Examples in Five-Part Harmony • Favorite number • Figure 9.2 shows pseudocode algorithm • Algorithm implemented in Ada (Figure 9.3), C++ (Figure 9.4), C# (Figure 9.5), Java (Figure 9.6), and Python (Figure 9.7) Invitation to Computer Science, 6th Edition

  12. Figure 9.2 Pseudocode Algorithm for Favorite Number Invitation to Computer Science, 6th Edition

  13. Figure 9.3 Ada Program for Favorite Number Invitation to Computer Science, 6th Edition

  14. Figure 9.4 C++ Program for Favorite Number Invitation to Computer Science, 6th Edition

  15. Figure 9.5 C# Program for Favorite Number Invitation to Computer Science, 6th Edition

  16. Figure 9.6 Java Program for Favorite Number Invitation to Computer Science, 6th Edition

  17. Figure 9.6 Java Program for Favorite Number (continued) Invitation to Computer Science, 6th Edition

  18. Figure 9.7 Python Program for Favorite Number Invitation to Computer Science, 6th Edition

  19. Introduction to Java: A Simple Java Program • Comments • Give information to human readers of code • Class header • Announces that a class is about to be defined • Class • A collection of methods • Method • A section of code that performs a service Invitation to Computer Science, 6th Edition

  20. A Simple Java Program Invitation to Computer Science, 6th Edition

  21. Running a Java Program • File containing the Java code • Same name as the class • File extension .java • Example: TravelPlanner.java • Running a Java program • Program compiled • Example: File TravelPlanner.class created • Translation to object code completed; program linked, loaded, and executed Invitation to Computer Science, 6th Edition

  22. Virtual Data Storage • Identifiers • Names of variables, methods, classes, packages and interfaces. just ways of referring to them. • Keyword • Has a special meaning in Java • Java is a case-sensitive, free-format language • Variable • A named location in memory • Must be declared before it can be used Invitation to Computer Science, 6th Edition

  23. Some of the Java Primitive Data Types Invitation to Computer Science, 6th Edition

  24. Virtual Data Storage (continued) • An array • Groups together a collection of memory locations, all storing data of the same type Figure 8.5 A 12-Element Array Hits Invitation to Computer Science, 6th Edition

  25. Statement Types • Input/output statements • Input statement • Collects a specific 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 or to a file Invitation to Computer Science, 6th Edition

  26. Statement Types (continued) • Assignment statement • Assigns a value to a program variable • Control statement • Directs the flow of control • Can cause it to deviate from the usual sequential flow Invitation to Computer Science, 6th Edition

  27. Input Statements • A prompt • A message that tells the user what kind of input the program wants • Console class • Not a standard Java class; written for this book • Can be used to handle both the prompt and the retrieval of the value in one statement Invitation to Computer Science, 6th Edition

  28. Input Statements (continued) • Methods • readInt • readDouble • readChar • Syntax variable1 = Console.readInt(prompt); variable2 = Console.readDouble(prompt); variable3 = Console.readChar(prompt); Invitation to Computer Science, 6th Edition

  29. Managing Complexity: Divide and Conquer • Divide and conquer • Divide the problem into small pieces • In a computer program • Divide the code into modules or subprograms, each of which does some part of the overall task • Empower these modules to work together to solve the original problem Invitation to Computer Science, 6th Edition

  30. Figure 8.20 Structure Charts Invitation to Computer Science, 6th Edition

  31. Using Methods • Method • A module of code in Java • Named using ordinary Java identifiers • By custom, name starts with a lowercase letter Invitation to Computer Science, 6th Edition

  32. Using Methods (continued) • Two types of methods • void method • Does not pass any value back to the main method • nonvoid method • Returns a single new value back to the main method • Overall form of a method invocation method-identifier(argument list) (same class) class-identifier.method-identifier(argument list) (different class) Invitation to Computer Science, 6th Edition

  33. Eg • public class TravelPlanner • { • public static void main(String[] args) • { • int speed = 1; • double distance = 0; • double time; • speed = Console.readInt("Enter speed in mph:"); • distance = Console.readDouble("Enter your distance in miles;"); • time = distance/speed; • System.out.print("At " + speed + "mph, it will take " + time + "hours to travel " + distance + " miles"); • } • } Invitation to Computer Science, 6th Edition

  34. Writing Methods • General form of the method header scope-indicator return-indicator identifier(parameter list) • Arguments in Java are passed by value • A variable declared within a method can be used only within that method • Return statement • Syntax return expression; Invitation to Computer Science, 6th Edition

  35. Figure 9.27 Some Java Terminology Invitation to Computer Science, 6th Edition

  36. Eg • public class TravelPlanner • { • public static void main(String[] args) • { • int speed = 1; • double distance = 0; • double time; • speed = Console.readInt("Enter speed in mph:"); • distance = Console.readDouble("Enter your distance in miles;"); • time = distance/speed; • System.out.print("At " + speed + "mph, it will take " + time + "hours to travel " + distance + " miles"); • } • } Invitation to Computer Science, 6th Edition

  37. Output Statements • Output to the screen System.out.println(string); • The string can be • Empty System.out.println(); • Literal string System.out.println("Here's your answer." ); • Composed of two or more items System.out.println("Give me " + 5); Invitation to Computer Science, 6th Edition

  38. The Assignment Statement • General form variable = expression; • Expression is evaluated first; the result is written into the memory location specified on the left Invitation to Computer Science, 6th Edition

  39. The Assignment Statement (continued) • Examples B = 2; • Suppose that B is an integer variable A = B + C; • Suppose that A, B, and C are integer variables Letter = 'm'; • Suppose that Letter is a variable of type char Invitation to Computer Science, 6th Edition

  40. Type casting: happen when an integer value is assigned to a double value. • Initialize variable as soon as they they declared, like: • int count = 0; Invitation to Computer Science, 6th Edition

  41. Control Statements • Types of control mechanisms • Sequential • Instructions are executed in order • Conditional • The choice of which instructions to execute next depends on some condition • Looping • A group of instructions may be executed many times Invitation to Computer Science, 6th Edition

  42. Control Statements (continued) • Sequential is default mode of execution • Conditional flow of control • Evaluation of a Boolean condition (also called a Boolean expression) • The programming statement to execute next is based on the value of the Boolean condition (true or false) Invitation to Computer Science, 6th Edition

  43. Control Statements (continued) • Conditional flow of control (continued) • if-else statement if (Boolean condition) S1; else S2; • if variation of the if-else statement if (Boolean condition) S1; S1, S2 may be compound statement (may have serval statements) Invitation to Computer Science, 6th Edition

  44. Conditional Flow of Control (If-Else) Invitation to Computer Science, 6th Edition

  45. If-Else with Empty Else Invitation to Computer Science, 6th Edition

  46. Comparison Operator Symbol Example Example Result == 2==5 F < 2<5 T <= 2<=5 T > 2>5 F >= 2>=5 F != 2!=5 T Invitation to Computer Science, 6th Edition

  47. Boolean Operator Operator Symbol Eg Eg result AND && (2<5)&&(2>7) F OR || (2<5)||(2>7) T NOT ! !(2==5) T Invitation to Computer Science, 6th Edition

  48. public class TravelPlanner { public static void main(String[] args) { int speed = 1; double distance = 0; double time; int hours = 0; int minutes = 0; char choice = 'M'; speed = Console.readInt("Enter speed in mph:"); distance = Console.readDouble("Enter your distance in miles;"); choice = Console.readChar("Decimal hours or hours and mins"); if (choice == 'D') { time = distance/speed; System.out.print("At " + speed + "mph, it will take " + time + "hours to travel " + distance + " miles"); } else { time = distance/speed; hours = (int) time; minutes = (int)((time - hours)*60); System.out.print("At " + speed + "mph, it will take " + hours + " hours " + minutes + " minutes " + distance + " miles"); } } } Invitation to Computer Science, 6th Edition

  49. Control Statements (continued) • Looping (iteration) • The loop body may be executed repeatedly based on the value of the Boolean condition • while statement while (Boolean condition) S1; Invitation to Computer Science, 6th Edition

  50. While Loop Invitation to Computer Science, 6th Edition

More Related