103
Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Embed Size (px)

Citation preview

Page 1: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science 6th Edition

Chapter 9

Introduction to High-Level Language Programming

Page 2: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 2

Objectives

In this chapter, you will learn about:

• The language progression

• A family of languages

• Introduction to Java

• Virtual data storage

• Statement types

Page 3: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 4: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

The Language Progression

• Using computers to solve problems – Often involves writing programs in a high-level

programming language

Invitation to Computer Science, 6th Edition 44

Page 5: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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 5

Page 6: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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 66

Page 7: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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 7

Page 8: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 9: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 9

Figure 9.1 Transitions of a High-Level Language Program

Page 10: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 11: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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)

Page 12: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 12

Figure 9.2 Pseudocode Algorithm for Favorite Number

Page 13: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 13

Figure 9.3 Ada Program for Favorite Number

Page 14: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 14

Figure 9.4 C++ Program for Favorite Number

Page 15: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 15

Figure 9.5 C# Program for Favorite Number

Page 16: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 16

Figure 9.6 Java Program for Favorite Number

Page 17: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 17

Figure 9.6 Java Program for Favorite Number (continued)

Page 18: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 18

Figure 9.7 Python Program for Favorite Number

Page 19: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 20: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 20

A Simple Java Program

Page 21: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 22: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 23: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 23

Some of the Java Primitive Data Types

Page 24: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 25: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 26: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 27: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 28: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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);

Page 29: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 30: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 30

Figure 8.20 Structure Charts

Page 31: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 32: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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)

Page 33: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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 33

Page 34: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 34

Writing Methods

• General form of the method headerscope-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;

Page 35: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 35

Figure 9.27

Some Java Terminology

Page 36: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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 36

Page 37: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 37

Output Statements

• Output to the screenSystem.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);

Page 38: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 39: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 40: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

• 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 40

Page 41: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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

Page 42: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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)

Page 43: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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)

Page 44: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 44

Conditional Flow of Control

(If-Else)

Page 45: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 45

If-Else with Empty Else

Page 46: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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 46

Page 47: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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 47

Page 48: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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 48

Page 49: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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;

Page 50: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 50

While Loop

Page 51: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

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';

char more = 'Y';

more = Console.readChar("Want to plan a trip? (Y or N):");

while (more == 'Y') {

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 ");

}

}

System.out.println();

more = Console.readChar("Want to plan another trip? (Y or N):");

}

}

Invitation to Computer Science, 6th Edition 51

Page 52: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Another Example

Algorithm (Psudo code)

Get value for user’s choice about continuing

While user wants to continue, do the following steps

Get value for pool radius

Get value for choice of task

If taske choice is circumference

Compute pool circumference

print output

Else (task to aread)

Compute pool area

print output

Get value for user’s choice about continuing

StopInvitation to Computer Science, 6th Edition 52

Page 53: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

public class SportsWorld {

public static void main(String[] args) {

double radius = 0.0;

double circumference = 0.0;

double area = 0.0;

char taskToDo = ' ';

char more = 'Y';

more = Console.readChar("Want to process a pool? (Y or N):");

while (more == 'Y') {

System.out.println();

radius = Console.readDouble("Enter the value of radius:");

System.out.println("Enter your choice of task: ");

taskToDo = Console.readChar("C to circumference; A to compute area ");

if (taskToDo == 'C') {

circumference = 2 * Math.PI * radius;

System.out.print("The circumference for a pool of radius " + radius + " is " + circumference);

} else {

area = Math.PI * radius * radius;

System.out.print("The area for a pool of radius " + radius + " is " + area);

}

System.out.println();

more = Console.readChar("Want to process another trip? (Y or N):");

}

}

}

Invitation to Computer Science, 6th Edition 53

Page 54: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 54

Data Cleanup (Again)

• In this problem:– Input is a set of integer data values that may contain

0s, and 0s are considered to be invalid data– Output is to be a clean data set where the 0s have

been eliminated

Page 55: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 55

Figure 9.8 The Converging-Pointers Algorithm for Data Cleanup

Page 56: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 56Figure 9.9 Ada Converging-Pointers Algorithm

Page 57: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 57Figure 9.9 Ada Converging-Pointers Algorithm

Page 58: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 58

Figure 9.9 Ada Converging-Pointers Algorithm (continued)

Page 59: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 59

Figure 9.9 Ada Converging-Pointers Algorithm (continued)

Page 60: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 60

Figure 9.10 C++ Converging-Pointers Algorithm

Page 61: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 61

Figure 9.10 C++ Converging-Pointers Algorithm

Page 62: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 62

Figure 9.10 C++ Converging-Pointers Algorithm (continued)

Page 63: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 63

Figure 9.11 C# Converging-Pointers Algorithm

Page 64: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 64

Figure 9.11 C# Converging-Pointers Algorithm (continued)

Page 65: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 65

Figure 9.11 C# Converging-Pointers Algorithm (continued)

Page 66: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 66

Feature Analysis

• If you have studied one (or more) of the online chapters for Ada, C++, C#, Java, or Python:– Compare them with the features of the other

languages by scanning Figure 9.15

Page 67: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 67

Figure 9.15 Feature Analysis of Five High-Level Languages

Page 68: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 68

Figure 9.15 Feature Analysis of Five High-Level Languages (continued)

Page 69: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 69

Figure 9.15 Feature Analysis of Five High-Level Languages (continued)

Page 70: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 70

Figure 9.15 Feature Analysis of Five High-Level Languages (continued)

Page 71: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 71

Figure 9.15 Feature Analysis of Five High-Level Languages (continued)

Page 72: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 72

Figure 9.15 Feature Analysis of Five High-Level Languages (continued)

Page 73: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 73

Meeting Expectations

• Expectations for programs written in a high-level programming language– Programmer need not manage details of the

movement of data items within memory or pay any attention to exactly where those items are stored

– Programmer can take a macroscopic view of tasks, thinking at a higher level of problem solving

Page 74: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 74

Meeting Expectations (continued)

• Expectations for programs written in a high-level programming language (continued)– Programs written in a high-level language will be

portable rather than machine specific– Programming statements in a high-level language

will be closer to natural language and will use standard mathematical notation

Page 75: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 75

Object-Oriented Programming: What Is It?

• Object-oriented programming (OOP)– A program is a simulation of some part of the world

that is the domain of interest– Each object is an example drawn from a class of

similar objects

• Key elements of OOP– Encapsulation

• A class consists of its subtask modules and its properties, and both components are “encapsulated” with the class

Page 76: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 76

What Is It? (continued)

• Key elements of OOP (continued)

– Inheritance

• Once a class A of objects is defined, a class B of objects can be defined as a “subclass” of A

– Polymorphism

• One name, the name of the service to be performed, has several meanings, depending on the class of the object providing the service

Page 77: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 77

Java and OOP

• Java is an object-oriented programming language

• Objects: Instances of a class

• Instance variables: Properties

• Instance methods: Services

• Static method

– Can be invoked by giving the class name, a dot, the method name, and a list of arguments. Use class to call static method

Page 78: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 78

Java and OOP (continued)

• Syntax to request an object to invoke a method

object-identifier.method-identifier(argument list)

• Calling object

– The object that invokes a method

Page 79: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

• package myproject;

• public class SportsWorld {

• public static void main(String[] args) {

• double newRadius = 0.0;

• char taskToDo = ' ';

• char more = 'Y';

• Circle SwimmingPool = new Circle();

• more = Console.readChar("Do you want to prcess a new pool? (Y or N): ");

• while (more == 'Y')

• {

• System.out.println();

• newRadius = Console.readDouble("Enter the value to the radius of the pool: ");

• SwimmingPool.setRadius(newRadius);

• System.out.println("Enter your choice of task.");

• taskToDo = Console.readChar("C to compue ciucumference, A to compute area: ");

• System.out.println();

• if (taskToDo == 'C')

• System.out.println("the ciccumference for a pool radius "

• + SwimmingPool.getRadius() + " is " + SwimmingPool.doCircumference());

• else

• System.out.println("the area for a pool radius "

• + SwimmingPool.getRadius() + " is " + SwimmingPool.doAreaY());

• more = Console.readChar("Do you want to prcess a new pool? (Y or N): ");

• }

• }

• }

Invitation to Computer Science, 6th Edition 79

Page 80: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

One more examplepublic class Circle {

public void setRadius(double value)

{

radius =value;

}

public double getRadius()

{

return radius;

}

public double doCircumference()

{

return 2 * Math.PI * radius;

}

public double doArea()

{

return Math.PI * radius * radius;

}

private double radius;

}

Invitation to Computer Science, 6th Edition 80

Page 81: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

public class Rectangle {

public void setWidth(double value)

{

width =value;

}

public void setHeight(double value)

{

height =value;

}

public double getWidth()

{

return width;

}

public double getHeitgh()

{

return height;

}

public double doArea()

{

return width * height;

}

protected double width, height;

}

Invitation to Computer Science, 6th Edition 81

Page 82: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

public class Square {

public void setSide(double value)

{

side = value;

}

public double getSide()

{

return side;

}

public double doArea()

{

return side * side;

}

private double side;

}Invitation to Computer Science, 6th Edition 82

Page 83: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

public class Square2 extends Rectangle{

public void setSide(double value)

{

width = value;

height = value;

}

}

Invitation to Computer Science, 6th Edition 83

Page 84: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

public class Ceometry {

public static void main(String[] args) {

Circle joe = new Circle();

joe.setRadius(23.5);

System.out.println("The area of a circle with radius " + joe.getRadius() + " is " + joe.doArea());

Rectangle luis = new Rectangle();

luis.setWidth(12.4);

luis.setHeight(18.1);

System.out.println("The area of a rectangle with dimensions " + luis.getWidth() + " and " + luis.getHeight() + " is " + luis.doArea());

Square anna = new Square();

anna.setSide(3);

System.out.println("The area of a square with side " + anna.getSide() + " is " + anna.doArea());

Square2 tyler = new Square2();

tyler.setSide(4.2);

System.out.println("The area of a square with side " + tyler.getWidth() + " is " + tyler.doArea());

}

}

Invitation to Computer Science, 6th Edition 84

Page 85: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 85

What Have We Gained?

• Two major advantages of OOP

– Software reuse

– A more natural “world view”

Page 86: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 86

Graphical Programming: Graphics Hardware

• Bitmapped display– The screen is made up of thousands of individual

picture elements, or pixels, laid out in a two-dimensional grid

• Frame buffer– Memory that stores the actual screen image

• Terminal hardware displays the frame buffer value of every individual pixel on the screen

Page 87: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 87

Figure 8.34

Pixel Numbering System in a Bitmapped Display

Page 88: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 88

Graphics Software

• Graphics library

– Contains a collection of software routines that control the setting and clearing of pixels

• Abstract Windowing Toolkit (AWT)

– Contains routines that allow users to create powerful interfaces

• Swing components

– Even more powerful GUI components than AWT

Page 89: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 89

Graphics Software (continued)

• Graphics class

– Contains drawing commands that allow you to

• Draw geometric shapes (lines, rectangles, ovals, polygons, and so on)

• Set, change, and define colors

• Fill in or shade objects

• Create text in a range of fonts and sizes

• Produce graphs and charts

Page 90: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Egimport java.awt.*;

public class Graph {

public static void main(String[] args) {

Frame f = new Frame("Eg 1");

f.setSize(500, 500);

f.setVisible(true);

Graphics g;

g = f.getGraphics();

g.setColor(Color.blue);

while (true)

{

g.drawLine(168, 112, 112, 168);

g.drawOval(100, 100, 80, 80);

g.drawString("No Entry", 112, 145);

}

}

}

Invitation to Computer Science, 6th Edition 90

Page 91: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Other functions

• drawRect(int x, int y, int width, int height);

• drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight);

• fillRect(int x, int y, int width, int height);

• fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight);

• fillOvel(int x, int y, int width, int height);

Invitation to Computer Science, 6th Edition 91

Page 92: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 92

The Big Picture: Software Engineering

• Software development life cycle– Overall sequence of steps needed to complete a

large-scale software project

• Studies have shown that on big projects:– Initial implementation of the program may occupy

only 10–20% of the total time – 25–40% of time is spent on problem specification

and program design– 40–65% is spent on tasks that follow implementation

Page 93: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 93

Figure 9.16 Steps in the Software Development Life Cycle

Page 94: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 94

Scaling Up

• Figure 9.17 categorizes software products in terms of :– Size– Number of programmers needed for development– Duration of the development effort

• Software engineering – Large-scale software development

Page 95: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 95

Figure 9.17 Size Categories of Software Products

Page 96: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 96

The Software Development Life Cycle

• The feasibility study– Evaluation, costs vs benefits

• purchase new system

– Feasibility document

• Problem specification– Clear, concise, unambiguous statement of the

problem– Problem specification document

Page 97: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 97

The Software Development Life Cycle

• Program design– Design solution– Divide-and-conquer– Program design document

• Breaks into subtasks and various classes

• Algorithm selection or development, and analysis– Pseudo code– Analysis of efficiency– rationale

• Coding

Page 98: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 98

The Software Development Life Cycle (continued)

• Debugging– Locating and correcting program errors

• Syntax errors• Runtime errors• Logic errors

– A=A+317

• Testing, verification, and benchmarking– Empirical testing

– Unit testing

– Integration testing

– Regression testing

Page 99: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 99

The Software Development Life Cycle (continued)

• Testing, verification, and benchmarking– Output data satisfies certain conditions– Performance falls within required limits– Correct and efficient program

• Documentation– Internal documentation– External documentation– Technical documentation– User documentation

• Maintenance

Page 100: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 100

The Software Development Life Cycle (continued)

• Maintenance– Adapting– 65%– Involves repetition of previous steps

• Feasibility study

• Implementation

• Testing

• Updated documentation

Page 101: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 101

Modern Environments

• Integrated Development Environment– Lets programmer perform a number of tasks within

the shell of a single application program

• Rapid prototyping – Allows miscommunications between the user and

the programmer to be identified and corrected early in the development process

Page 102: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 102

Summary

• In a high-level language, the programmer:– Need not manage storage – Can think about the problem at a higher level– Can use more powerful program instructions that are

more like natural language– Can write a much more portable program

Page 103: Invitation to Computer Science 6 th Edition Chapter 9 Introduction to High-Level Language Programming

Invitation to Computer Science, 6th Edition 103

Summary (continued)

• Software development life cycle– Overall sequence of steps needed to complete a

large-scale software project