Custom-made Java Objects
The goal of this lab is to give you practice with:
- creating your own java classes and objects
- defining instance variables and instance methods, and testing them
- lists of objects
- practice writing loops
- reading from a file
In particular, we will define a class named Vehicle
to represent information
about vehicles. There are no code files to download; you will be writing all your java code today from scratch.
Part I: Vehicle class
We will define a Java class, named Vehicle, that will allow us to store information
about a single vehicle. For this application, we are interested in the following
information, related to a vehicle instance:
- the type of the vehicle: SUV, minivan or utility car
- the number of passengers the vehicle can fit (ranges from 1-10)
- the color of the car
Instance Variables
What instance variables do we need to have in the Vehicle class? Make all your instance variable
private and of type String.
Constructor
The job of a constructor is to give initial values to the instance variables of a class.
Here is an example of how this constructor will be called:
Vehicle v1 = new Vehicle("minivan", "10", "red");
Testing your code: main()
Add a main()
method to your Vehicle.java file, so you can test your code as
you develop it. As always, you should test your code incrementally, as you
add it to the Vehicle.java
file.
You can test your code by creating vehicles within your main method, e.g.
Vehicle bigCar = new Vehicle("minivan", "10", "white");
You should test the constructor by creating a couple of
vehicle instances.
Instance Methods
- We always recommend that you implement the
toString()
instance method, which returns a String representation of the invoking object instance. This method is very useful for testing purposes. We recommend that you implement thetoString()
method every time you define a class! For example, here is a printed representation of an instance of the Vehicle class:Vehicle: minivan, sits 10, Color red.
The above line was generated by this line in the main method:
System.out.println(bigCar.toString());
Since our instance variables are private to this class, we, as implementors, need to provide "getter" methods so information about a particular instance of the class can be accessed from outside the class. For now, we will define the following public instance method:
getType()
, which returns the type of the invoking Vehicle instance- As time permits, you can come back to it later, to provide getters for the rest of the instance variables.
In your main()
method, test the methods
you have defined so far on a couple of instances of the
Vehicle
class.
Part II: VehiclesCollection class
In order to write the code for this part, you'll need to download the lab10_programs
folder in the cs111 download directory.
The purpose of this class is to keep track of a collection of objects of type Vehicle
.
Instance Variables
In this implementation we will use a list to hold the Vehicle instances. In particular,
we will use the
ObjectList
implementation of a list.
Add the following two instance variables to your class:
ObjectList collection; //linked list to hold the Vehicle instances
int size; //number of Vehicles in the list, at any point
Constructor
The constructor creates the instance variable object list, collection
, initially to be empty.
Then, it fills it in by reading data from a file, named vehicles.txt
. This text file contains
data about a few Vehicles, one piece of datum per line. Here is how you can read from this file, line by line,
create Vehicle instances and add them to the collection:
Scanner scan = new Scanner(new File("vehicles.txt"));
while (scan.hasNext()) {
String type = scan.nextLine();
String passengers = scan.nextLine();
String color = scan.nextLine();
Vehicle v = new Vehicle(type, passengers, color);
collection = ObjectList.prepend(v, collection);
size++;
}
scan.close();
Other methods to define
- Define a
toString()
method, that prints out the whole collection of Vehicle instances. The code will be much simpler, if you use thetoString()
method in the ObjectList class. - Add a
main()
method to your VehiclesCollection.java file, so you can test your code as you develop it. As always, you should test your code incrementally, as you add it. At this point, create an object of typeVehiclesCollection
, and add a couple of Vehicles objects. Try to print out your collection of vehicles. -
Add an instance method,
findCarsByType()
, which will take as a paramenter aString type
, and will print out all the instances in the collection that have that type. Here is some sample output:Printing minivans: Vehicle: minivan - sits 8 - Color blue Vehicle: minivan - sits 4 - Color green Vehicle: minivan - sits 10 - Color red
-
Add an instance method,
findCarsByColor()
, which will take as a paramenter aString type
, and will print out all the instances in the collection that have that type. Here is some sample output:Printing red vehicles: Vehicle: SUV - sits 5 - Color red Vehicle: minivan - sits 10 - Color red