JAVA
Module 1 Labs
DR. CATHERINE ANDERSON MCNEESE STATE UNIVERSITY
CSCI 282 OBJECT ORIENTED PROGRAMMING
CHAPTER 1
Shipping Fleet
Practicing inheritance with the super- class Ship and another class called Fleet
SHIPPING FLEET
1. Read data from Arrays - create subclasses
2. Read from File - populate fleet from data in file
SECTION 1
Problem Description You are to use the Ship class you created in last weeks lab to create three subclasses. In the bundle of files that accompa- nies this lab, there is a Fleet Class. The Fleet Class requires these three sub classes inherited from the Ship class: specifi- cally the CruiseShip, the CargoShip and the BattleShip. These sub classes will use all methods from the super class in addition to new attributes and methods unique to the specific class.
The additional attribute per subclass are as follows:
• The Cargo Ship subclass has both maximum cargo weight and freezer space.
• The Cruise ship subclass has the number of state rooms (each berthing two passengers), number of double rooms (each berthing 2 passengers) and number of economy rooms (each berthing a single passenger).
• The battle ship subclass has weapon capacity and troop carrying capacity.
What are the names of these attributes? Well you know from the convention of writing getters which is to name the method getVariableName(), where variable name is the name of the attribute. So if you know the name of the getter, you know what the attribute should be called. In addition, in the file Fleet.java, you will see that both setters and getters for these subclasses are being used, so you know what the attrib- utes name should be.
2
There is no code-along for this lab. There will still be code- alongs in future labs, but fewer of them. I have provided you with a file bundle that contains the following file
For part 1: • Ship.java ( my implementation of last weeks class) • fleet.java
For part 2: • ship_info.java • fleet2.java
Part 1: Creating the subclasses
Your task for the first part of this exercise, is to write the sub classes with all the methods needed to run the Fleet class with- out changing anything in the Fleet class. In fact you, MAY NOT CHANGE ANY CODE within the Fleet class. Looking at the method calls in the Fleet class will also tell you the names of setters and getters methods. The data needed to in- stantiate these classes is hard coded in to the Fleet class.
Remember the OOP principle that all class attributes should be private and as such need accessor and mutator methods.
Remember the convention of writing an accessor and muta- tor method. Use the code written in the Fleet class for method names and parameter lists. Also, you are require to adhere to the setter and getter convention of having the attribute name reflected in the setter or getter.
When you have completed the subclasses, you can compile and run the executable Fleet class. You should get the a dis- play identical to the one to the right, without changing any code within the fleet class. The only differences might be if you are running your program on Windows. Then the display will have the MSWindows look-and-feel as opposed to the Mac look-and-feel as shown here. This first part it to make sure you subclasses are properly implemented.
3
Part 2 - More File IO:
In the bundle of source code that accompanies this lab, you will also find a class called Fleet2. This class requires the data to be entered from a selected file. The file name selection and the display code has been provided for you. You need to pro- vide the code that will load the data in from file. It is recom- mended that you use the scanner class.
After you have success- fully loaded the data. you should see the dis- play to the right when you run the Fleet2 class. Now, to demon- strate that you under- stand what is going on in the Fleet2 code, you are to insert your name and the name of any team member be- tween the list of ships and the beginning the the cargo fleet capacity and shown in red to the right. There should be a blank line both above and below your names.
Criteria for evaluation:
Your program will be evaluate by the criteria below. This is part of the specification of the problem and should be read, es- pecially when discussing project and class names. If you do not understand a criterion, it is YOUR responsibility to ask questions until you do understand!
4
CHAPTER 2
GUI Temperature converter
First GUI app with interaction between user and computer.
FIRST SIMPLE GUI APP
1. Initializing window components
2. Setting up the listeners
3. Create response to events
SECTION 1
Your first interactive GUI This lab will require that you implement a very simply GUI app. One that is so simple in design that it can all be done in in a single class, which will supply the window, the compo- nents and the logic to do the conversion.
To get you started, there will be a code-along that will take you through the GUI display of this Lab. Then you will be re- sponsible for supplying the methods that make the window in- teractive which include the helper function as shown on the UML. This will give you a bit of practice on exchanging infor- mation between components.
You will notice in my on-going code-alongs and in the tutori- als that I give you to work through, I favor adding listeners anonymously, as opposed to allowing the class to implement the needed listener. I find this clearer when reading the code and less prone to error due to misunderstanding the code. You will also notice in future classes that IDEs that automati- cally generate code also add listeners anonymously.
I strongly recommend that in your future programs, you also adopt this approach. However, in this lab, there is no choice, it is required, just so that you have gone through it once. In the future, you will be given the choice. And it is recom- mended that you do not mix the techniques, again for in- creased readability.
Lab code-along:
The code-along will create the window of the very simple GUI app that is pictured on the cover of this chapter. The UML for
6
this class is shown to the left. You should notice that the attrib- utes are private. As is expected from the OOP programmings. This is simplify you first use of a GUI interface.
It will be up to you to make the app interactive by implementing the methods of checkValid, processTemp and clear. The code-along will take you through
the implementation of the other methods. All methods should have the same return value that is shown on the UML. And Yes, I know there are many ways that this can be done. What is also being practiced in these labs is programming to Spec. The spec. or specification is the UML.
Once your implementation is done, you should run the follow- ing test career and post screen shots of your results in the M3- Lab2 lab sheet.
Test cases:
Fahrenheit Temperature 1: 32
Fahrenheit Temperature 2: 100
Fahrenheit Temperature 3: -21
7