|
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
Since System.out is the terminal window (which knows how to do things like print strings), you might think that its counterpart — the keyboard, aka System.in — is full of useful methods like “give me the next number typed in”. Alas, System.in is rather stupid; it can only read one letter at a time. Instead, we'lls a Scanner:
java.util.Scanner in; in = new java.util.Scanner( System.in ); System.out.print( "Enter a year: " ); int y = in.nextInt(); System.out.print( "Enter the name of the month: " ); String m = in.next(); System.out.println( "OMG, I was born in " + m + " of " + y + "!" ); |
java.util.Scanner in; in = new java.util.Scanner( System.in ); System.out.println( "Welcome to the 120th National Bank." ); System.out.print( "How much money would you like to start your account with? " ); int startBal = in.nextDouble(); BankAccount b1 = new BankAccount(startBal); System.out.print( "Okay, how much do you want to withdraw now?" ) int wAmt = in.nextDouble(); b1.withdraw(wAmt); System.out.println( "Your new balance is " + b1.getBalance() ); |
1 import java.util.Scanner; 2 3 /** 4 * Write a description of class PersonalTellerIO here. 5 * 6 * @author (your name) 7 * @version (a version number or a date) 8 */ 9 public class PersonalTeller 10 { 11 // The sole BankAccount that this PersonalTeller works with: 12 BankAccount b; 13 // A field which is an entire *object*, not just a primitive! 14 // We haven't seen this before, but it's the same concepts of fields 15 // and objects that we've always had, just layered on top of each other! 16 17 /** 18 * Constructor for objects of class PersonalTellerIO 19 */ 20 public PersonalTeller() 21 { 22 // Initialize our only field. 23 // When a brand new PersonalTeller is created, 24 // they'll manage one brand-new BankAccount with balance $0: 25 this.b = new BankAccount(0); 26 } 27 28 29 /** Have the PersonalTeller look up the balance, and print it to 30 * whoever is looking at the terminal screen. 31 */ 32 public void interactiveBalanceCheck() { 33 System.out.println( "The current balance is: " + b.getBalance()); 34 } 35 36 37 /** Have this PersonalTeller prompt the user-at-keyboard for 38 * the amount of deposit, and then go actually make that deposit. 39 */ 40 public void interactiveDeposit() { 41 System.out.print( "Please enter deposit amount: " ); 42 Scanner s; 43 s = new Scanner( System.in ); 44 double theNewDep = s.nextDouble(); 45 b.deposit( theNewDep ); 46 47 // The personalTeller will ask *themselves* to do a task 48 // (the 'interactiveBalanceCheck' task) ! 49 this.interactiveBalanceCheck(); 50 // Recall that 'this' is a local variable which 51 // is implicitly declared and initialized for you; 52 // it refers to 'the PersonalTeller object 53 // whose interactiveDeposit was called'. 54 } 55 56 public void interactiveWithdraw() { 57 } 58 59 60 61 void scannerDemo() { 62 Scanner in; 63 in = new Scanner( System.in ); 64 65 System.out.print( "Enter a year: " ); 66 int y = in.nextInt(); 67 System.out.print( "Enter the name of the month: " ); 68 String m = in.next(); 69 70 System.out.println( "\nOMG, I was born in " + m + " of " + y + "!" ); 71 } 72 73 } |
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
©2008, Ian Barland, Radford University Last modified 2008.Oct.13 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |