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:
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, here’s another chance.
V. What to Upload to Canvas
Upload 2 files via the Programming Assignment 2link
Upload another empty Word doc via the Javadoclink
Make sure you zip the project folder itself, and not the individual java files.
_______________________________________________________________________________________
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<Car> Garage;
/**
* 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;
}
}
Thanks for posting the question, we are glad to help you. Here is the updated code where we have used a Car array of size 10 in place of using ArrayList of Cars.
I don't have the Car.java file implementation hence I could not test the program. If you face any problem please post your error I'll be happy to assist you.
___________________________________________________________________________________________________
/** * 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 Car[] garage; private int carCount; /** * Constructor to initialize empty array list. */ public Garage() { // setting an array size of 10 garage = new Car[10]; carCount = 0; } /** * 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. */ // Add a car to list if list < 10 public boolean arrive(Car number) { if (carCount < 10) { garage[carCount] = number; carCount++; 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 < carCount; i++) { String matchName = garage[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[i].addMove(); } justLeft = garage[position]; carCount--; 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; } }
Programming Assignment #2 (Arrays) I. The Assignment This assignment is to take your Garage class from...
I am currently using eclipse to write in java.
A snapshot of the output would be greatly appreciated to verify
that the program is indeed working. Thanks in advance for both your
time and effort.
Here is the previous exercise code:
/////////////////////////////////////////////////////Main
/*******************************************
* Week 5 lab - exercise 1 and exercise 2: *
* ArrayList class with search algorithms *
********************************************/
import java.util.*;
/**
* Class to test sequential search, sorted search, and binary search
algorithms
* implemented in...
/** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; } /** * Create a String that...
Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...
*** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J IN CLASS (IF IT DOESNT MATTER PLEASE COMMENT TELLING WHICH PROGRAM YOU USED TO WRITE THE CODE, PREFERRED IS BLUE J!!)*** ArrayList of Objects and Input File Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info...
Below are the Car class and Dealership class that I
created In the previous lab
import java.util.ArrayList;
class Car
{
private String make;
private String model;
private int year;
private double transmission;
private int seats;
private int maxSpeed;
private int wheels;
private String type;
public Car()
{
}
public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) {
this.make = make;
this.model = model;
this.year = year;
this.transmission = transmission;
this.seats =...
Task #3 Arrays of Objects 1. Copy the files Song java (see Code Listing 7.1), Compact Disc.java (see Code Listing 7.2) and Classics.txt (see Code Listing 7.3) from the Student Files or as directed by your instructor. Song.java is complete and will not be edited. Classics.txt is the data file that will be used by Compact Disc.java, the file you will be editing. 2. In Compact Disc.java, there are comments indicating where the missing code is to be placed. Declare...
In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...
JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet { /** * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList * containing all the indexes of Strings in arr that match String s. For this method, two Strings, * p and q, match when p.equals(q) returns true or if both of the compared references are null. * * @param s The string...
Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...
PLease, I need help with this Code. PLease create correctly
documentations comments for better understanding.
This is the input:
JAV001 ARRIVE
JAV002 ARRIVE
JAV003 ARRIVE
JAV004 ARRIVE
JAV005 ARRIVE
JAV001 DEPART
JAV004 DEPART
JAV006 ARRIVE
JAV007 ARRIVE
JAV008 ARRIVE
JAV009 ARRIVE
JAV010 ARRIVE
JAV011 ARRIVE
JAV012 ARRIVE
JAV013 ARRIVE
JAV014 ARRIVE
JAV006 DEPART
JAV014 DEPART
JAV013 DEPART
JAV005 DEPART
JAV015 ARRIVE
JAV010 DEPART
JAV002 DEPART
JAV015 DEPART
JAV014 DEPART
JAV009 DEPART
JAV003 DEPART
JAV008 DEPART
JAV007 DEPART
JAV012 DEPART
JAV011...