Presentation is loading. Please wait.

Presentation is loading. Please wait.

Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.

Similar presentations


Presentation on theme: "Elements of a Java Program Bina Ramamurthy SUNY at Buffalo."— Presentation transcript:

1 Elements of a Java Program Bina Ramamurthy SUNY at Buffalo

2 B.Ramamurthy01/26/982 Introduction u A programming language is used to translate an algorithmic solution into a computer program. u In this discussion we will study words, symbols, simple statements, and rules for constructing a simple Java program.

3 B.Ramamurthy01/26/983 Topics of Discussion u Problem Analysis u class u Program structure u method, main method u Comments u Identifier, reserved words, and literal u Standard output u Compilers and interpreters u Errors u Summary

4 B.Ramamurthy01/26/984 “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

5 B.Ramamurthy01/26/985 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.

6 B.Ramamurthy01/26/986 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.

7 B.Ramamurthy01/26/987 Java Class u Properties and functions together can be used to specify a class of objects: class of cars, class of counters. u In Java, classes are means for describing the properties and capabilities of objects in real life that a problem has to deal with. u Properties are referred to as “data declarations” or simply declarations. u Capabilities are referred to as “methods”

8 B.Ramamurthy01/26/988 Class examples u Example: class of cars, class of trees u Class car: – Properties / attributes/parts of a car: color, number of cylinders, make – Capabilities/ methods/member function : acceleration, anti-lock brake

9 B.Ramamurthy01/26/989 Elements, Syntax, Semantics u Elements : The words, symbols, basic structures that form the vocabulary of a programming language. Ex: verb, noun u 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 u Semantics: It defines what will happen if a statement is executed. It defines the meaning.

10 B.Ramamurthy01/26/9810 Program Structure u A simple Java program is a class with at least one method called “main” u main method must always be defined using the words public, static, and void. u main method is where the processing begins in a Java program. u main contains statements to be executed. u File name a program class name should be same.

11 B.Ramamurthy01/26/9811 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 …”); }

12 B.Ramamurthy01/26/9812 Comments u Comments are used for internal documentation of your program. u Single line comments start with // u Multiple line comments: start with /* and end with */ u Use comments efficiently: Express clearly what you to say in minimum number of words. u Do not crowd the program with too many comments: Ex: one comment/line

13 B.Ramamurthy01/26/9813 Java Syntax u Page. 651, Appendix N (quite complex for a beginner) u Here is an informal syntax for a simple program structure class ClassName { public static void main (String[] args) { // main method … add statements here } u We will add to this definition as we learn more.

14 B.Ramamurthy01/26/9814 More Details About Class u Java is case-sensitive. Ex: Rings and rings two distinct names. u Semi-colon is a terminator of a statement. u A class represents a class of objects. u A class contains the data declarations (“parts”) and methods (“behaviors” or “capabilities” ). u Declarations are answers to “What is it made of?” (It has a ____, ____, etc.) u Methods are answers to “What can it do?”

15 B.Ramamurthy01/26/9815 Identifiers u All the entities used in a program construction should have a name. u These names known as identifiers. u An identifier can be composed of letters, underscore ( _ ), digits and dollar symbol ($), but it cannot begin with a digit. u Example (correct) : label7, next_stock, $sys, Ex_9 u Example (incorrect) : 3rd_rock, coin#value u Style: ThirdRock, NextStock, CoinValue

16 B.Ramamurthy01/26/9816 Identifiers (contd.) u Identifiers can be of any length. u Reserved words have special meaning in a programming language and cannot be used as identifiers. u 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?! u See the list of reserved word on page.41 of blue book.

17 B.Ramamurthy01/26/9817 Primitive Data Types u Every data is used in a program should have Name (identifier) and Type. u 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

18 B.Ramamurthy01/26/9818 Variables u A variable is an identifier (name) of data whose value may change during the execution of a program. u A variable refers to the memory location where the data is stored. u Syntax for a variable declaration: DataType VariableName; DataType VariableName = InitialValue; DataType VName1, VName2, VName3; /* multiple variables of the same type*/

19 B.Ramamurthy01/26/9819 Variables : Example u Data: Number of cars in the parking lot. int NumCars; u Data : Salary of an employee. float Salary = 25000.00; //signing bonus u First and last initials of a person. char FirstInitial, LastInitial; u Raining? boolean raining; u How about distance between stars? How about distance between two atoms in a crystal?

20 B.Ramamurthy01/26/9820 Constants u Constants are identifiers that hold a particular value for the duration of their existence. u Syntax: final DataType ConstantName = ConstantValue; u Example: final double Pi = 3.14159; u “final” is a reserved word for indicating that the value is final or constant.

21 B.Ramamurthy01/26/9821 Assignment Statement u Syntax: Variable = expression; u 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). u The expression on the RHS should be consistent with the type of the variable on the LHS. u Expression represents a formula. It could be a constant, variable or combination of variables, constants and operators.

22 B.Ramamurthy01/26/9822 Assignment Statement : Example int Score1, Score2, Score3; int Total; …… Total = Score1; Total = Total + Score2; Total = Total + Score3;

23 B.Ramamurthy01/26/9823 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); }}

24 B.Ramamurthy01/26/9824 Summary We learnt about u class, method and declarations u main method u Program structure u Identifiers, variable and constants u Assignment statement


Download ppt "Elements of a Java Program Bina Ramamurthy SUNY at Buffalo."

Similar presentations


Ads by Google