Presentation is loading. Please wait.

Presentation is loading. Please wait.

Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.

Similar presentations


Presentation on theme: "Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve."— Presentation transcript:

1

2 Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve or -ve integer double 8 bytes +ve or -ve number with optional decimal point char 2 bytesa single character boolean 1 bit true or false

3 You Do Review: Distance - part 1 of 2 on P 79 of your text.

4 Primitive Data Types (cont’d) double is a sometimes called a floating point number Don’t choose a bigger data type than you need (e.g. a double where an int will do). Conserve memory. Choose the appropriate size also signals the type of value which is expected.

5 Abstract Data Types Variables can also be declared using abstract data types. A class is an abstract data type. A variable declared with a class is an object.

6 Abstract Data Types - Instantiation = new ( ); Circle spot = new Circle(4); //spot has radius 4 We have new variable spot with radius of 4. Access members of a class using the object name, then a dot, then the member name. Suppose Circle has at getRadius() method. System.out.println(“Radius of spot: “ + spot.getRadius()); Radius of spot: 4

7 Data from the Outside So far we’ve only used data that comes from inside our program. –i.e. data that the program already knows about when it’s compiled and before it begins to run. Useful programs need to be able to get/read data from outside themselves.

8 Data from the Outside (cont’d) We can get data from the keyboard (typed in by the user). We can also read data from files. –that’s for later. We can also read data from a device or sensor (think robotics or environmental monitoring.)

9 Getting Data from the User Values can be read from an input stream. An input stream is a sequence of characters received from an input device, such as a keyboard. We can use the included Scanner class to read from the input stream in Java.

10 The Scanner Class To use the Scanner class you have to declare a Scanner object in your program. –Q:What do we call it when we declare an object? –A:instantiation.

11 The Scanner Class Instantiating a scanner object: Scanner input = new Scanner(System.in); We’ve just declared a Scanner object and called it input. This won’t work unless we also add an import statement at the top of our program outside the class.

12 The Scanner Class Here’s the import statement: import java.util.Scanner; This tells the compiler that we want to use the Scanner package.

13 The Scanner Class We can use the Scanner object that we just declared and a method called nextInt() to get an integer value from the user: someVariable = input.nextInt(); Assuming someVariable has already been declared then after this line executes someVariable will have whatever integer the user types on the keyboard. What datatype do you think someVariable will have to have?

14 The Scanner Class The Scanner class also has methods that will accept decimal numbers - nextDouble() - and character strings - nextLine()

15 You Do Implement RectangleArea2 as laid out on P82 of your text. What happens if you enter a decimal number instead of an integer when prompted? Now, do Review: Distance - part 2 of 2 on p 82 of your text.

16 UserInput Here are the general steps for getting values from the keyboard: –IMPORT the Scanner class –DECLARE some variables to store stuff –Declare a Scanner object –PROMPT the user to enter the data –use nextInt(), nextDouble(), nextLine() with the Scanner object to get the data and store it in the variable.

17 Numeric Expressions Arithmetic operators: –Addition + –Subtraction - –Multiplication * –Division / –Modulus Division %

18 Integer Division If the values being divided are both integers, “/” returns only the integer portion of the division. E.g. 20/7 ==> 2 (with remainder 6)

19 You Do: Create an application called AvgDistance that prompts the user for 3 distances and displays the average. Users should be able to enter decimal numbers. You will need to use: nextDouble()

20 Modulus Division Modulus division returns the remainder of an integer division E.g. 20 % 7 ==> 6 Integer and modulus division are useful for retrieving individual digits in a number E.g 27 % 10 ==> 7 27 / 10 ==> 2

21 You Do. Create a program called ParseInteger that prompts the user for a 3 digit integer and returns each individual digit. You will need to use modulus Division for this. Brainstorm with a partner/colleague in class. Hint: You will need to use both integer and modulus division to make this work

22 Order of Operations What does 6 + 4 * 2 - 1 evaluate to? It depends on the order in which you do the operations. Java uses the same rules of precedence as mathematics. Multiplication and division precede addition and subtraction. Operators of equal precedence are evaluated from left to right.

23 Order of Operations (cont’d) Change the order of operations by using round parentheses “ ( ) ” (6 + 4) * (2 – 1) evaluates to 10 It is good practice to include parentheses whenever there is the possibility of ambiguity.

24 Type Casting Typecasting is a way of making a variable of one data type act like a variable of another data type for a single operation. Most useful when we want to divide data that we collect as integers without truncating. Remember truncation?

25 Type Casting (cont’d) int litres; int kilometres; int fuelEfficiency; //should be expressed in litres/km Car mazda = new Car(); // assume we have a class called Car litres = mazda.getGasPurchases(); kilometres = mazda.getMileage(); //assume 42 litres and 475 km fuelEfficiency = litres/kilometres; //this was integer div so the result was 0 //instead fuelEfficiency = (double)litres/kilometres;


Download ppt "Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve."

Similar presentations


Ads by Google