AP Computer Science Static Methods, Strings, User Input Static Methods The Math class contains a special type of methods, called static methods. A static method DOES NOT operate on an object. This is because the methods of the Math class operate on numbers, and in Java numbers are NOT objects. Instead, Math methods require a parameter. How can you tell a method, from a class, from an object? CLASS - names should ALWAYS start with a capital letter (e.g., Math, Rectangle, String) METHODS names should NEVER start with a capital letter so they are not confused with Class names (you should use capitals in the middle of the name to improve understanding, e.g., getbalance()). Method calls are ALWAYS followed by () sometimes with parameters, sometimes empty (). OBJECTS names should NEVER start with a capital letter so they are not confused with Class names. The name of an object should have similarity with the name of the Class that creates it (e.g., a object of the Rectangle class might be called myrectangle.) Objects can be distinguished from methods because they are not followed by (). EXAMPLES: Math.sqrt(4); System.out.println( Hello ); myrectangle.getheight();
How to program a mathematical formula (see HOW TO 4.1 for detailed answer). 1. Understand the problem what are the inputs? outputs? Example: Stamp Machine 2. WORK OUT EXAMPLES BY HAND. (If you can t do it, you won t be able to make the computer do it!) Customer enters $5.00 in a stamp machine. How many $.44 stamps will they receive and how many $.01 stamps. 3. Turn the arithmetic steps into a mathematical equation that represents the situation. 4. Turn the mathematical equations into Java expressions.
5. Build a class that carries out the calculations. 6. Test the class (use your examples from step 2 plus some others; be sure you know the answer first, so you know whether your program works!) String Methods A string is a sequence of characters. Specific strings are OBJECTS of the STRING class. You can concatenate strings using.concat() method or the + sign. - Only 1 side of the + sign needs to be a string; the other one will be forced into being a string. String name = Agent int n = 7; System.out.println(name + 7);.length() is a method of the String class that will return the # of characters in the string. Integer.parseInt() and Double.parseDouble() will turn a character that is a number into the value of the number. This is usually used to interpret user input. (these are both static methods.)
.substring(start, onepastend) will return the characters of a string as specified by the parameters (characters in a string start at position 0.) (If second parameter is missing, the substring will go to the end of the string.) Reading User Input The Scanner class is used for capturing user input from a console window. To use this class, include: import java.util.scanner at the beginning of your program. You can create a Scanner object as follows: Scanner in = new Scanner(System.in); Methods you can use include: in.nextint() in.nextdouble() in.nextline() in.next()
Example of getting user input for CashRegister Class ch04/cashregister/cashregistersimulator.java 1 import java.util.scanner; 2 3 /** 4 This program simulates a transaction in which a user pays for an item 5 and receives change. 6 */ 7 public class CashRegisterSimulator 8 { 9 public static void main(string[] args) 10 { 11 Scanner in = new Scanner(System.in); 12 13 CashRegister register = new CashRegister(); 14 15 System.out.print( Enter price: ); 16 double price = in.nextdouble(); 17 register.recordpurchase(price); 18 19 System.out.print( Enter dollars: ); 20 int dollars = in.nextint(); 21 System.out.print( Enter quarters: ); 22 int quarters = in.nextint(); 23 System.out.print( Enter dimes: ); 24 int dimes = in.nextint(); 25 System.out.print( Enter nickels: ); 26 int nickels = in.nextint(); 27 System.out.print( Enter pennies: ); 28 int pennies = in.nextint(); 29 register.enterpayment(dollars, quarters, dimes, nickels, pennies); 30 31 System.out.print( Your change: ); 32 System.out.println(register.giveChange()); 33 } 34 } Output Enter price: 7.55 Enter dollars: 10 Enter quarters: 2 Enter dimes: 1 Enter nickels: 0 Enter pennies: 0 Your change: 3.05 ADVANCED TOPIC To learn how to format your output, see Advanced Topic 4.6