IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Chapter 2 Elementary Programming
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte,
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Introduction to Computers and Programming Lecture 4: Mathematical and Relational Operators.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Introduction to Java Programming, 4E Y. Daniel Liang.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 7 Single-Dimensional Arrays. 2 Arrays Array is a data structure that represents a collection of the same types of data elements. A single-dimensional.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Chapter 2 Elementary Programming. 2 Objectives  To write Java programs to perform simple calculations  To obtain input from the console using the.
Chapter 2: Using Data.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 2 Elementary Programming
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Lab 8. Declaring and Creating Arrays in One Step datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10];
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
Topic 2 Elementary Programming
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Elementary Programming
Chapter 2 Elementary Programming
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Elementary Programming
Chapter 6 Arrays Solution Opening Problem
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
elementary programming
Chapter 6 Arrays.
Single-Dimensional Arrays chapter6
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

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