Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

24
Elements of a Java Program Bina Ramamurthy SUNY at Buffalo

Transcript of Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

Page 1: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

Elements of a Java Program

Bina Ramamurthy

SUNY at Buffalo

Page 2: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 2

Introduction

A programming language is used to translate an algorithmic solution into a computer program.

In this discussion we will study words, symbols, simple statements, and rules for constructing a simple Java program.

Page 3: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 3

Topics of Discussion Problem Analysis class Program structure method, main method Comments Identifier, reserved words, and literal Standard output Compilers and interpreters Errors Summary

Page 4: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 4

• “Out of Their Minds”

“ In general whatever you’re trying to learn, if you can imagine trying to explain it to a computer, then you learn what you don’t know about the subject. It helps you ask the right questions. It is the ultimate test of what you know.”

---Donald E. Knuth

Page 5: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 5

Problem Analysis - Case 1

Problem: Design and build a car.

Solution: Lets follow D.E.Knuth and describe a car to a computer.

Step 1: List the parts / attributes / properties. Ex: Number of doors, Color...

Step 2: List the functionality. Ex: Drive, honk, brake…..

Step 3: Design a blue-print to put the parts+functions together.

Step 4: Build the car and test it.

Page 6: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 6

Problem Analysis - Case 2

Problem: Design a counter to keep track of scores in any field games such as football, hockey, basketball.

Step 1: List parts / properties of Counter: data container to hold score.

Step 2: Functionality : initialize, increment, decrement, display.

Step 3: Build and use.

Page 7: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 7

Java Class

Properties and functions together can be used to specify a class of objects: class of cars, class of counters.

In Java, classes are means for describing the properties and capabilities of objects in real life that a problem has to deal with.

Properties are referred to as “data declarations” or simply declarations.

Capabilities are referred to as “methods”

Page 8: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 8

Class examples

Example: class of cars, class of trees Class car:

– Properties / attributes/parts of a car: color, number of cylinders, make

– Capabilities/ methods/member function : acceleration, anti-lock brake

Page 9: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 9

Elements, Syntax, Semantics Elements : The words, symbols, basic structures

that form the vocabulary of a programming language. Ex: verb, noun

Syntax : Rules of a language that specify how the elements of a language can be put together to form a statement in the language. Ex: English grammar

Semantics: It defines what will happen if a statement is executed. It defines the meaning.

Page 10: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 10

Program Structure A simple Java program is a class with at least one

method called “main” main method must always be defined using the

words public, static, and void. main method is where the processing begins in a

Java program. main contains statements to be executed. File name a program class name should be same.

Page 11: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 11

Example (on the overhead)

class CountDown {

public static void main (String[] args) {

System.out.print (“Three…”);

System.out.print (“Two…”);

System.out.print (“One…”);

System.out.println (“LiftOff …”);

}

}

Page 12: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 12

Comments

Comments are used for internal documentation of your program.

Single line comments start with // Multiple line comments: start with /* and end

with */ Use comments efficiently: Express clearly what

you to say in minimum number of words. Do not crowd the program with too many

comments: Ex: one comment/line

Page 13: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 13

Java Syntax Page. 651, Appendix N (quite complex for a beginner) Here is an informal syntax for a simple program

structure

class ClassName {

public static void main (String[] args) {

// main method … add statements here

}

} We will add to this definition as we learn more.

Page 14: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 14

More Details About Class

Java is case-sensitive. Ex: Rings and rings two distinct names.

Semi-colon is a terminator of a statement. A class represents a class of objects. A class contains the data declarations (“parts”) and

methods (“behaviors” or “capabilities” ). Declarations are answers to “What is it made of?” (It

has a ____, ____, etc.) Methods are answers to “What can it do?”

Page 15: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 15

Identifiers

All the entities used in a program construction should have a name.

These names known as identifiers. An identifier can be composed of letters, underscore

( _ ), digits and dollar symbol ($), but it cannot begin with a digit.

Example (correct) : label7, next_stock, $sys, Ex_9 Example (incorrect) : 3rd_rock, coin#value Style: ThirdRock, NextStock, CoinValue

Page 16: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 16

Identifiers (contd.) Identifiers can be of any length. Reserved words have special meaning in a

programming language and cannot be used as identifiers.

This is similar to English language reserving “what”, “when”, “who” , to mean a question or interrogation. You don’t use these for names of objects or people?!

See the list of reserved word on page.41 of blue book.

Page 17: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 17

Primitive Data Types Every data is used in a program should have

Name (identifier) and Type. Basic data types supported by Java are:

byte, short, int and long to represent whole numbers.

float and double to represent real numbers (numbers with fractional components).

char to represent single character data.

boolean to represent conditions.

void - no type

Page 18: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 18

Variables

A variable is an identifier (name) of data whose value may change during the execution of a program.

A variable refers to the memory location where the data is stored.

Syntax for a variable declaration:

DataType VariableName;

DataType VariableName = InitialValue;

DataType VName1, VName2, VName3; /* multiple variables of the same type*/

Page 19: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 19

Variables : Example Data: Number of cars in the parking lot.

int NumCars; Data : Salary of an employee.

float Salary = 25000.00; //signing bonus First and last initials of a person.

char FirstInitial, LastInitial; Raining?

boolean raining; How about distance between stars? How about distance between

two atoms in a crystal?

Page 20: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 20

Constants

Constants are identifiers that hold a particular value for the duration of their existence.

Syntax:

final DataType ConstantName = ConstantValue; Example:

final double Pi = 3.14159; “final” is a reserved word for indicating that the

value is final or constant.

Page 21: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 21

Assignment Statement Syntax:

Variable = expression; Semantics:

Evaluate the expression on the right hand side (RHS) of the = and assign the result to the variable on the left hand side (LHS).

The expression on the RHS should be consistent with the type of the variable on the LHS.

Expression represents a formula. It could be a constant, variable or combination of variables, constants and operators.

Page 22: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 22

Assignment Statement : Example

int Score1, Score2, Score3;

int Total;

……

Total = Score1;

Total = Total + Score2;

Total = Total + Score3;

Page 23: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 23

A Complete (but still simple) Program

Class Circle {

static final double PI = 3.14;

public static void main (String[] args) {

double Radius = 4.6;

double Area, Circumference;

Area = PI * Radius *Radius;

Circumference = 2.0 * PI * Radius;

System.out.println(“Area =“ + Area);

System.out.println(“Circumference = “ + Circumference);

}}

Page 24: Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

B.Ramamurthy 01/26/98 24

Summary

We learnt about class, method and declarations main method Program structure Identifiers, variable and constants Assignment statement