Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Objectives: Learn about the 8 primitive data types Learn how to declare variables Learn about arithmetic operators Learn how to read numbers from the keyboard using the Scanner class. Lab 02-2 Primitive Data Types All information in digital computers is represented by numbers! Lab 02-4 Primitive Data Types All information in digital computers is represented by numbers! There is no such thing as letters of the alphabet. Words, sounds and images are all digitally stored as binary numbers. Lab 02-5 Primitive Data Types Digital computers are capable of storing numbers in two different formats. Lab 02-6 Primitive Data Types Digital computers are capable of storing numbers in two different formats. 1. Integers – (whole numbers) Lab 02-7 Primitive Data Types Digital computers are capable of storing numbers in two different formats. 1. Integers – (whole numbers) 2. Real numbers – numbers that have a fractional part. Lab 02-8 Primitive Data Types • • • • int double char boolean • • • • byte short long float Most frequently used Lab 02-9 Primitive Data Types (whole numbers) • • • • byte short int long (8 bits) (16 bits) (32 bits) (64 bits) It’s all about size. The more bits used by a data type the larger the values it can contain. Lab 02-10 Primitive Data Types (whole numbers) • • • • byte short int long (8 bits) (16 bits) (32 bits) (64 bits) An int uses four bytes to represent a whole number from -231 to 231-1. −2,147,483,648 to 2,147,483,647 Lab 02-11 Primitive Data Types (real numbers) • float (32 bits) • double (64 bits) A double can hold any number from -1.7 * 10308 to 1.7 * 10308 (+ or -) 17,976,931,348,623,157.0 Lab 02-12 Primitive Data Types (char & boolean) • char (16 bits) • boolean (8 bits) A char holds a 16 bit unicode character. A boolean is a logical data type that holds two values - true and false. Lab 02-13 Variables In Java, variable names typically begin with lower case letters. • A variable is a “named container” that holds a value. • int q = 100; means: 100 q 1. q is a whole number (data type int) 2. q is storing the value 100 Lab 02-15 Variables (cont’d) • A variable must be declared before it can be used: int Type count; double x, y; long size; float area; short age; Name(s) Declarations Lab 02-16 Variables (cont’d) • The assignment operator = sets the variable’s value: count = 5; x = 0; age = 16; area = 45; Assignments • A variable can be initialized in its declaration: int count = 5; double pi = 3.14; Short age = 16; Declarations with initialization Lab 02-17 Constants Constants are exactly like fields except their values cannot be changed. In the field declaration the final modifiers is used. final double PI = 3.142; Lab 02-19 Constants Constants are exactly like fields except their values cannot be changed. In the field declaration the final modifiers is used. final double PI = 3.142; PI = 3.14; Lab 02-20 Constants Constants are exactly like fields except their values cannot be changed. In the field declaration the final modifiers is used. final double PI = 3.142; PI = 3.1415; Lab 02-21 Fields Fields are variables declared in the class. All methods of the class can access the fields. public class MyClass { public int age = 16; Field public void output() { System.out.println(age); } } Lab 02-23 Local Variables Local variables are declared in a method. They can only be used in the method in which they are declared. public class MyClass { public void output() { Local Variable int age = 16; System.out.println(age); } } Lab 02-24 Arithmetic (cont…) • Operators: +, -, /, * , % • The precedence of operators and parentheses work the same way as in algebra. • m % n means the remainder when m is divided by n (e.g. 17 % 5 is 2). • % has the same rank as / and * • Same-rank binary operators are performed in order from left to right. Lab 02-26 Arithmetic • Caution: if a and b are ints, then a / b is truncated to an int… 17 / 5 yeilds 3 3 / 4 yeilds 0 • …even if you assign the result to a double: double ratio = 2 / 3; The double type of the result doesn’t help: ratio still gets the value 0.0. Lab 02-27 Arithmetic (cont’d) • The type of the result is determined by the types of the operands, not their values; this rule applies to all intermediate results in expressions. • If one operand is an int and another is a double, the result is a double; if both operands are ints, the result is an int. Lab 02-28 Type Casting Java is a strong-typed language. Many conversions, specially those that imply a different interpretation of the value, require an explicit conversion. Lab 02-30 Type Casting (cont…) double d = 6.3; int n = d; Can you put a double into an int? Lab 02-31 Type Casting (cont…) double d = 6.3; int n = (int) d; Note: The data type not the value is placed inside a set of parentheses. Lab 02-32 Type Casting (cont…) int n = 35; int divisor = 3; double quotient = n / divisor; We can type cast either n or divisor into a double thereby forcing the solution to be performed using floating point math. Lab 02-33 Type Casting (cont…) int n = 35; int divisor = 3; double quotient = (double) n / divisor; Lab 02-34 Possible Loss Of Precision What happens when you pour a gallon of milk into a one quart container? You get an overflow! Lab 02-36 Possible Loss Of Precision The same thing may happen when you try to assign a double to an int or an int to a byte or an int to a char. Lab 02-37 Possible Loss Of Precision As a programmer, you needs to be able to recognize a loss of precision and you need to be able to fix the problem. Lab 02-38 Possible Loss Of Precision int n = 4.5; int n = 4; double n = 4.5; Lab 02-39 Possible Loss Of Precision double d = 6.3; int n = d; int n = (int) d; double n = d; Lab 02-40 Possible Loss Of Precision int n = 4.2 / 2.1; int n = (int) (4.2 / 2.1); double n = 4.2 / 2.1; Lab 02-41 Possible Loss Of Precision int n = 65; char c = n; char c = (char) n; Lab 02-42 Scanner • The Scanner class provides methods to read data from the keyboard. • To use the Scanner class you must import java.util.Scanner or java.util.*. • Requires an InputStream object as an argument. • Contains methods to read words, lines, integers and doubles. Lab 05-44 Scanner (cont’d) Instantiating an object of the Scanner class: Scanner reader = new Scanner(System.in); Default InputStream (Keyboard) Lab 05-45 Scanner (cont’d) Read the next integer: int number = reader.nextInt(); • nextInt() reads a single integer and places the number that is read into the variable number. • The end of an number is indicated by any white space. This could be the Space Bar, the Tab Key, or the Enter key. • When nextInt() reads a number, it leaves the white space in the input buffer. • If the number begins with white space, the white space is ignored and does not become part of the number. Lab 05-46 Scanner (cont’d) Read the next double: double number = reader.nextDouble(); • nextDouble() reads a single double and places the number that is read into the variable number. • The end of an number is indicated by any white space. This could be the Space Bar, the Tab Key, or the Enter key. • When nextDouble() reads a number, it leaves the white space in the input buffer. • If the number begins with white space, the white space is ignored and does not become part of the number. Lab 05-47 Scanner (cont’d) InputMismatchException nextInt() and nextDouble() will throw an InputMismatchException if the next value read is not an int or a double, respectively. Lab 05-48 Top Down Design Planning a trip to Europe? Save your money. The dollar doesn’t hold up well in Europe. You will only get .7522 Euros for every dollar you convert. Lets write a program that converts Dollars to Euros. $1 = € 0.7522 Lab 02-50 Top Down Design Lab 02-51 Top Down Design Start JCreator. Create a new file called “Lab02.java”. Save the new file in your Lab02 folder. Lab 02-52 Top Down Design – import statements An import statement is a way of making more of the functionality of Java available to your program. Java can do a lot of things, and not every program needs to do everything. So, to cut things down to size, so to speak, Java has its classes divided into "packages." Your own classes are part of packages, too. Lab 02-53 Top Down Design import java.util.Scanner; Lab 02-54 Top Down Design – define the class Every program in Java is a class. Lab 02-55 Top Down Design import java.util.Scanner; public class Lab02 { } Lab 02-56 Top Down Design (cont’d) We need two instance fields and one constant to solve the problem. Instance fields are values that are needed by more than one method. Usually they will be values read from the keyboard and the values we are solving for. Lab 02-57 Top Down Design - Fields public class Lab02 { private int dollars; private double euros; private static final double rate = .7522; } Lab 02-58 Top Down Design The “big problem” is defined in the main method. Lab 02-59 Top Down Design – main method public class Lab02 { private int dollars; private double euros; private static final double rate = .0705; public static void main(String[ ] args) { } } Lab 02-60 Top Down Design – main method public static void main(String[ ] args) { Lab 02 lab = new Lab02(); } Lab 02-61 Top Down Design – main method public static void main(String[ ] args) { Lab 02 lab = new Lab02(); lab.input(); } Lab 02-62 Top Down Design – main method public static void main(String[ ] args) { Lab 02 lab = new Lab02(); lab.input(); lab.process(); } Lab 02-63 Top Down Design – main method public static void main(String[] args) { Lab 02 lab = new Lab02(); lab.input(); lab.process(); lab.output(); } Lab 02-64 Top Down Design input(), process() and output( ) are the smaller parts of the problem. Lab 02-65 Top Down Design input() Lab 02-66 Top Down Design – input method public void input() { } Lab 02-67 Top Down Design – input method Declare an instance of a Scanner public void input() { Scanner reader = new Scanner(System.in); } Lab 02-68 Top Down Design – input method Prompt the user. public void input() { Scanner reader = new Scanner(System.in); System.out.print("Enter an amount in dollars: "); } Lab 02-69 Top Down Design – input method Read from the keyboard. public void input() { Scanner reader = new Scanner(System.in); System.out.print("Enter an amount in dollars: "); dollars = reader.nextInt(); } Lab 02-70 Top Down Design process() Lab 02-71 Top Down Design – process method public void process() { } Lab 02-72 Top Down Design – process method public void process() { euros = dollars * rate; } Lab 02-73 Top Down Design – process method public void process() { euros = dollars * rate; } dollars rate euros (dpllars * rate) 35 .7522 26.326999999999998 Lab 02-74 Top Down Design output() Lab 02-75 Top Down Design – output method public void output() { } Lab 02-76 Top Down Design – output method dollars rate euros (dpllars * rate) 35 .7522 26.326999999999998 public void output() { } Lab 02-77 Top Down Design – output method dollars rate euros (dpllars * rate) 35 .7522 26.326999999999998 public void output() { } 35 Dollars = 26.326999999999998 Euros Lab 02-78 Top Down Design – output method dollars rate euros (dpllars * rate) 35 .7522 26.326999999999998 public void output() { System.out.print(dollars } 35 Dollars = 26.326999999999998 Euros Lab 02-79 Top Down Design – output method dollars rate euros (dpllars * rate) 35 .7522 26.326999999999998 public void output() { System.out.print(dollars + “ dollars = ” } 35 Dollars = 26.326999999999998 Euros Lab 02-80 Top Down Design – output method dollars rate euros (dpllars * rate) 35 .7522 26.326999999999998 public void output() { System.out.print(dollars + “ dollars = ” + euros } 35 Dollars = 26.326999999999998 Euros Lab 02-81 Top Down Design – output method dollars rate euros (dpllars * rate) 35 .7522 26.326999999999998 public void output() { System.out.print(dollars + “ dollars = ” + euros + “ Euros”); } 35 Dollars = 26.326999999999998 Euros Lab 02-82 Top Down Design – output method Lab 02-83 Top Down Design import java.util.Scanner; import java.text.DecimalFormat; public class Lab02 { /* code not shown */ } Lab 02-85 Top Down Design – output method public void output() { DecimalFormat df = new DecimalFormat(“#.00”); System.out.println(dollars + " dollars = " + euros + " Euros"); } Lab 02-86 Top Down Design – output method public void output() { DecimalFormat df = new DecimalFormat(“#.00”); System.out.println(dollars + " dollars = " + df.format(euros) + " Euros"); } Lab 02-87 Top Down Design Lab 02-88 Questions? Lab 02-89