©2010 Felleisen, Proulx, et. al.
Starting with this lab we will use the standard Java language. Of course, we only know a small part of the language. We will learn new features when they are needed to support our program design process.
Standard Java Project differs very little from the projects we have built so far. The main difference is that standard Java expects you to define every class and every interface in a separate file whose name is the name of the class or interface, followed by .java. So, if our project contains classes Book, the class Author, and the class ExamplesBooks, we will need to define these classes in files Book.java, Author.java, and ExamplesBooks.java. Typically, each Project contains all files that are used to solve one problem.
The first new feature of the standard Java we need to introduce is the use of visibility modifiers. In Java every class, interface, field, method declaration, and method definition in Java typically starts with one of the words public, private, or protected. The fields and methods declared to be public can be accessed and are visible to all other classes — the way we have been using the fields and methods in FunJava. Fields or methods declared to be private can only be accessed within the class in which they are defined. So, for example, if we need a helper method that is not relevant for anyone using our class, we would make this a private method. We will have example of the use of the private visibility modifiers over the next couple of weeks.
If the visibility modifier is omitted, as we have done, the methods and fields can be used by any other classes within the same package. In our projects, all classes are defined in the default package, and so we only need to add the visibility modifiers when it serves a specific purpose:
When a class implements an interface which includes method declarations, every method definition in the class that implements a method declared in the interface must be annotated with the public visibility modifier. This is because defining a private method in an interface would be meaningless.
If a class (possibly abstract) defines a method, the class that extends it cannot reduce the visibility of this method. If the super class defines the method as public, the subclass must also define it as public.
We will worry about the protected visibility modifiers later.
Create a new Project in Eclipse, name it Date.
Right click on the src block under Date in the Pacakage Explorer pane. Select New then File in the File menu name your file Date.java.
Copy the following data definition into your Date.java file and save the file:
// to represent a calendar date class Date { int year; int month; int day; Date(int year, int month, int day){ this.year = year; this.month = month; this.day = day; } }
Create a new file ExamplesDates.java while the default package block (under the src block )is highlighted. This is where you will define the examples and tests for the Date class.
Define the default constructor for the class ExamplesDates:
ExamplesDates(){}
Define in the ExamplesDates class three examples of valid dates.
Import tester.jar as External Jar, as we have done before.
Highlight Date project in the Package Explorer pane.
In the Run menu select Run Configurations....
In the top left corner of the inner pane click on the leftmost item. When you mouse over it should show New launch configuration.
Select the name for this configuration - usually the same as the name of your project.
In the Main class: click on Search....
Among Matching items select Main - tester and hit OK.
Select the Arguments tab and type in the name of your Examples class in double quotes. For this example it would be "ExamplesDates". Notice, this is the name of the class, not the name of the file.
At the bottom of the Run Configurations select Apply then Run.
Next time you want to run the same project, make sure Date.java is shown in the main pane, then hit the green circle with the white triangle on the top left side of the main menu.
You can create an archive of your project by highlighting the project, then choose Export then select Archive File. Eclipse will ask you for a folder where to place the zip file and will let you choose the name for the zip file.
Your project will remain in the Eclipse workspace, but now you have saved a copy that will not change as you keep working.
This is also the file that you will be submitting as your homework.