Question

/*hello everyone. my question is mostly related to the garagetest part of this assignment that i've...

/*hello everyone. my question is mostly related to the garagetest part of this assignment that i've been working on. i am not sure how to read the file's contents AND put them into variables/an array. should it be in an arraylist? or maybe a regular array? or no arrays at all? i am also not sure how to call the factory method from garagec. i'm thinking something along the lines of [Garage garage = new GarageC.getInstance("GarageC", numFloors, areaofEachFloor);] but i am not sure if that's correct. i will post the instructions for my project below for full context. any help is appreciated. thank you.*/

1.Create a Java project in Eclipse called "ParkingGarageSystem".

2.You will need to create 4 java classes in the “src” subfolder of the package.

a.The ABSTRACT class would be Garage (Eclipse will put this class in the file “Garage.java”). This is a base class for garages.

b.Next there would be one interface, ParkingSpot (Eclipse will put this class in the file “ParkingSpot.java”).

c.The third class would be GarageC (Eclipse will put this class in the file “GarageC.java”). This class will inherit properties from Garage class and implement ParkingSpot interface.

3.Add the course header with your name at the top of the file.

4.Write your Java codes.

5.Save your Java codes.

6.Run your Java codes.

Provided Resources:

A text file “cars.txt”. You can use this text file to test your code, however we will be grading your submission with a DIFFERENT text file. There are tabs(“\t”) between the words and numbers. The test case text file will be of same format of the given cars.txt file, only with different car names, width, and length.

The Problem:

In this problem we will try to learn and implement abstract class and interface. We will also learn to do the String and File operations. We will create 3 classes to maintain a parking system.

For the sake of simplicity, we will consider only three types of cars (irrespective of make and model): Small, Medium, Large.

Your job is to read the text file containing all the car information at a time, and determine:

(i)How occupied the garage is,

(ii)How many different types of cars are present in the garage,

(iii)If the garage is still OPEN or FULL.

From implementational point of view, here are certain points which could be helpful:

1.The base class (which is also abstract) Garage has the following features:

a.A private variable to take garage name

b.A constructor for setting the garage name

c.A getter method for garage name

d.An abstract method to calculate the total occupied area for a garage. Hint- the input argument is an Arraylist containing the areas of all the cars.

2.The ParkingSpot interface has the following features. Note that the methods will not be implemented within the interface:

a.A method specification which takes the width and length of a car and returns the type of the parking (Small/Medium/Large), needed for the car. The length range for those three types of cars are: Small (<=15 feet), Medium (> 15 but <=17), and Large (> 17).

b.A method which will return the area of a parking spot, taking width and length of the car as arguments. Take the width and length as double.

3.The class GarageC will inherit all the properties from Garage and implement the all properties from ParkingSpot. It has some extra features:

a.A private variable for number of floors. Type: Integer.

b.A private variable for each floor area. Type: Double.

c.A PRIVATE constructor that will initialize the instance variables of GarageC. It will take three arguments, garage name, number of floors, and each floor area.

d.You have to implement Single tone design pattern. The whole system cannot have more than one GarageC object. So, create a factory method that utilize the singleton design pattern and prevent to have more than one object of GarageC.

e.A getter method for number of floors variable.

f.A getter method for each floor area variable.

g.Properly implement the interface methods and abstract method.

4.Create a GarageTest class and it should contain the main method. In the main method,please take three input for GarageC: one for number of floors of Garage C (point 3.a), second for each floor area of Garage C (point 3.b), and lastly the filename, (for example “cars.txt” file). You should keep the file in appropriate directory so that your program can read it.

a.Please read the file using proper file operations.

b.Perform the proper string operations and get width and length for each car.

c.Determine parking spot’s type for each car.

d.Determine parking spot area for each car.

e.While adding the cars in the garage from the file, determine the total area occupied in Garage C after reading each car. Also, and print the information in the console like this (Car: truck1, parking type: Large, Area: 150, Total area occupied in Garage C: 150.66, Total vacant area in Garage C: 500)

f.Whenever, the Garage C becomes full, print it in the console “Garage C is full”and write the list of the car type and their count into a text file called “out.txt” (example: small: 3, medium: 2). To taste this part of the code, you need to provide a relatively smaller number of floor and area for each floor, so that the garage becomes full with the input data from the file.

Sample Input/Output Format:

Enter number of floor: 2

Enter area at each floor: 500

Enter input file name: cars.txt

Car: truck1, parking type: Large, Area: 150.66, Total area occupied in Garage C: 150, Total vacant area in Garage C: 850 //because 500*2 – 150

If at any point the total area occupied exceeds 1000, it should display full and write out.txt with the information as required in in 4.f above.

//cars.txt is below

Name Length Width

truck1 18.6 8.1

suv1 17.4 7.4

coupe1 14.8 5.4

mini1 14.1 5.0

sedan1 16.4 6.1

suv2 17.5 7.3

mini2 14.3 5.2

sedan2 16.5 6.2

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

package com.HomeworkLib1;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

//Car class
class Car {
   //Class fields
   String name;
   double length;
   double width;

   /**
   * @param name
   * @param length
   * @param width
   */
   //Constructor with argument
   public Car(String name, double length, double width) {
       super();
       this.name = name;
       this.length = length;
       this.width = width;
   }

}

public class GarageTest {

   public static void main(String[] args) throws IOException {
       // TODO Auto-generated method stub
       // Read input
       Scanner consoleInp = new Scanner(System.in);
       {
           // From user
           System.out.print("Enter number of floor:");
           int numberOfFloors = consoleInp.nextInt();
           System.out.print("Enter area at each floor:");
           int floorArea = consoleInp.nextInt();
           System.out.print("Enter input file name:");
           String fileName = consoleInp.next();
           // Define file reader
           Scanner fileReader = new Scanner(new File(fileName));
           // Create instance
           GarageC garage = GarageC.getInstance("GarageNew", numberOfFloors, floorArea);
           // Define list
           java.util.List<Car> carList = new ArrayList<>();
           // Read all lines from file
           while (fileReader.hasNextLine()) {
               String car = fileReader.nextLine();
               StringTokenizer st1 = new StringTokenizer(car, " ");
               String name = st1.nextToken();
               double length = Double.parseDouble(st1.nextToken());
               double width = Double.parseDouble(st1.nextToken());
               Car c = new Car(name, length, width);
               // Add to list
               carList.add(c);
           }
           // Find total area
           double totalArea = numberOfFloors * floorArea;
           double vacant = totalArea;
           int smallCount = 0;
           int mediumCount = 0;
           int largeCount = 0;
           int index = 0;
           // Define list to calculate total occupied space
           ArrayList<Double> spacelist = new ArrayList<>();
           // Loop thu all car
           for (Car c : carList) {
               double area = garage.getParkingArea(c.length, c.width);
               // Check if remainign space is >0
               if (vacant - area > 0) {
                   // Get type of car
                   String type = garage.getParkingType(c.length, c.width);
                   // increment corresponfing count
                   if (type.equals("Small"))
                       smallCount++;
                   else if (type.equals("Medium"))
                       mediumCount++;
                   else if (type.equals("Large"))
                       largeCount++;
                   // get car area

                   // Add to list
                   spacelist.add(area);
                   // Calculate totoal occupied area
                   double occupiedArea = garage.calculateTotalSpace(spacelist);
                   vacant -= area;
                   // Print
                   System.out.println("Car: " + c.name + ", parking type: " + type + ", Area: " + String.format("%.2f", area)
                           + ", Total area occupied in Garage C:" + String.format("%.2f", occupiedArea) + ", Total vacant area in Garage C:"
                           + String.format("%.2f", vacant));
               } else {
                   // Print full warning
                   System.out.println("Can not Park. Parking is Full");
                   String out = "";
                   // Print
                   if (smallCount != 0)
                       out += "small: " + smallCount;
                   if (mediumCount != 0)
                       out += " medium: " + mediumCount;
                   if (largeCount != 0)
                       out += " large:" + largeCount;
                   // Create file writter and write to file
                   BufferedWriter br = new BufferedWriter(new FileWriter(new File("out1.txt")));
                   br.write(out);
                   br.flush();
                   br.close();
                   return;
               }
           }

       }
   }

}


----------------
package com.HomeworkLib1;
import java.util.ArrayList;
//Garage abstract class
public abstract class Garage{
   //NAme variable
private String garageName;
//Constructor
public Garage(String garageName) {
this.garageName = garageName;
}
//Getter for name  
public String getGarageName() {
return garageName;
}
//SEtter for name
public void setGarageName(String garageName) {
this.garageName = garageName;
}
//Abstract method to calculate total space
public abstract double calculateTotalSpace(ArrayList<Double> cars);
}
-----------------------
/**
*
*/
package com.HomeworkLib2;

import java.util.List;

/**
* @author vinoth.sivakumar
*
*/
public class GarageC extends Garage implements ParkingSpot {
   //Private variable
   private int numberOfFloors;
   private double floorArea;
   //Singleton obj
   public static GarageC objGarage = null;
   //Constructor declaration
   private GarageC(String garageName, int numberOfFloors, double floorArea) {
       super(garageName);
       this.numberOfFloors = numberOfFloors;
       this.floorArea = floorArea;
       // TODO Auto-generated constructor stub
   }
   //Static method to return singleton object
   public static GarageC getInstance(String garageName, int numberOfFloors, double floorArea) {
       if (objGarage == null) {
           objGarage = new GarageC(garageName, numberOfFloors, floorArea);
           return objGarage;
       } else
           return objGarage;
   }
  
  
   //Getter methods
   /**
   * @return the numberOfFloors
   */
   public int getNumberOfFloors() {
       return numberOfFloors;
   }

   /**
   * @param numberOfFloors the numberOfFloors to set
   */
   //Setter method
   public void setNumberOfFloors(int numberOfFloors) {
       this.numberOfFloors = numberOfFloors;
   }

   /**
   * @return the floorArea
   */
   public double getFloorArea() {
       return floorArea;
   }

   /**
   * @param floorArea the floorArea to set
   */
   public void setFloorArea(double floorArea) {
       this.floorArea = floorArea;
   }

   @Override
   public String getParkingType(double lengthOfCar,double widthOfCar) {
       // TODO Auto-generated method stub
       //Check lenght and find parking typpeaccordingly
       if (lengthOfCar <= 15)
           return "Samll";
       else if (lengthOfCar > 15 && lengthOfCar <= 17)
           return "Medium";
       else if (lengthOfCar > 17)
           return "Large";
       return "";
   }
   //Override super class method and return parkign size
   @Override
   public double getParkingArea(double length, double width) {
       // TODO Auto-generated method stub
       return length * width;
   }

  
   @Override
   public double calculateTotalSpace(List<Double> carArea) {
       // TODO Auto-generated method stub
       int totalArea = 0;
       //Read thru all cars passed.
       //Access area and sum to total
       for (Double area : carArea) {
           totalArea += area;
       }
       return totalArea;
   }

}
---------------
package com.HomeworkLib1;
public interface ParkingSpot {
   //Both methid will be implemented in sub classes
   //Abstract method to get parking type.
public String getParkingType(double length, double width);
//Abstract method to claculate parking size
public double getParkingArea(double length, double width);
}

--------------
Enter number of floor:2
Enter area at each floor:500
Enter input file name:cars.txt
Car: truck1, parking type: Large, Area: 150.66, Total area occupied in Garage C:150.66, Total vacant area in Garage C:849.34
Car: suv1, parking type: Large, Area: 128.76, Total area occupied in Garage C:279.42, Total vacant area in Garage C:720.58
Car: coupe1, parking type: Small, Area: 79.92, Total area occupied in Garage C:359.34, Total vacant area in Garage C:640.66
Car: mini1, parking type: Small, Area: 70.50, Total area occupied in Garage C:429.84, Total vacant area in Garage C:570.16
Car: sedan1, parking type: Medium, Area: 100.04, Total area occupied in Garage C:529.88, Total vacant area in Garage C:470.12
Car: suv2, parking type: Large, Area: 127.75, Total area occupied in Garage C:657.63, Total vacant area in Garage C:342.37
Car: mini2, parking type: Small, Area: 74.36, Total area occupied in Garage C:731.99, Total vacant area in Garage C:268.01
Car: sedan2, parking type: Medium, Area: 102.30, Total area occupied in Garage C:834.29, Total vacant area in Garage C:165.71
Car: sedan3, parking type: Medium, Area: 102.30, Total area occupied in Garage C:936.59, Total vacant area in Garage C:63.41
Can not Park. Parking is Full

------------------

Please comment if any clarification or modification needed.

Add a comment
Know the answer?
Add Answer to:
/*hello everyone. my question is mostly related to the garagetest part of this assignment that i've...
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
  • You are given a specification for some Java classes as follows.   A building has a number...

    You are given a specification for some Java classes as follows.   A building has a number of floors, and a number of windows. A house is a building. A garage is a building. (This isn’t Florida-like … it’s a detached garage.) A room has a length, width, a floor covering, and a number of closets. You can never create an instance of a building, but every object that is a building must have a method that calculates the floor space,...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • plz write if it is in another class or package Question 1: 1. Create a new...

    plz write if it is in another class or package Question 1: 1. Create a new project in Eclipse (File > New > Java Project.) Name it Homework2Q1 2. Create a package for your classes 3. Create an abstract superclass with only non default constructor(s) 4. Create two different subclasses of that superclass 5. Create another class that is not related (you need a total of four classes up to this point) 6. Create an interface 7. Make the class...

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

  • JAVA Developing a graphical user interface in programming is paramount to being successful in the business...

    JAVA Developing a graphical user interface in programming is paramount to being successful in the business industry. This project incorporates GUI techniques with other tools that you have learned about in this class. Here is your assignment: You work for a flooring company. They have asked you to be a part of their team because they need a computer programmer, analyst, and designer to aid them in tracking customer orders. Your skills will be needed in creating a GUI program...

  • Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to...

    Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to display several different types (classes) of objects. Let's say, we want to display three objects of type Deer, two objects of type Tree and one object of type Hill. Each of them contains a method called display. We would like to store their object references in a single array and then call their method display one by one in a loop. However, in Java,...

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

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

  • Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one me...

    Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...

  • Can someone help me with the main class of my code. Here is the assignment notes....

    Can someone help me with the main class of my code. Here is the assignment notes. Implement project assignment �1� at the end of chapter 18 on page 545 in the textbook. Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that the method is delcared static. Therefore, you do not need to create a new instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)" with the appropriate 4 parameter values. When...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

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