Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction Correct program design makes the complexity of today's program requirements possible. The average student thinks that any program over 1000.

Similar presentations


Presentation on theme: "Introduction Correct program design makes the complexity of today's program requirements possible. The average student thinks that any program over 1000."— Presentation transcript:

1

2 Introduction Correct program design makes the complexity of today's program requirements possible. The average student thinks that any program over 1000 lines in length is a large program. Yet a typical video game created in the late Nineties had programs that exceed 500,000 lines of programming. These video games are quite small compared to application programs like CAD and many other popular application programs.

3 Chapter 15 Programs The organization of the program examples is a little different in this chapter than it has been in previous chapters. Each program example has its own folder. The main driver program still follows the established convention by naming the programs Java1501.java, Java1502.java, etc., but folders are used to keep multiple files contained in the same location. This is especially important, because there will frequently be subtle differences introduced in files that are almost identical.

4 Modular Programming Program development requires that large programs are divided into smaller program modules to be manageable. This is the principle of divide and conquer.

5 // Java1501A.java // This program places all 10 required task into a single program module. public class Java1501A { public static void main(String args[]) { System.out.println("Java1501A.java\n"); System.out.println("Task 1\n"); System.out.println("Task 2\n"); System.out.println("Task 3\n"); System.out.println("Task 4\n"); System.out.println("Task 5\n"); System.out.println("Task 6\n"); System.out.println("Task 7\n"); System.out.println("Task 8\n"); System.out.println("Task 9\n"); System.out.println("Task 10\n"); System.out.println(); } Java1501A.java Output Java1501A.java Task 1 Task 2 Task 3 Task 4 Task 5 Task 6 Task 7 Task 8 Task 9 Task 10

6 // Java1501B.java // This program creates ten separate modules for each required task. public class Java1501B { public static void main(String args[]) { System.out.println("Java1501B.java\n"); Task1 t1 = new Task1(); Task2 t2 = new Task2(); Task3 t3 = new Task3(); Task4 t4 = new Task4(); Task5 t5 = new Task5(); Task6 t6 = new Task6(); Task7 t7 = new Task7(); Task8 t8 = new Task8(); Task9 t9 = new Task9(); Task10 t10 = new Task10(); System.out.println(); } class Task1 { public Task1() { System.out.println("Task 1\n"); } Java1501B.java Output Java1501B.java Task 1 Task 2 Task 3 Task 4 Task 5 Task 6 Task 7 Task 8 Task 9 Task 10 The other 9 tasks don’t fit here, but you should get the idea.

7 Simple Projects Many program languages, or rather the Integrated Development Environment (IDE) of many program languages require the creation of a project to compile any program. JCreator gives the programmer the option to work with or without a project. Keep in mind that the use of projects are IDE dependent. Why do we need projects? Imagine an Air-Traffic-Control Program. The program is 4,000,000 lines in length. Suppose just one module, which might be 1000 lines long needs to be edited. Now the entire 4,000,000 line long program must be compiled before it can be executed. This can take hours (even days!) With projects each separate module is compiled independently. If one module is changed, only that module needs to be recompiled. The project manager keeps track of all of the modules and determines which need recompiling, and which do not.

8 // Java1502.java // This program is a classic example of a program that does not require // the creation of a project. It is still possible to create a project if it is // desirable or your IDE requires the creation of a project for every // program. public class Java1502 { public static void main(String args[]) { System.out.println("Hi, I am a one-line program"); System.out.println(); } Java1502.java Output Hi, I am a one-line program

9 Multiple Class Warning Each class should have its own file. It is possible to compile multiple classes that are placed into a single file, but it is not very good program design.

10 // Java1503.java // This program uses multiple classes, all inside one file. This is not // considered good program design. There should be one file for each class. public class Java1503 { public static void main(String args[]) { System.out.println("\nJava1503.java\n"); Widget w = new Widget(10); System.out.println("Starting with " + w.getWidgets() + " widgets"); w.setWidgets(30); System.out.println("Ending with " + w.getWidgets() + " widgets"); System.out.println(); Aardvark a = new Aardvark(20); System.out.println("Starting with " + a.getAardvarks() + " aardvarks"); a.setAardvarks(50); System.out.println("Ending with " + a.getAardvarks() + " aardvarks"); System.out.println(); Mango m = new Mango(25); System.out.println("Starting with " + m.getMangos() + " mangos"); m.setMangos(75); System.out.println("Ending with " + m.getMangos() + " mangos"); System.out.println(); } // The other classes are shown on the next slide.

11 // Java1503.java continued class Widget { private int numWidgets; public Widget(int w){ numWidgets = w; } public int getWidgets() { return numWidgets; } public void setWidgets(int w) { numWidgets = w; } } class Aardvark { private int numAardvarks; public Aardvark(int a) { numAardvarks = a; } public int getAardvarks() { return numAardvarks; } public void setAardvarks(int a) { numAardvarks = a; } } class Mango { private int numMangos; public Mango(int m) { numMangos = m; } public int getMangos() { return numMangos; } public void setMangos(int m) { numMangos = m; } } Java1503.java Output Java1503.java Starting with 10 widgets Ending with 30 widgets Starting with 20 aardvarks Ending with 50 aardvarks Starting with 25 mangos Ending with 75 mangos

12 // Java1504.java // This program is now divided up into four separate files for four separate classes. Provided all // necessary files are placed in the same directory, the "file-compile" method can still work. public class Java1504 { public static void main(String args[]) { System.out.println("\nJava1504.java\n"); Widget w = new Widget(10); System.out.println("Starting with " + w.getWidgets() + " widgets"); w.setWidgets(30); System.out.println("Ending with " + w.getWidgets() + " widgets"); System.out.println(); Aardvark a = new Aardvark(20); System.out.println("Starting with " + a.getAardvarks() + " aardvarks"); a.setAardvarks(50); System.out.println("Ending with " + a.getAardvarks() + " aardvarks"); System.out.println(); Mango m = new Mango(25); System.out.println("Starting with " + m.getMangos() + " mangos"); m.setMangos(75); System.out.println("Ending with " + m.getMangos() + " mangos"); System.out.println(); } Java1504.java Output Java1504.java Starting with 10 widgets Ending with 30 widgets Starting with 20 aardvarks Ending with 50 aardvarks Starting with 25 mangos Ending with 75 mangos

13 // Widget.java // Goes with Java1504.java public class Widget { private int numWidgets; public Widget(int w) { numWidgets = w; } public int getWidgets() { return numWidgets; } public void setWidgets(int w) { numWidgets = w; } Java1504.java Output Java1504.java Starting with 10 widgets Ending with 30 widgets Starting with 20 aardvarks Ending with 50 aardvarks Starting with 25 mangos Ending with 75 mangos

14 // Aardvark.java // Goes with Java1504.java public class Aardvark { private int numAardvarks; public Aardvark(int a) { numAardvarks = a; } public int getAardvarks() { return numAardvarks; } public void setAardvarks(int a) { numAardvarks = a; } Java1504.java Output Java1504.java Starting with 10 widgets Ending with 30 widgets Starting with 20 aardvarks Ending with 50 aardvarks Starting with 25 mangos Ending with 75 mangos

15 // Mango.java // Goes with Java1504.java public class Mango { private int numMangos; public Mango(int m) { numMangos = m; } public int getMangos() { return numMangos; } public void setMangos(int m) { numMangos = m; } Java1504.java Output Java1504.java Starting with 10 widgets Ending with 30 widgets Starting with 20 aardvarks Ending with 50 aardvarks Starting with 25 mangos Ending with 75 mangos

16 Creating a Project – Step 1 Click Project followed by New Project.

17 Creating a Project – Step 2 Click on the Projects tab, if it is not already selected. Click Empty Project. In the Project name: window, type Project1505.

18 Creating a Project – Step 3 Click the... browse button next to the Location window. Click on the Java1505 folder. Click the OK button twice.

19 Creating a Project – Step 4 Click Project and Add Files.

20 Creating a Project – Step 5 Move up one folder by clicking the Up arrow. all four of the.java files. Click the Open button.

21 Creating a Project – Step 6 Notice the Project1505 in the Workspace window. Click the [+] in front of Project1505 to view the included files. Double-Click on any file to make it appear in the edit window.

22 Creating a Project – Step 7 Click on the Compile Project (not Compile File) button or F7. If all the files are correct, you will see Process completed.

23 Creating a Project – Step 8 Click the Execute Project (not Execute File) button or F5. The output window is identical to compiling/executing files.

24 Creating a Project – Step 9 Click File. Click Save Workspace, if you want to save this project. Click Close Workspace, if you want to close the project. NOTE: Not closing your project will mess up the student in the next class!

25 // Java1506.java // This program is once again identical to programs Java1504 and Java1505. // This time the class files are placed in different folders. // You cannot compile the program now without a project. public class Java1506 { public static void main(String args[]) { System.out.println("\nJava1506.java\n"); Widget w = new Widget(10); System.out.println("Starting with " + w.getWidgets() + " widgets"); w.setWidgets(30); System.out.println("Ending with " + w.getWidgets() + " widgets"); System.out.println(); Aardvark a = new Aardvark(20); System.out.println("Starting with " + a.getAardvarks() + " aardvarks"); a.setAardvarks(50); System.out.println("Ending with " + a.getAardvarks() + " aardvarks"); System.out.println(); Mango m = new Mango(25); System.out.println("Starting with " + m.getMangos() + " mangos"); m.setMangos(75); System.out.println("Ending with " + m.getMangos() + " mangos"); System.out.println(); }

26 Java1506.java:13: cannot resolve symbol symbol : class Widget location: class Java1506 Widget w = new Widget(10); ^ Java1506.java:13: cannot resolve symbol symbol : class Widget location: class Java1506 Widget w = new Widget(10); ^ Java1506.java:19: cannot resolve symbol symbol : class Aardvark location: class Java1506 Aardvark a = new Aardvark(20); ^ Java1506.java:19: cannot resolve symbol symbol : class Aardvark location: class Java1506 Aardvark a = new Aardvark(20); ^ Java1506.java:25: cannot resolve symbol symbol : class Mango location: class Java1506 Mango m = new Mango(25); ^ Java1506.java:25: cannot resolve symbol symbol : class Mango location: class Java1506 Mango m = new Mango(25); ^ 6 errors Java1506.java Output when trying to compile the file Java1506.java. Since these files are all in different folders, this cannot work without projects.

27 Java1506.java Output #2 This is the output you get when you create a project. You will need to browse to different folders to find all of the files.

28 Creating Simple Projects 1.Start a new project 2.Give the project a name. 3.Specify the location where all project information is stored. 4.Add all the files that will be used with the project. 5.Compile the project. 6.Execute the project. 7.Save the project.

29 Projects With jar Files Some clever programmers manage to compress a bunch of their classes into special Java Archive Files, or JAR files. Such files contain multiple classes and these files all end with.jar. Additional steps will be necessary to create projects that use.jar files. Once again realize that these steps are IDE dependent. The steps that follow will be those that are required for JCreator.

30 AP Exam Alert The Marine Biology Case Study (MBCS) will be tested on the AP Computer Science Examination. The MBCS is copyrighted by the College Board and Educational Testing Service. A wealth of information about the MBCS is available on AP Central, the official website of the College Board for AP Computer Science teachers.

31 Files Used by the MBCS BoundedEnv.java DarterFish.java Environment.java Fish.java mbsbb.jar mbsgui.jar MBSGUI1.java MBSGUI2.java MBSGUI3.java Simulation.java SlowFish.java UnboundedEnv.java

32 Compiling MBCS – Step 1 Click File. Click New. Click the Workspaces tab.

33 Compiling MBCS – Step 2 Type MBCS in the Workspace name window. Click the location browse button... and select Java1507.

34 Compiling MBCS – Step 3 Click Project, New Project and click Empty Project. Type MBCS1 in the Project name window. Repeat this process 2 more times using names MBCS2 & MBCS3.

35 New Project Alert A new projects defaults to Basic Java Application, which includes a main method that you did not create. Failure to select Empty Project results in executing a program with two different main methods, and potentially bizarre execution consequences.

36 Compiling MBCS – Step 4 Click Project, and Set Active Project. Select MBCS1. Click Project and Add Files. Navigate up two folder levels. Select all.java files, except MBSGUI2.java and MBSGUI3.java. Click the Open button. Click the + in front of MBCS1

37 Compiling MBCS – Step 5 Repeat Step 4 two more times. First, change the active project to MBCS2 and then add all the files except MBSGUI1.java and MBSGUI3.java. Second, change the active project to MBCS3 and then add all the files except MBSGUI1.java and MBSGUI2.java.

38 Compiling MBCS – Step 6 Try to compile the project. This should be possible based on your experience with projects performed by the previous exercise. You will find that Java is unhappy with an error window output shown below.

39 Compiling MBCS – Step 7 You must add the.jar files to the project before compiling is possible. Click Project and Project Settings. Click the Required Libraries tab. Click the New... button.

40 Compiling MBCS – Step 8 You must add the.jar files to the project before compiling is possible. Click Project and Project Settings. Click the Required Libraries tab. Click the New... button.

41 Compiling MBCS – Step 9 Navigate to the Java1507 folder. the two.jar files. Click the Open button and the OK button.

42 Compiling MBCS – Step 10 Place a Check mark in the box next to MBCSJars. You have created the library. The check associated it with your project.

43 Compiling MBCS – Step 11 Click the Compile button three times for each one of the three projects. Make sure to use Set Active Project to change between projects. All three projects should compile.

44 Compiling MBCS – Step 12 Make MBCS1 the active project and execute the project. Be prepared to wait a while for the execution. This a very large program and the computer needs to do lots of processing to get to this stage.

45 Compiling MBCS – Step 13 Your first reaction to the MBCS execution may be Now what? Click File followed by Create new environment...

46 Compiling MBCS – Step 14 The default fish environment is 10 rows by 10 columns. Keep the default environment by clicking the Create button.

47 Compiling MBCS – Step 15 Place 9 fish at the indicated locations Click on the cell and fish of random color will appear. If a specific fish color is desired you can click the Color button. Click the Run button to see the fish move. Executing the MBCS2 and MBCS3 projects are very similar. The difference is that MBCS2 has 2 types of fish and MBCS3 has 3.

48 Another Look at Comments You have also seen comments, which are statements that do not compile. One- line comments are created with // and multiple lines can be created with /* */. Folder 1508 contains three classes that use a combination of self-commenting identifiers and non-compiling comments.

49 Using Javadoc Javadoc is a special program that creates webpage-based documentation of your classes and their components along with your provided comments. There is a twist though; the comments need to be placed prior to the method or item being commented and you need to use double asterisks for your comments. In folder 1509 you will find the same Aardvark, Mango and Widget classes with the same comments using a slightly different format. It is a third style of commenting that is picked up by Javadoc. /** * This third style of comments uses a slash * and a double asterisk. **/

50 /** * Aardvark2.java * The Aardvark2 class stores the total number of aardvarks in any Aardvark2 object. * There is one constructor, one get method and one set method. * Goes with Java1509 folder **/ public class Aardvark2 { /** * numAardvark attribute store the total number of aardvarks. **/ private int numAardvarks; /** * Aardvark2 one-parameter constructor, which constructs an object and * initializes the numAardvarks attribute. **/ public Aardvark2(int a) { numAardvarks = a; } The other classes and methods use the new comments in the same way and will not be shown.

51 Using Javadoc – Step 1 Get to the Command or DOS prompt. Type CD C:\Java\ChapterProgs\Progs15\Java1509 (This may be different depending on the Java1509 folder location) You are now located at the 3 classes with special Javadoc comments.

52 Using Javadoc – Step 2 You need to create access to the Javadoc program with a path command. Type path C:\Java\bin

53 Using Javadoc – Step 3 Now you can use the Javadoc program to create the web page documents. Type Javadoc *.* You will see many.html created.

54


Download ppt "Introduction Correct program design makes the complexity of today's program requirements possible. The average student thinks that any program over 1000."

Similar presentations


Ads by Google