Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.

Similar presentations


Presentation on theme: "Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR."— Presentation transcript:

1 Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR

2 Decision Statements (General)
How to compare data values? Relational operators How to alter the sequence of program execution based on the result? If.. Else statements in non-event-driven programming Logical operators (&&, || and ! ) and their use. How to deal with multiple choices? Switch statement 4/23/2019 BR

3 Boolean expressions Any expression (variable) that evaluates to TRUE or FALSE is a Boolean expression (variable). example: boolean Raining; Boolean expression can be formed using logic operators (AND, OR , NOT) and/or relational operators used for comparison. 4/23/2019 BR

4 Logical expressions boolean OnProbation, OutOfSchool, BackInSchool;
float GPA; /* if OnProbation and GPA < 2.0 then OutOf School is TRUE else OutOfSchool is FALSE*/ /* if OnProbation and GPA >= 2.0 then BackInSchool is TRUE else BackInSchool is FALSE*/ OutOfSchool = OnProbation && (GPA < 2.0); BackInSchool = OnProbation && ( GPA >= 2.0); /*Actually OutOfSchool is the negation of BackInSchool */ 4/23/2019 BR

5 if Statement An if statement allows a program to choose whether or not to execute a following statement. Syntax: if (condition) statement; Semantics: condition is a Boolean expression: Something that evaluates to True or False. If condition is true then execute the statement is executed. 4/23/2019 BR

6 If -else Statement An if-else statement allows a program to do one thing if a condition is true and a different thing if the condition is false. Syntax: if ( condition ) statement1 else statement2 Statements to be executed for if and else can be a single statement or multiple statements enclosed in { }. 4/23/2019 BR

7 Nested if Statement A body of an if statement could be another if statement. This situation is called nested-if. In this case, an else is matched with the most recent unmatched if. A nested-if allows a program to make decisions based on the results of the previous decision. 4/23/2019 BR

8 Nested if Let the integers be N1, N2 and N3. if ( N1 > N2)
Largest = N1; else /* ASSERT : N3 > N1 and N1 > N2 */ Largest = N3; else /* ASSERT : N2 > N1 */ if (N2 > N3) Largest = N2; else /* ASSERT : N3 > N2 and N2 > N1 */ 4/23/2019 BR

9 Switch ... syntax switch (selector) { case label1: statements1; break;
..... case labeln : statementsn; default : statementsd; } 4/23/2019 BR

10 switch ... semantics Switch statement is used when there are more two alternatives to select from.. selector is an ordinal expression: That means that the values it can take should be finite, countable: integer, char, bool types are acceptable but not float. The selector type and case label type should be same. Each case label must be distinct. The selector expression is evaluated and compared to each of the case labels. If the value of the selector is one of the case labels, say label x, then the statementsx is executed. 4/23/2019 BR

11 switch ... semantics Execution continues on until a break statement is encountered. At this point, control is transferred to the next statement after the switch. If the selector value matches none of the labels, the default statements are executed, if one is provided. Though default provision is optional, it is a good programming practice to always have a default clause. 4/23/2019 BR

12 switch ... Example Problem: Write a switch statement to print out the grade points corresponding to the letter grades “A”, “B” , “C”, “D”, and “F”. switch (letter) { case ‘A’ : points = 4.0; break; case ‘B’ : points = 3.0; case ‘C’ : points = 2.0; break; case ‘D’ : points = 1.0; break; case ‘F’ : points = 0.0; break; default : cout << “error in grade” << endl; } cout << points << endl; 4/23/2019 BR

13 Control Structures Selection: if, if..else, switch
Selection is used to implement “mutual exclusion”; when selective execution of a piece of code is required. Iteration: while, do..while, for Iteration is used when repeated execution of a piece of code is needed. 4/23/2019 BR

14 Reference Types (arrays) A.2.2
Examples of array usage: int primes1 [] = {2,3,5,7,11}; int primes2[ ] = new int[5]; int primes3[ ] = primes2; int primes3[4] = 11; double dip [] ; dip = new int [4][5]; 4/23/2019 BR

15 Strings A.3.3 Basic Strings are non-mutable objects. String s = “”;
int i; for (I=1; I <= 10; I++) { s = s + “ “ + i; } System.out.println(s); 4/23/2019 BR

16 What is an Object? Everything around you is an object.
How would you describe an object? Using its characteristics (has a ----?) and its behaviors (can do ----?) Object must have unique identity (name) : Basketball, Blue ball Consider a ball: Color and diameter are characteristics (Data Declarations) throw, bounce, roll are behaviors (Methods) 4/23/2019 BR

17 Classes are Blueprints
A class defines the types of data associated with an object and the methods allowed on the data. The process creating an object from a class is called instantiation. Every object is an instance of a particular class. There can be many instances of objects from the same class possible with different values for data. 4/23/2019 BR

18 Instantiation : Examples
class FordCar defines a class name FordCar FordCar Windstar; ---- defines a Object reference WindStar Windstar = new FordCar(); instantiates a Windstar Object class HousePlan1(color) HousePlan1 BlueHouse, GreenHouse; BlueHouse = new HousePlan1(Blue); GreenHouse = new housePlan1(Green); 4/23/2019 BR

19 Operator new and “dot” new operator creates a object and returns a reference to that object. After an object has been instantiated, you can use dot operator to access its methods and data declarations (if you access permissions). 4/23/2019 BR

20 Java API and class libraries
JAVA Application Programming Interface (JAVA API) Package of related classes : java.awt Random java.util Date Dictionary Java.io, java.beans,.. Etc.. package class 4/23/2019 BR

21 Using Predefined Classes
Java API contains a large number of predefined classes for use in your program (application). String class in one of them. It is defined in java.lang package. Instantiate an object MyName and initialize it to your name: 4/23/2019 BR

22 Problem Solving Using Classes
From the problem statement decide the classes you need. Many of the classes you need may be predefined in Java API. Design the other classes by specifying the data and methods. Use the classes by instantiating objects from the classes invoking the methods 4/23/2019 BR

23 A complete example Problem Statement: You have been hired to assist in an secret encryption project. In this project each message (string) sent out is attached to a randomly generated secret code (integer) between 1 and 999. Design and develop an application program in Java to carry out this project. 4/23/2019 BR

24 Identify Objects There are two central objects:
Message Secret code Is there any class predefined in JAVA API that can be associated with these objects? Yes , “string” of java.lang and “Random” of java.util 4/23/2019 BR

25 Design Class Random Class String An instance of Random number
An instance of string Class Random An instance of Random number Input and fill up message. Generate Random integer Attach (concatenate) Output combined message. 4/23/2019 BR

26 Implementation On the overhead projector 4/23/2019 BR

27 Debugging and Testing Compile-time Errors : Usually typos or syntax errors (Solution: debugging) Run-time Errors : Occurs during execution. Example: divide by zero . These are known as exceptions. C++ and Java provide special exception handling mechanism to take care of these errors. (Solution: debugging) Logic Errors: Software will compile and execute with no problem, but will not produce expected results. (Solution: testing and correcting) 4/23/2019 BR

28 Summary Basic statements of Java Control Structures Reference types
Using classes available in the Java API “structures” package available with the text provides an easier way to do IO. Next class we will start design of classes and methods. 4/23/2019 BR


Download ppt "Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR."

Similar presentations


Ads by Google