1 / 7

What we ‘should’ have done in intro

What we ‘should’ have done in intro. 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

toby
Download Presentation

What we ‘should’ have done in intro

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What we ‘should’ have done in intro

  2. 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)

  3. Here is documentation for the Application class

  4. 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

  5. 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

  6. 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

More Related