IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1
Content Data Type Control Structure Sequence Statement Selection Statement Looping Statement Array(3 rd week) Interactive Input (3 rd week) Prepared by: Mrs Sivabalan2
Data Type What is variable?? How to declare a variable ?? Name the data type that you are familiar with.. Do u know the difference between declaration and initialization?? Is there any rules for writing variable or data type Prepared by: Mrs Sivabalan3
Data Type-Answer What is variable?? Variable is a identifier that always changed How to declare a variable ?? ; Name the data type that you are familiar with.. Integer, Float, Double and etc.. Prepared by: Mrs Sivabalan4
Data Type-Continue Do u know the difference between declaration and initialization?? Declaration int a; Initialization int a = 10; Prepared by: Mrs Sivabalan5
Data Type-Continue Is there any rules for writing variable or data type An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($). An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. ◦ An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,” for a list of reserved words). An identifier cannot be true, false, or null. An identifier can be of any length. Prepared by: Mrs Sivabalan6
Data Type Prepared by: Mrs Sivabalan7 byte 8 bits short 16 bits int 32 bits long 64 bits float 32 bits double 64 bits
Class Activity Write the declaration for the following: Declare count to be an integer variable int count; Declare interestRate to be a double variable double interestRate; Assign 1 to variable x int x = 1; Assign 1.0 to variable radius double radius = 1.0; Assign the addition of y and 1 to x double x=y + 1; Prepared by: Mrs Sivabalan8
Operator +, -, *, /, and % Examples: 5 / 2 yields an integer / 2 yields a double value % 2 yields 1 (the remainder of the division) Prepared by: Mrs Sivabalan9
Shortcut Assignment Operators OperatorExampleEquivalent +=i+=8i = i+8 -=f-=8.0f = f-8.0 *=i*=8i = i*8 /=i/=8i = i/8 %=i%=8i = i%8 Prepared by: Mrs Sivabalan10
Increment and Decrement Operator Prepared by: Mrs Sivabalan11 OperatorNameDescription ++varpreincrementThe expression (++var) increments var by 1 and evaluates to the new value in var after the increment. var++postincrementThe expression (var++) evaluates to the original value in var and increments var by 1. --varpredecrementThe expression (--var) decrements var by 1 and evaluates to the new value in var after the decrement. var--postdecrement The expression (var--) evaluates to the original value in var and decrements var by 1.
Boolean Data Type Prepared by: Mrs Sivabalan12
Comparison Operator Operator Name < less than <= less than or equal to > greater than >= greater than or equal to == equal to != not equal to Prepared by: Mrs Sivabalan13
Boolean Operator Operator Name ! not && and || or Prepared by: Mrs Sivabalan14
The & and | Operators &&: conditional AND operator &: unconditional AND operator ||: conditional OR operator |: unconditional OR operator exp1 && exp2 (1 < x) && (x < 100) (1 < x) & (x < 100) Prepared by: Mrs Sivabalan15
Class Activity 1. Assuming that x is 1, predict the result of the following BOOLEAN expression (true) && (3 > 4) (x > 0) || (x < 0) 2. Which of the following assignment statement is correct to assign 5 to character c? A. char c = 5; B. char c = "5"; C. char c = '5'; D. char c = "344"; Prepared by: Mrs Sivabalan16
Control Structure Draw standard flowchart for the following: Sequence Statement Selection Statement Looping Statement Prepared by: Mrs Sivabalan17
Sequence Statement Prepared by: Mrs Sivabalan18
Selection Statement Prepared by: Mrs Sivabalan19
Looping Statement Prepared by: Mrs Sivabalan20 For loopingWhile loopingDo….while looping
Next Class-need to know b4 come for class Difference between these three looping.. In terms of Flowchart Coding writing style Prepared by: Mrs Sivabalan21
Content Array Importance of array Declare array reference variable Initialize value in array Length of array program common array operations (displaying arrays, summing all elements, finding min and max elements) Interactive Input Prepared by: Mrs Sivabalan22
Array-Importance of array Array is a data structure that represents a collection of the same types of data. Prepared by: Mrs Sivabalan23
Declare array reference variable datatype[] arrayRefVar; Example: double[] myList; datatype arrayRefVar[]; // This style is allowed, but not preferred Example: double myList[]; Prepared by: Mrs Sivabalan24
Creating array arrayRefVar = new datatype[arraySize]; Example: myList = new double[10]; myList[0] references the first element in the array. myList[9] references the last element in the array. Prepared by: Mrs Sivabalan25
26 Declaring and Creating in One Step datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10]; datatype arrayRefVar[] = new datatype[arraySize]; double myList[] = new double[10];
Initialize value in array Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5; Prepared by: Mrs Sivabalan27
Length of array Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayRefVar.length For example, myList.length returns 10 Prepared by: Mrs Sivabalan28
Indexed Variables The array elements are accessed through the index. The array indices are 0-based, i.e., it starts from 0 to arrayRefVar.length-1. In the example in Figure 6.1, myList holds ten double values and the indices are from 0 to 9. Each element in the array is represented using the following syntax, known as an indexed variable: arrayRefVar[index]; Prepared by: Mrs Sivabalan29
30 Using Indexed Variables After an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in myList[0] and myList[1] to myList[2]. myList[2] = myList[0] + myList[1];
Interactive Input JOptionPane Scanner BufferedReader Prepared by: Mrs Sivabalan31
JOptionPane Here are two useful static methods from javax.swing.JOptionPane that allow you to easily create dialog boxes for input and output. Prepared by: Mrs Sivabalan32 ValueMethod call userInput JOptionPane.showInputDialog(component, text); JOptionPane.showMessageDialog(component, text);
Example coding-JOptionPane Prepared by: Mrs Sivabalan33 import javax.swing.JOptionPane; public class circleDialog { public static void main(String [] args) { double radius=2.0; double area; area=radius*radius*3.14; JOptionPane.showMessageDialog(null,"The radius of circle is: " + radius + "\n The area of circle is: " + area); }
Example coding to receive input- JOptionPane Prepared by: Mrs Sivabalan34 import javax.swing.JOptionPane; public class circleDialogInput { public static void main(String [] args) { double radius; String radiusString; double area; radiusString=JOptionPane.showInputDialog("Please enter radius:"); radius=Double.parseDouble(radiusString); area=radius*radius*3.14; JOptionPane.showMessageDialog(null,"The radius of circle is: " + radius + "\n The area of circle is: " + area); }
Scanner Input – Read from the console A Scanner object can parse user input entered on the console or from a file Scanner in = new Scanner(System.in); int i = in.nextInt(); int j = in.nextInt(); System.out.println(i+j); Prepared by: Mrs Sivabalan35
Example-Scanner Input Prepared by: Mrs Sivabalan36 import java.util.*; public class circleInput { public static void main(String [] args) { double radius; double area; Scanner scanner = new Scanner(System.in); System.out.println("Please enter radius:"); radius=scanner.nextDouble(); area=radius*radius*3.14; System.out.println("The area of circle is: " + area); }
BufferedReader BufferedReader wraps another Reader and improves performance. BufferedReader provides a readLine method to read a line of text BufferedReader bufferedReader = new BufferedReader (i nputStreamReader(System.in)); Prepared by: Mrs Sivabalan37
Prepared by: Mrs Sivabalan38 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class MainClass { public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a line:"); System.out.println(stdin.readLine()); } }
Thanks The brain is a wonderful organ. It starts working the moment you get up in the morning and does not stop until you get into the office. `Albert Einstein ` Prepared by: Mrs Sivabalan39