Download presentation
Presentation is loading. Please wait.
Published byMerryl Richardson Modified over 9 years ago
1
Week 2 - Friday
2
What did we talk about last time? Data representation Binary numbers Types int boolean double char String
5
To output stuff, we just use System.out.println() What about input? Input is a little trickier We need to create a new object of type Scanner System.out.println("Flip mode is the squad!"); System.out.println(35); System.out.println("Flip mode is the squad!"); System.out.println(35);
6
There are three parts to using Scanner for input 1. Include the appropriate import statement so that your program knows what a Scanner object is 2. Create a specific Scanner object with a name you choose 3. Use the object you create to read in data
7
Lots of people have written all kinds of useful Java code By importing that code, we can use it to help solve our problems To import code, you type import and then the name of the package or class To import Scanner, type the following at the top of your program (before the class !) import java.util.Scanner;
8
Once you have imported the Scanner class, you have to create a Scanner object To do so, declare a reference of type Scanner, and use the new keyword to create a new Scanner with System.in as a parameter like so: You can call it whatever you want, I chose to call it in Doesn't make any sense? For now, that's okay. Scanner in = new Scanner(System.in);
9
Now that you've got a Scanner object, you can use it to read some data It has a method that will read in the next piece of data that user types in, but you have to know if that data is going to be an int, a double, or a String Let's say the user is going to input her age (an int ) and you want to store it in an int variable called years We'll use the nextInt() method to do so: int years; years = in.nextInt(); int years; years = in.nextInt();
10
Scanner has a lot of methods (ways to accomplish some tasks) For now, we're only interested in three These allow us to read the next int, the next double, and the next String, respectively: Scanner in = new Scanner(System.in); int number = in.nextInt(); double radius = in.nextDouble(); String word = in.next(); Scanner in = new Scanner(System.in); int number = in.nextInt(); double radius = in.nextDouble(); String word = in.next();
11
import java.util.Scanner; public class Age { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("What is your age?"); int years; years = in.nextInt(); years = years * 2; System.out.print("Your age doubled is "); System.out.println(years); } import java.util.Scanner; public class Age { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("What is your age?"); int years; years = in.nextInt(); years = years * 2; System.out.print("Your age doubled is "); System.out.println(years); } }
13
In Java, each data type has a set of basic operations you are allowed to perform It’s not possible to define new operations or change how the operations behave Some programming languages allow this, but not Java
14
Today we are going to consider the basic operations for numerical types: int double
16
Use the + operator to add two int s together int a; int b; a = 5 + 6;// a contains 11 b = a + 3;// b contains 14 a + b; // not allowed, does nothing a = a + 1;// a contains 12, and b? int a; int b; a = 5 + 6;// a contains 11 b = a + 3;// b contains 14 a + b; // not allowed, does nothing a = a + 1;// a contains 12, and b?
17
Some expressions are used so often, Java gives us a short cut x = x + y; can be written x += y; x = x + 1; can be written x++; int x; x = 6;// x contains 6 x += 4;// x contains 10 x++; // x contains 11 int x; x = 6;// x contains 6 x += 4;// x contains 10 x++; // x contains 11
18
Exactly like + except performs subtraction int a; int b; a = 5 - 6;// a contains -1 b = 3 - a;// b contains 4 a -= 10; // shortcut for a = a – 10; a--; // shortcut for a = a – 1; int a; int b; a = 5 - 6;// a contains -1 b = 3 - a;// b contains 4 a -= 10; // shortcut for a = a – 10; a--; // shortcut for a = a – 1;
19
The * operator performs multiplication int a; int b; a = 5 * 6;// a contains 30 b = a * 3;// b contains 90 a *= 2; // shortcut for a = a * 2; int a; int b; a = 5 * 6;// a contains 30 b = a * 3;// b contains 90 a *= 2; // shortcut for a = a * 2;
20
The / operator performs integer division Not the same as regular division The factional part is dropped, not rounded int a; int b; a = 3;// a contains 3 b = a / 2;// b contains 1 a /= 2; // shortcut for a = a / 2; int a; int b; a = 3;// a contains 3 b = a / 2;// b contains 1 a /= 2; // shortcut for a = a / 2;
21
The % operator is the mod operator It finds the remainder after division This operator is a good way to find out if a number is even or odd int a; int b; a = 8;// a contains 8 b = a % 5;// b contains 3 a %= 2; // shortcut for a = a % 2; int a; int b; a = 8;// a contains 8 b = a % 5;// b contains 3 a %= 2; // shortcut for a = a % 2;
22
Compute the area of a rectangle Area = length ∙ width length widthArea
24
Exactly the same as + for int, except now you can have fractional parts double a; double b; a = 3.14159;// a contains 3.14159 b = a + 2.1;// b contains 5.24159 a += 1.6; // shortcut for a = a + 1.6; a++; // shortcut for a = a + 1.0; double a; double b; a = 3.14159;// a contains 3.14159 b = a + 2.1;// b contains 5.24159 a += 1.6; // shortcut for a = a + 1.6; a++; // shortcut for a = a + 1.0;
25
No surprises here They do subtraction and multiplication double a; double b; a = 3.14159;// a contains 3.14159 b = a - 2.1;// b contains 1.04159 a = b * 0.5;// a contains 0.520795 double a; double b; a = 3.14159;// a contains 3.14159 b = a - 2.1;// b contains 1.04159 a = b * 0.5;// a contains 0.520795
26
Unlike int, this division does have fractional parts Can you explain this mystery? double a; double b; a = 3;// a contains 3.0 b = a / 2;// b contains 1.5 b = 3 / 2;// b contains 1.0 double a; double b; a = 3;// a contains 3.0 b = a / 2;// b contains 1.5 b = 3 / 2;// b contains 1.0
27
Yes, there is a % operator for double s, but no one uses it So, don’t worry about it
28
Given a temperature in Celsius, what is the equivalent in Fahrenheit? T F = (9/5)T C + 32
29
How complex can expressions get? What’s the value of a ? 18! int a = 31; int b = 16; int c = 1; int d = 2; a = b + c * d – a / b / d; int a = 31; int b = 16; int c = 1; int d = 2; a = b + c * d – a / b / d;
30
Order of operations holds like in math You can use parentheses to clarify or change the precedence Now a is 16 int a = 31; int b = 16; int c = 1; int d = 2; a = (((b + c) * d) – a / b) / d; int a = 31; int b = 16; int c = 1; int d = 2; a = (((b + c) * d) – a / b) / d;
31
You cannot directly store a double value into an int variable However, you can cast the double value to convert it into an int Casting tells the compiler that you want the loss of precision to happen You can always store an int into a double int a = 2.6;// fails! int a = (int)2.6;// succeeds! (a = 2)
32
In Java, the conversion of a double into an int does not use rounding As in the case of integer division, the value is always rounded down You can think of this as using the floor function from math If you want to round normally, you can simply add 0.5 before the cast double x = 2.6; int a = (int)(x + 0.5);// rounds double x = 2.6; int a = (int)(x + 0.5);// rounds
35
Examples Advanced math operations Operations on boolean and char types
36
Keep reading Chapter 3 of the textbook Get an early start on Project 1 (as soon as it is assigned)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.