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 need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...
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...
The Bashemin Parking Garage contains a single lane that can hold up to ten cars. Arriving cars enter the garage at the rear and are parked in the empty space nearest to the front. Departing cars exit only from the front. If a customer needs to pick up a car that is not nearest to the exit, then all cars blocking its path are moved out temporarily, the customer's car is driven out, and the other cars are restored in...
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...
PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class definition. * * @author David Brown * @version 2019-01-22 */ public class Movie implements Comparable { // Constants public static final int FIRST_YEAR = 1888; public static final String[] GENRES = { "science fiction", "fantasy", "drama", "romance", "comedy", "zombie", "action", "historical", "horror", "war" }; public static final int MAX_RATING = 10; public static final int MIN_RATING = 0; /** * Converts a string of...