Question
Programming Assignment #2 (Arrays) I. The Assignment This assignment is to take your Garage class from the previous assignment and modify it so that it
Programming Assignment #2
(Arrays)
I. The Assignment
This assignment is to take your Garage class from the previous assignment and modify it so that it uses an array of Car objects as the principal data structure instead of an ArrayList-of-Car.
This is an example of the OOP principle of information hiding as your Car and test classes will not have to be modified at all. Unless you broke encapsulationon the previous assignment, that is
II. Specifications
Specifications for all 3 classes are the same as for the previous version, except as noted here:
- To get credit, your Garage class must use an array (not an ArrayList) of Car objects to implement the garage
- Use a defined constant to set the size of the array (i.e. number of spaces in the garage). The literal 10 should not appear anywhere else in your program
- No credit will be given if any data structures other than the array of Car objects are used
- No credit will be given if you use Arrays.copyOfor System.arraycopy, or resize the garage array in any other way.
Hint: Uas a counterto keep track of the number of Cars in the garage.
Dude, why the restrictions?
Answer: To earn a meaningful merit badge in arrays, we must use only those techniques that will work for arrays in everylanguage in this quadrant of the galaxy. Loops.
Make sure your classes adhere to the style and documentation standards discussed in class and online notes
III. Due Date: Tuesday, February 5th
IV. Grading
As per the previous assignment, this one will generate two (2) separate grades one for the program itself and the other for the html files generated by Javadoc.
If you aced the Javadoc grade on the previous one, no need to do anything else. If you did not, heres another chance.
V. What to Upload to Canvas
Upload 2 files via the Programming Assignment 2link
- a zip file containing your NetBeans project folder with the output file included
- an empty Word document to receive your feedback
Upload another empty Word doc via the Javadoclink
Make sure you zip the project folder itself, and not the individual java files.
- See Submitting Your Assignments online to make sure you dont lose credit!
- For reference, see PartiallyFilled.javaand OrderedList.javaonline in this unit
_______________________________________________________________________________________
THIS IS ASSIGNMENT 1 GARAGE CLASS:
package garage.project;
import java.util.ArrayList;
/**
* Class that contains an array list full of car objects. Contains methods to
* handle departure and arrival operations of the cars.
* @param justLeft saves the value of a car that just left.
*/
public class Garage {
private Car justLeft;
private ArrayList
/**
* Constructor to intialize empty array list.
*/
public Garage()
{
Garage = new ArrayList<>() ;
}
/**
* Adds car to the array if there is room and turns car away if not.
* @param number the plate numbers for the car
* @return true if car object is added to array and false if list is full.
*/
public boolean arrive(Car number) // Add a car to list if list < 10
{
if (Garage.size() < 10)
{
Garage.add(number);
return true;
}
return false;
}
/** Check for car in garage and remove it. Pass true if car was removed.
* Removed car object is assigned to the variable justLeft.
* @param name the plate number of the car.
* @return true if car is found in list but false if not.
*/
public boolean depart(String name)
{
int position = -1;
// Traverse array to check for car position.
for (int i = 0; i < Garage.size(); i++)
{
String matchName = Garage.get(i).getName(); //get plate number
if ( name.equals(matchName) ) //compare plate numbers
{
position = i; // Get index if car is found
}
}
// if car is in garage then increment moves for cars ahead of it.
// Remove car and add its values to justLeft variable.
if (position != -1)
{
for (int i=position - 1; i >= 0; i--)
{
Garage.get(i).addMove();
}
justLeft = Garage.remove(position);
return true;
}
return false;
}
/**
* Gets the value of the variable justLeft.
* @return justLeft the variable that contains the last car to leave.
*/
public Car getLeft()
{
return justLeft;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started