1.05k likes | 1.11k Views
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).
E N D
Invitation to Computer Science 6thEdition 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 Invitation to Computer Science, 6th Edition
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
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
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
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
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
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
Figure 9.1 Transitions of a High-Level Language Program Invitation to Computer Science, 6th Edition
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
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
Figure 9.2 Pseudocode Algorithm for Favorite Number Invitation to Computer Science, 6th Edition
Figure 9.3 Ada Program for Favorite Number Invitation to Computer Science, 6th Edition
Figure 9.4 C++ Program for Favorite Number Invitation to Computer Science, 6th Edition
Figure 9.5 C# Program for Favorite Number Invitation to Computer Science, 6th Edition
Figure 9.6 Java Program for Favorite Number Invitation to Computer Science, 6th Edition
Figure 9.6 Java Program for Favorite Number (continued) Invitation to Computer Science, 6th Edition
Figure 9.7 Python Program for Favorite Number Invitation to Computer Science, 6th Edition
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
A Simple Java Program Invitation to Computer Science, 6th Edition
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
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
Some of the Java Primitive Data Types Invitation to Computer Science, 6th Edition
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
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
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
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
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
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
Figure 8.20 Structure Charts Invitation to Computer Science, 6th Edition
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
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
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
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
Figure 9.27 Some Java Terminology Invitation to Computer Science, 6th Edition
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
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
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
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
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
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
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
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
Conditional Flow of Control (If-Else) Invitation to Computer Science, 6th Edition
If-Else with Empty Else Invitation to Computer Science, 6th Edition
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
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
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
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
While Loop Invitation to Computer Science, 6th Edition