Download presentation
Presentation is loading. Please wait.
1
Chapter 2 More on Math More on Input
Math and I/O Chapter 2 More on Math More on Input
2
Math Math operators different for ints & doubles Addition: +
7 + 3 10.0 Subtraction: – 7 – 3 – 3.0 4.0 Multiplication: * 7 * 3 * 3.0 21.0
3
Division Operators Division: / Modulus (Division Remainder): %
7 / 3 / 3.0 integer division for ints; floating-point division for doubles (mixed expressions – later) Modulus (Division Remainder): % 7 % 3 % 3.1 1.5
4
Math Expressions int m; m int n; ? n m = 3 + 5; 8 ? n = 4 * m; 8 32 m = n % 5; 2 32 n = n / 5; 2 6 n = m + 8 / m + 3; 2 __
5
Order of Operations Precedence: the order things get done
Left to right except: Parentheses ( ... ) done before Multiplying * / % done before Adding + – done before PEMDAS / PEDMAS parentheses, (exponents), mult/div, add/sub Java doesn’t do exponents
6
Common Mistake x + 8 / x + 3, where x = 2 BUT (x + 8) / (x + 3)
2 + 8 / 2 + 3 6 + 3 = 9 BUT (x + 8) / (x + 3) 10 / 5 = 2
7
Exercises 2 * 2 + 3 * 4 = _____ 5 / 3 – 1 = _____
4 + 3 * 4 = 7 * 4 = 28 4 + 3 * 4 = = 16 2 * 5 * 4 = 10 * 4 = 40 5 / 3 – 1 = _____ 1.667 – 1 = 0.667 5 / 2 = 2.5 5 / 2 = 2 1 – 1 = 0
8
Mixed Mode Expressions
Expressions with both doubles and ints = ? Integers are converted to double = 5.5 Type Coercion Just what you’d expect usually!
9
When Coercion Happens Not before needed
int sub-expressions use int math / / 2 ( ) / 2 ( ) / 2 / 2 / 2.0 4.3
10
Mixed Mode Assignments
Assign an int value to a double variable… double x; x = 0; …it gets automatically changed to double assignment above is same as x = 0.0; Use the second way, anyway! helps prevent confusion
11
Mixed Mode Assignments
Assign an double value to an int variable… int n; n = 0.0; Java compiler objects! can’t assign a double value to an int variable! even tho’ 0.0 is equal to 0, which is an integer! “possible loss of precision” error
12
Type Casting You can tell the computer to change a number from one type to another used especially with variables int count = 0; int sum = 0; … double average = (double)sum / (double)count; 45 / 4 = 11 45.0 / 4.0 = 11.25 11.25 average count 45 count 4 sum sum 45.0 4.0
13
Changing Double to Int When change double to int…
double price; int intPrice; price = 19.95; // price in $ and ¢ intPrice = (int)price; // just $ …everything after the decimal point is lost it is not “rounded off”! we’ll see how to round off later 19.95 price 19 intPrice
14
Exercises Say what value gets assigned int j; double f; j f
f = f + 2.6; __ __ j = (int)f; __ __ f = (double)(j / 2) + 1.1; __ __
15
Doing Input with Scanners
System.out is useful for doing output There is also a System.in it’s connected (usually) to our keyboard it’s not nearly so useful (for beginners) it doesn’t know how to read a number So for input we will use Scanners need to tell the computer we’ll be using them need to create the Scanners we use
16
Getting Ready for Input
Tell the computer we’ll be using Scanners import java.util.Scanner; Scanner is a class (a piece of software) Create the scanner we will use Scanner keyboard = new Scanner(System.in); keyboard is an object (a piece of s/w in use) it uses System.in (it knows how) this command is a(n object) variable declaration
17
Reading Data Read a variable value by assigning into it
n1 = keyboard.nextInt(); nextInt is the method a Scanner uses to get the next integer keyboard is the Scanner we’re using (remember that it’s connected to our keyboard) keyboard.nextInt() returns the next integer the assignment command (=) puts that number into the variable (in this case n1)
18
Exercise nextInt read next int value
What is the command for reading a double value? write a Java command to prompt for and read the length and width of a table (in metres) … System.out.println("The table’s area is " + (length * width) + " square metres.");
19
Prompting for Input Prompt for input
otherwise user just staring at a blank screen! System.out.print(" Enter a number: "); n1 = keyboard.nextInt(); System.out.print(" Enter another: "); n2 = keyboard.nextInt(); Enter a number: 32 Enter another: 47 Enter a number: 32 Enter another: Enter a number: Enter a number: 32
20
Input & Output on the Monitor
Input & output mixed together on the screen Black = produced by System.out Blue = typed by user, read by keyboard Note: two separate “streams”! computer will not read the 79—it’s output Enter a number: 32 Enter another number: 47 Their sum is 79.
21
The Two Streams Computer prints User types
Enter a number: (pause) 32 Enter another: (pause) 47 Their sum is 79. Computer pauses when it needs more input End of a number indicated by enter/return ( on diagram above) or anything that’s not part of a number
22
Anticipating the Computer
Computer prints User types Enter a number: (pause) 32 47 Enter another: Their sum is 79. Two numbers on first line (space between) Second number already in stream when computer gets to second input command Computer will not pause before reading 2nd # Don’t need/get to type the 2nd
23
Preventing Anticipation
nextLine method reads everything up to and including the next character System.out.print(“Enter a number: ”); n1 = keyboard.nextInt(); keyboard.nextLine(); System.out.print(“Enter another: ”); n2 = keyboard.nextInt(); Enter a number: (pause) 32 47 Enter another: (pause) 55 Their sum is 87.
24
Question Where did the 47 go?
we read it, but we didn’t put it anywhere (no assignment command) computer read it, then immediately forgot it What did the second nextLine read & forget? Enter a number: (pause) 32 47 Enter another: (pause) 55 Their sum is 87.
25
Pausing for ENTER Can use nextLine to pause input Except….
wait for user to press ENTER System.out.print(“Press ENTER to continue”); keyboard.nextLine(); Press ENTER to continue(pause) Except…. What if there’s already a in the input stream?
26
Pausing for ENTER What if there’s already a in the input stream?
System.out.print(“Enter a number: ”); n2 = keyboard.nextInt(); System.out.print(“Press ENTER to continue”); keyboard.nextLine(); Enter a number: (pause) 32 Press ENTER to continue: Computer does not pause. WHY NOT?
27
Tidying Up As You Go Along
Use nextLine() whenever you expect the user to press the ENTER key gets rid of the and everything before it makes sure that your next input statement gives the user a chance to type something
28
Exercise Write commands to Make sure numbers entered separately
prompt for length read length (double value) prompt for width read width (double value) echo back length and width pause for user to press enter key Make sure numbers entered separately then change so numbers entered on one line
29
Reading Strings You can save the lines you read
String line; System.out.print("Enter a line: "); line = keyboard.nextLine(); System.out.print("You said \"" + line + "\""); here we put the stuff we read into a variable (Note: the character doesn’t get saved)
30
Reading Strings You can also read Strings word by word String w1, w2;
System.out.print("Enter two words: "); w1 = keyboard.next(); w2 = keyboard.next(); keyboard.nextLine(); // tidy up! Note: NOT nextWord()! Enter two words: two words ? w1 “two” w1 “words” w2 ? w2
31
Exercise Write code to read in a person’s name…
first name (one word) and last name (one word) …then print out their name in this format: Last, First …then pause for user to press Enter What is your full name? Mark Young Young Mark Press ENTER to continue...
32
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.