Question

PLease, I need help with this Code. PLease create correctly documentations comments for better understanding.

Programming Assignment #2 (Arrays,) 2. The Car class will have private instance variables that atore the license number and number of timea the car haa been moved, and any methods you d1acover to be neccaaary 3. To get credit, your Garage class mast usc an array (not an ArzayList) of C objects to implement the garage. No credit ill be given if you ak the aaaignment I. The Aasignment the B55aSBAA. Parking Garage contains a 51ngle 1ane that an hold up to ten cara. Ariving cars enter the garage at the rear and are parked in the enpty apace neareat to the front Departing cara exit only from the front unneceaaarily more difficult by uaing more than one array 4. Use 츠 defined con tant to set the 5ize of the array. Other than in the constant declaration, the literal 10 ahould not appear in your program If a cu8tomer needs to pick up a car that is not neare t to the exit, then all cora blocking 1ts path are moved out temporarily, the customer cars are estored in the ordez they were in originally Whenever a car departs, all cars behind it in the garage are moved 5. To handle the arrival and departure of cach car, your Garage claas must implement separate methods aive) aned depart each of which retur a String ahowing the reault , 츠r î driven out 츠nd the other r of the operation (aee above) 6. Your garage elas may centain thethod should you find izīte 즈 Java program to perate the garage them necessary or advisable 7. Alao write a tet claas that reada the operationa from the input file, echoes each to the output file, calls the appropziate Garage clas method, and writes the String The program will read and proce55 lines of input from f11e until end-of-file. E ch input line contain욥 a license plate number and an operation (ARRIVE o DEPARTseparated by ара се 3 . Cara arrive and depart In the ord input output file, along with an appropriate message showing the atatus of the operation returned to the output file er apecified by the Each input operation must be echo printed to n Astated in . and 6. above, all output 1 to be done in the test class None of the Garage or Ca class methods do any output 9. Make ure your class adhere to th atyle and ฝึhen a car 乜エrives, the messige will include the licens number and tate whether the car is being parked or turned away because the garage 13 full. If the garage 1a Eull, the car leaves without eve having entered the arage 10. No eredit will be given if you resize the garage array at time. It must be created with a capaci changC Hint: See PartialiyriIed. iava for an excellent examole ohow to use a counter to keep track of the number of When a car departs, the message will include the license number and the number of timea the car as moved class, thi.is how the £122() method of the Acょ52融镻 class ia implemented) rhe number of moves does not include the one where the car depar ts from the gara or the number of times the car It i8 an gara 11. or full credit, do not use Sa.5R to move the cars within the garage . Use a loop. (Mo credit will be given if times it wa3m out of the to allOw you uac ax.FOEYRE - aec above) car behind it to depart If a DEPARI operation calla for a car that 1a not in the garage, the 12. As with the previous assigment, thi ne will generate to acparate gradcaone for the program itaelf and the other for the html files generated by axades

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 DEPART

This is what I have so far:



public class Car

{

private String licenceNo;

private int countOfMove;

public String getLicenceNo() {

return licenceNo;

}

public void setLicenceNo(String licenceNo) {

this.licenceNo = licenceNo;

}

public int getCountOfMove() {

return countOfMove;

}

public void setCountOfMove() {

this.countOfMove;

}

}

************************************************************************

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// Car.java (modified)

public class Car

{

      private String licenceNo;

      private int countOfMove;

      public String getLicenceNo() {

            return licenceNo;

      }

      public void setLicenceNo(String licenceNo) {

            this.licenceNo = licenceNo;

      }

      public int getCountOfMove() {

            return countOfMove;

      }

      /**

      * method to increment the number of moves car moved

      */

      public void incrementCountOfMove() {

            this.countOfMove++;

      }

}

// Garage.java

public class Garage {

      // defining attributes

      private static final int SIZE = 10;

      private Car cars[];

      private int count;

      /**

      * default constructor

      */

      public Garage() {

            // initializing array

            cars = new Car[SIZE];

            // setting current number of cars to 0

            count = 0;

      }

      /**

      * method to perform the arrival operation of a car

      *

      * @param licenseNumber

      *            license number of the car

      * @return the result of operation, in a String format

      */

      public String arrival(String licenseNumber) {

            if (count == cars.length) {

                  // garage is full

                  return "Garage is full, " + licenseNumber + " is not parked";

            }

            // creating a new car

            Car newCar = new Car();

            newCar.setLicenceNo(licenseNumber);

            // adding to the end and updating count

            cars[count] = newCar;

            count++;

            // return success message

            return licenseNumber + " parked in the Garage";

      }

      /**

      * method to perform the departure operation of a car

      *

      * @param licenseNumber

      *            license number of the car

      * @return the result of operation, in a String format

      */

      public String depart(String licenseNumber) {

            // creating a car

            Car c = new Car();

            c.setLicenceNo(licenseNumber);

            // finding the index of car in the array

            int index = indexOf(c);

            if (index == -1) {

                  // car not found

                  return licenseNumber + " is not found in the Garage";

            }

            /**

            * simulating the movement of cars before the specified car out of the

            * garage

            */

            for (int i = 0; i < index; i++) {

                  // incrementing move count

                  cars[i].incrementCountOfMove();

            }

            // storing the car to be removed

            Car removed = cars[index];

            // shifting the remaining cars to occupy the vacant space in array

            for (int i = index; i < count - 1; i++) {

                  cars[i] = cars[i + 1];

            }

            count--; // decreasing the count

            // returning removed car stats

            return removed.getLicenceNo() + " is departed from Garage,"

                        + " number of moves made by this car: "

                        + removed.getCountOfMove();

      }

      /**

      * a private helper method to find the index of a car c in the cars array

      *

      * @param c

      *            - Car object

      * @return index if found, -1 if not found

      */

      private int indexOf(Car c) {

            for (int i = 0; i < count; i++) {

                  if (cars[i].getLicenceNo().equals(c.getLicenceNo())) {

                        return i;

                  }

            }

            return -1;

      }

}

// Test.java

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

public class Test {

      public static void main(String[] args) throws FileNotFoundException {

            // make sure you have input.txt file in your directory

            Scanner scanner = new Scanner(new File("input.txt"));

            // creating a Garage

            Garage garage = new Garage();

            // creating a PrintWriter for writing to output file

            PrintWriter writer = new PrintWriter(new File("output.txt"));

            String result;

            // looping through the file

            while (scanner.hasNext()) {

                  // getting attributes

                  String license = scanner.next();

                  String operation = scanner.next();

                  // appending to output file

                  writer.println(license + " " + operation);

                  // performing operations and appending results to output file

                  if (operation.equalsIgnoreCase("DEPART")) {

                        result = garage.depart(license);

                        writer.println(result);

                  } else {

                        result = garage.arrival(license);

                        writer.println(result);

                  }

            }

            writer.close(); //important to close the writer to save the file

            System.out.println("output has been saved to output.txt file");

      }

}

/*output.txt*/

JAV001 ARRIVE

JAV001 parked in the Garage

JAV002 ARRIVE

JAV002 parked in the Garage

JAV003 ARRIVE

JAV003 parked in the Garage

JAV004 ARRIVE

JAV004 parked in the Garage

JAV005 ARRIVE

JAV005 parked in the Garage

JAV001 DEPART

JAV001 is departed from Garage, number of moves made by this car: 0

JAV004 DEPART

JAV004 is departed from Garage, number of moves made by this car: 0

JAV006 ARRIVE

JAV006 parked in the Garage

JAV007 ARRIVE

JAV007 parked in the Garage

JAV008 ARRIVE

JAV008 parked in the Garage

JAV009 ARRIVE

JAV009 parked in the Garage

JAV010 ARRIVE

JAV010 parked in the Garage

JAV011 ARRIVE

JAV011 parked in the Garage

JAV012 ARRIVE

JAV012 parked in the Garage

JAV013 ARRIVE

Garage is full, JAV013 is not parked

JAV014 ARRIVE

Garage is full, JAV014 is not parked

JAV006 DEPART

JAV006 is departed from Garage, number of moves made by this car: 0

JAV014 DEPART

JAV014 is not found in the Garage

JAV013 DEPART

JAV013 is not found in the Garage

JAV005 DEPART

JAV005 is departed from Garage, number of moves made by this car: 1

JAV015 ARRIVE

JAV015 parked in the Garage

JAV010 DEPART

JAV010 is departed from Garage, number of moves made by this car: 0

JAV002 DEPART

JAV002 is departed from Garage, number of moves made by this car: 4

JAV015 DEPART

JAV015 is departed from Garage, number of moves made by this car: 0

JAV014 DEPART

JAV014 is not found in the Garage

JAV009 DEPART

JAV009 is departed from Garage, number of moves made by this car: 2

JAV003 DEPART

JAV003 is departed from Garage, number of moves made by this car: 6

JAV008 DEPART

JAV008 is departed from Garage, number of moves made by this car: 3

JAV007 DEPART

JAV007 is departed from Garage, number of moves made by this car: 4

JAV012 DEPART

JAV012 is departed from Garage, number of moves made by this car: 1

JAV011 DEPART

JAV011 is departed from Garage, number of moves made by this car: 2

Add a comment
Know the answer?
Add Answer to:
PLease, I need help with this Code. PLease create correctly documentations comments for better understanding. This...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The CSC326 parking garage contains 2 lanes, each capable of holding up to 10 cars. There...

    The CSC326 parking garage contains 2 lanes, each capable of holding up to 10 cars. There is only a single entrace/exit to the garage at one end of the lanes. If a customer arrives to pick up a car which is not nearest the exit, all cars blocking the car's path are moved into the other lane. If more cars still must be moved out of the way, they go into the street. When the customer's car is driven out,...

  • The laughs parking garage contains a single lane that hold up to ten cars. Cars arrive...

    The laughs parking garage contains a single lane that hold up to ten cars. Cars arrive at the south end of the garage and leave from the north end. If a customer arrives to pick up a car that is not northernmost, all the cars to the north of his car are moved out, his car is driven out, and the others cars are restored in the same order that they were in originally. Whenever a car leaves, all the...

  • The Parking Garage Problem

    The Scratchemup parking garage contains a single lane that holds up to 10 cars. Cars arrive at the south end of the garage and leave from the north end. If a customerarrives to pick up a car that is not the northernmost, all cars to the north of his car are moved out, her car is driven out, and the other cars are restored in thesame order that they were in originally. Whenever a car leaves, all cars to the...

  • Programming Assignment #2 (Arrays) I. The Assignment This assignment is to take your Garage class from...

    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...

  • *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J...

    *** 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...

  • please need help with the java program A micro car rental company. package yourLastName.cis4110.assignment5; Please create...

    please need help with the java program A micro car rental company. package yourLastName.cis4110.assignment5; Please create an enum type, say Cartype, featuring all rental car types -- SUBCOMPACT, COMPACT, MIDSIZE, FULLSIZE (You may put the enum definition in Car class) class Car Instance variables: - private String plateNum - private Cartype carType - private boolean availability Member methods:             - Please define all setters and getters class RentACar - This is a five car system for prototyping instance variables:...

  • Need to fill the comments with code that works. Create The Starting Template for this program...

    Need to fill the comments with code that works. Create The Starting Template for this program is shown below. Fill in code where you see highlighted in these highlighted sections that do what the comments request. Don't modify maint) or the printCounts0 routine. When you run your program, you will need to eater in a string for the 'readString" method. Use a package named string methods for this program ructions in the methods readString), countOccurrences0.. ete. Write code the string:...

  • I need an OUTLINE ONLY (pseudocode/comments). DO NOT DO THE PROGRAMMING ASSIGNMENT. Part I: PA3 Outline...

    I need an OUTLINE ONLY (pseudocode/comments). DO NOT DO THE PROGRAMMING ASSIGNMENT. Part I: PA3 Outline (10 points). Create an outline in comments/psuedocode for the programming assignment below. Place your comments in the appropriate files: main.cpp, functions.h, dealer.cpp, dealer.h, dealer.cpp (as noted below). Place into a file folder named LastnamePA3, the zip the content and hand in a zip file to Canvas. Part II: PA3: Car Dealership (40 points) For Programming Assignment 3 you will be creating a program to...

  • I need help with this java project and please follow the instruction below. thank Class Project...

    I need help with this java project and please follow the instruction below. thank Class Project - Parking Ticket simulator Design a set of classes that work together to simulate a police officer issuing a parking ticket. Design the following classes: •           The ParkedCar Class: This class should simulate a parked car. The class’s responsibilities are as follows: – To know the car’s make, model, color, license number, and the number of minutes that the car has been parked. •          ...

  • Use BlueJ to write a program that reads a sequence of data for several car objects...

    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 for any number of cars. (You should not assume that it will always be seven lines in the input file). Use notepad to create input file "inData.txt". File should be stored in the same folder where all files from BlueJ for this program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT