Midterm Helps You Can See During the Test
- Setup to read something from the screen:
- In Java: there are 2 commands needed:
- before the class starts, import the scanner class
using: import java.util.Scanner;
- inside the method, create a scanner object using: Scanner kbd = new
Scanner(System.in);
- Read something from the screen:
- In Java: <your variable> = < name of the scanner you
created above>.nextLine();
- Use the scanner method that matches your variable type.
- example to get the whole line: myStringVar
= kbd.nextLine();
- example to get the next word: myStringVar
= kbd.next();
- example to get the next double: mydoubleVar
= kbd.nextDouble();
- example to get the next int: myIntVar
= kbd.nextInt();
- See
Sun's page on scanner for much more
- Math
- absolute value: Math.abs(#)
- random: Math.random()
- minimum: Math.min(#,#)
- maximum: Math.max(#,#)
- raise to a power: Math.max(x,y) : Note: x is raised to y.
- Strings
- stringvar.charAt(3) // will return a character
- stringvar.length() // will return an integer
- stringvar.toUpperCase() // will return a String that
is all uppercase
- stringvar.toLowerCase() // will return a String that
is all lowercase
- stringvar.subString(2) // will return a String
starting in position 2 to the end
- stringvar.subString(2, 5) // will return a String
starting in position 2 and ending in position 4
- stringvar.equals(somethingOtherString) // will return
either true or false (a boolean)
(See your Java Tools)