introduction correct program design makes the complexity of today's program requirements...

54

Upload: eustace-logan-thomas

Post on 17-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

Chapter 15 ProgramsThe 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.

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

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.

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

// 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

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

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

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

Simple ProjectsMany 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.

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

// 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

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

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.

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

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

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

// 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 widgetsEnding with 30 widgets

Starting with 20 aardvarksEnding with 50 aardvarks

Starting with 25 mangosEnding with 75 mangos

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

// 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 widgetsEnding with 30 widgets

Starting with 20 aardvarksEnding with 50 aardvarks

Starting with 25 mangosEnding with 75 mangos

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

// 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 widgetsEnding with 30 widgets

Starting with 20 aardvarksEnding with 50 aardvarks

Starting with 25 mangosEnding with 75 mangos

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

// 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 widgetsEnding with 30 widgets

Starting with 20 aardvarksEnding with 50 aardvarks

Starting with 25 mangosEnding with 75 mangos

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

// 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 widgetsEnding with 30 widgets

Starting with 20 aardvarksEnding with 50 aardvarks

Starting with 25 mangosEnding with 75 mangos

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

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

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

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

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

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

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

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

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

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

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

Creating a Project – Step 6Notice 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.

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

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

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

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

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

Creating a Project – Step 9Click 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!

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

// 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();

}}

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

Java1506.java:13: cannot resolve symbolsymbol : class Widgetlocation: class Java1506 Widget w = new Widget(10); ^Java1506.java:13: cannot resolve symbolsymbol : class Widgetlocation: class Java1506 Widget w = new Widget(10); ^Java1506.java:19: cannot resolve symbolsymbol : class Aardvarklocation: class Java1506 Aardvark a = new Aardvark(20); ^Java1506.java:19: cannot resolve symbolsymbol : class Aardvarklocation: class Java1506 Aardvark a = new Aardvark(20); ^Java1506.java:25: cannot resolve symbolsymbol : class Mangolocation: class Java1506 Mango m = new Mango(25); ^Java1506.java:25: cannot resolve symbolsymbol : class Mangolocation: 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.

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

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

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

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.

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

Projects With jar FilesSome 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.

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

AP Exam AlertThe 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.

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

Files Used by the MBCSBoundedEnv.javaDarterFish.javaEnvironment.javaFish.javambsbb.jarmbsgui.jarMBSGUI1.javaMBSGUI2.javaMBSGUI3.javaSimulation.javaSlowFish.javaUnboundedEnv.java

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

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

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

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

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

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

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

New Project AlertA 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.

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

Compiling MBCS – Step 4Click 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

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

Compiling MBCS – Step 5Repeat 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.

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

Compiling MBCS – Step 6Try 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.

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

Compiling MBCS – Step 7You 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.

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

Compiling MBCS – Step 8You 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.

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

Compiling MBCS – Step 9Navigate to the Java1507 folder.<Ctrl-Click> the two .jar files.Click the Open button and the OK button.

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

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

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

Compiling MBCS – Step 11Click 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.

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

Compiling MBCS – Step 12Make 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.

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

Compiling MBCS – Step 13Your first reaction to the MBCS execution may be Now what?

Click File followed by Create new environment...

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

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

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

Compiling MBCS – Step 15Place 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.

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

Another Look at CommentsYou 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.

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

Using JavadocJavadoc 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. **/

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

/** * 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.

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

Using Javadoc – Step 1Get 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.

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

Using Javadoc – Step 2You need to create access to the Javadoc program with a path command.

Type path C:\Java\bin

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

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

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