90 likes | 543 Views
Scanner class for input. Instantiate a new scanner object Scanner in = new Scanner( System.in ); Getting input using scanner int i = scanner.nextInt () double d = scanner.nextDouble (); String s = scanner.nextLine (); Differences from IO.read methods There is no popup window
E N D
Scanner class for input • Instantiate a new scanner objectScanner in = new Scanner(System.in); • Getting input using scanner • inti = scanner.nextInt() • double d = scanner.nextDouble(); • String s = scanner.nextLine(); • Differences from IO.read methods • There is no popup window • You just enter by typing at the bottom • You need to prompt the user what to enter
Command line Input • Using command line from Jgrasp • Click on build, then on run arguments • Enter the arguments that you want separated by spaces • Run the program and the program will use those arguments • Using the real command line • In Windows, click on start, then on run, then type cmd and click OK. You will be able to type Windows commands • Type:java <name of your program> <arguments separated with spaces> • Your program will run and use those command line arguments
Command Line Example • In Jgrasp, click on build, and then on run arguments, and enter 1 2 3 4 5 into the text box at the top • Enter the following program public class CommandLine { public static void main(String[] args) {int sum = 0; for (inti=0; i<args.length; i++) { sum += Integer.parseInt(args[i]); }System.out.println("Sum = " + sum); }} • Compile and run, the output should be: sum = 15 Note: parseInt is a static method that converts a string to an integer
Multiple Classes So far our applications had only a single class • Some applications can have hundreds of classes that work together to solve a problem • Each class is entered as a separate .java source file. • Compiling the main program will also compile all the classes it needs. • Design Challenge: How do we define an appropriate number of classes to create a structure for an application. This is a skill you learn in other Computer Science classes • Example: Creating a phone book (Following slides) • Class for a person • Class for a list of persons • Main class
The Person class public class Person { private String name, number; // Instance Variables public Person(String name, String number) // Constructor { this.name = name; this.number = number; } public String toString() // Return nicely formatted string { String spaces = “ ”; String s = (name + spaces).substring(0,spaces.length()); String t = (number + spaces).substring(0,spaces.length()); return s + “ ” + t; } // End of toString method } // End of Person class Note: We don’t want outside class methods accessing the instance variables, hence private Note: The toString() method is handy as you will see Note: The strings s and t mean that all Person outputs will be formatted the same way
Class for list of Persons public class PersonList { private Person[] persons; private inthowMany; public PersonList(int size) { persons=new Person[size]; howMany=0; } public booleanaddPerson(String person, String number) { if (howMany == persons.length) return false; else persons[howMany++] = new Person(person, number); return true; } public String toString() { String str = “Phone Book Listing\n”; for (inti=0; i<howMany; i++) str += persons[i] .toString() + “\n”; return str; } }
Main class Input a group of persons, and then print out the list public class Main { private static PersonListpersonList; public static void main(String[] args) { String phone, name; Scanner in = new Scanner(System.in); personList = new PersonList(10); do { System.out.print(“Enter name: ” ); name = in.nextString(); System.out.print(“Enter number: ”); phone = in.nextString(); } while (personList.addPerson(name, phone)); System.out.println(personList); } }
Final Thoughts • Using multiple classes makes the application more general • The Person class can be used by other programs • The details of the Person class is internal. Users only have to know how to call its methods • We did this from day one without knowing itExamples: System.out.println(), Math.round(), and IO.readString() • The private modifier is important • Normally make instance variables visible only where they are declared • This makes for easier modifications to an application • It enables the principle of limiting scope • The purpose of a constructor is to initialize an object • General Rule: Use the static modifier only when necessary