70 likes | 157 Views
Learn how to organize Java code into manageable methods using an application class. Discover how to run and execute programs from the command line effectively. Detailed documentation and examples provided.
E N D
Now we know about other classes and the main() and stuff • I have shown how we could reorganise a solution to the homework in zz-introduction-done-right • An application class that ‘runs’ the code you previously typed into GWD and pasted into the code pad • It also splits up the code into more manageable methods (call just with name if in same class) e.g star() or circle() • I used Kerry’s code – thanks (see directory)
With a main program of: public static void main(String args[]) { Application app=new Application(); app.runApp(); } And runApp() starts: public class Application{ private InputGetter reader; public Application() { reader=new InputGetter(); } public void runApp() { reader.getInputFromUser("How many times to loop"); int num=Integer.parseInt(reader.getCurrentValue()); for (int i=0;i<num;i++) { System.out.print("Going round the loop "+(i+1)+" time "); reader.getInputFromUser("what do you want to draw in the loop time "+(i+1)+"?"); String name=reader.getCurrentValue(); if (name.equals("star")) { star(); }Etc etc
public void runApp() { reader.getInputFromUser("How many to loop"); int num=Integer.parseInt(reader.getCurrentValue()); for (int i=0;i<num;i++) { System.out.print("Going round "+(i+1)+" time "); String name=reader.getCurrentValue(); if (name.equals("star")) { star(); } else if (name.equals("circle")) { circle(); } else if (name.equals("ltt")) { ltt(); } else { System.out.println("cannot do that!"); } } } public void star() { System.out.println("draws star"); //etc
So, we can now write programs • These programs can be run from the command line by typing: java Application • This will run the main, which in turn creates a new Application called app and then runs its runApp() method • Maybe we should have done this from start?? • The book doesn’t do it until chapter 5 or so