C211 Problem Solving and Programming II

C211 Homework 1

Due Date: Wednesday, October 30, 2024.

Make sure to complete Lab 1 before doing the homework.

In this homework, you will create a class storing vehicle information and a few methods to manipulate it.

Ex. 1. Vehicle Class

Create a Java project in Eclipse or in NetBeans called hw1 and a class called Vehicle in it containing the main method. Add your name at the top as a comment, along with the class name and date.

a. Attributes

Add instance attributes to the class to store the following information:

Also add a class attribute (static) called scan of type Scanner, initialized from System.in. Check the lab for more details.

b. Constructors

Create a constructor for the class with parameters for all 4 attributes. In the constructor, assign the values to the respective attributes. Also create a default constructor that takes no parameters and that initialized the number attributes to 0 and the string one to an empty string.

c. Input/Output

Write a couple of public methods of type void with no parameters for input and output. In the input, ask the user for values for each attribute and input them using the static scanner. For the output, print out all the values of the attributes.

d. Comparison

Write two methods to compare two vehicles: isBigger and isNewer. Both of them should be public methods of type boolean. Both of them should take one parameter of class Vehicle and compare it to the target object. For the first one, if the target object has more wheels than the parameter, it should return true, otherwise false. For the second one, if the target object's year is larger than the parameter's year, then return true, otherwise return false.

Remember that the attributes of the target object can be accessed with their name only or with this.year and so on.

e. Array of Vehicles

Write a static method in the class Vehicle called outputArraythat accepts an array of objects of this class as parameter. The method should first print a statement saying how many vehicles are in the array (use the length of the array). Then it should go through this array with a simple for loop and for each element in the array, it should call the method output that you defined before. You can either use the special array loop, or a for loop with the length of the array as the upper bound.

Hint. Please check the arrays ppt presentation in Module 1 on Canvas for examples of declaring arrays (slide 4) and using them in a function as parameters (slide 16), as well as declaring and using them for a custom class (slides 19 to 22).

f. Test

In the main, declare an array of type Vehicle and assign to it a new array with a size of 3. Remember that you also need to initialize all of its elements, otherwise they will be null. For the first element, assign to it a new vehicle object using the default constructor. For the second one, assign to it a new vehicle using the constructor with parameters and enter any values you want for them. For the third one, also use the default constructor. After that, call the input function on the third element of the array.

At this point, the content of the array is set. Call the function outputArray to display the content of all 3 objects. Then call the methods isBigger and isNewer from the first element with the second one as parameter, and output an appropriate message based on whether the methods return true or false. This is the end of the homework.

Homework Submission

Upload the files Date.java and Vehicle.java to Canvas, Assignments, Homework 1.