Question

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:

- Car rentalCars[5] //a constant size so that you don't have to worry about handling the flexible size of the array.

member methods:

- public void setRentalCars(Car[] rentalcars) // array as a formal param

- public Car[] getRentalCars() //array as a return type; pay attention to deep copy

- public String rentACar(cartype) // eg. rentACar(Car.cartype.COMPACT);

     return plateNum if available, otherwise null or empty string "" //search through the rental car list and find the first available car of the type matching the argument; //Please note, once the car is rented, the car's availability should be set to false or 0.

-returnACar(platNum) //Reset the car's availability to true

classManageCars// or any name if you'd like. Put your main() function here to test your program.  

          In your main()      

          1). You may manually create five Car objects here, say Car[] myRentalCars = new Car[5]. No need to use Scanner to ask user to input data to save time. e.g.: You may directly put - rentalCar[0] = new Car(); rentalCar[0].setPlateNum("P1"); rentalCar[0].setCarSize(Car.rentalcar.COMPACT); rentalCar[0].setAvailability(true); or using a Car constructor of three parameters e.g.: new Car("P1", Car.rentalcar.COMPACT,1);

        2). Create a CarRental object, say myRental

          3)  myRental.setRentalCars(myRentalCars);

          4) Put a few calls to rentACar() and returnACar() of myRental to test your program. e.g.: myRental.rentACar(Car.cartype.COMPACT); myRental.rentACar(Car.cartype.MIDSIZE); myRental.returnACar("PlateNo1");

To add any additional properties, methods if necessary

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

The source code for the same is as follows:

Source Code:

package yourLastName.cis4110.assignment5;

class Car {

enum Cartype {
SUBCOMPACT, COMPACT, MIDSIZE, FULLSIZE
};

private String plateNum;
private Cartype carType;
private boolean availability;

public Car() {
}

public Car(String plateNum, Cartype carType, boolean availability) {
this.plateNum = plateNum;
this.carType = carType;
this.availability = availability;
}

public String getPlateNum() {
return plateNum;
}

public void setPlateNum(String plateNum) {
this.plateNum = plateNum;
}

public Cartype getCarType() {
return carType;
}

public void setCarType(Cartype carType) {
this.carType = carType;
}

public boolean getAvailability() {
return availability;
}

public void setAvailability(boolean availability) {
this.availability = availability;
}
}

class RentACar {

private Car rentalCar[] = new Car[5];

public void setRentalCars(Car[] rentalcars) {
for (int i = 0; i < 5; i++) {
rentalCar[i] = rentalcars[i];
}
}

public Car[] getRentalCars() {
Car cars[] = new Car[5];

for (int i = 0; i < 5; i++) {
cars[i] = rentalCar[i];
}

return cars;
}

public String rentACar(Car.Cartype c) {
for (int i = 0; i < 5; i++) {
if (rentalCar[i].getCarType() == c) {
rentalCar[i].setAvailability(false);
return rentalCar[i].getPlateNum();
}
}

return "";
}

public void returnACar(String platNum) {
for (int i = 0; i < 5; i++) {
if (rentalCar[i].getPlateNum().equals(platNum)) {
rentalCar[i].setAvailability(true);
break;
}
}
}
}

class ManageCars {

public static void main(String[] args) {
Car[] myRentalCars = new Car[5];
myRentalCars[0] = new Car();
myRentalCars[0].setPlateNum("P1");
myRentalCars[0].setCarType(Car.Cartype.COMPACT);
myRentalCars[0].setAvailability(true);

myRentalCars[1] = new Car("P2", Car.Cartype.SUBCOMPACT, true);
myRentalCars[2] = new Car("P3", Car.Cartype.FULLSIZE, true);
myRentalCars[3] = new Car("P4", Car.Cartype.MIDSIZE, true);
myRentalCars[4] = new Car("P5", Car.Cartype.SUBCOMPACT, true);

RentACar myRental = new RentACar();

myRental.setRentalCars(myRentalCars);

System.out.println("Car of type COMPACT rented having plate number: " + myRental.rentACar(Car.Cartype.COMPACT));
System.out.println("Car of type MIDSIZE rented having plate number: " + myRental.rentACar(Car.Cartype.MIDSIZE));

System.out.println("Returning a car with plate number P1");
myRental.returnACar("P1");
}
}

Output:

run: Car of type COMPACT rented having plate number: P1 Car of type MIDSIZE rented having plate number: P4 Returning a car wi

Description:

Here I have written the 3 classes as needed.

At first, I am creating enum Cartype with 4 values.

Car class has 3 member variables and I have written a constructor for this, it was mentioned at the end that you can use the constructor so I defined it here.

Then I have created the getter and setter methods for every variable.

The next class is RentACar.

In this, I am defining an array of 5 Car object.

for setting and getting the array elements, deep copy is to be used.

This means you should copy each and every element of array from source to destination instead of just writing cars = rentalCar or rentalCar = cars.

If you write as above, it is shallow copying in which rentalCar and cars, both the object are pointing to the same location. But we need to make 2 arrays so both of the arrays must be different. So I have used a for loop which runs for 5 times, (number of car object times) and copying each and every array element i.e. object in setter and getter method.

In the rentACar(), a car type from above 4 is specified and if that type car is available, it is returned and its availability is set to false.

As it is asked that the first available car of that type is to be returned, I am using the for loop and iterating through every element, and when the first element of that car type is found, its availability is set to false and then the car object is returned.

If there is no such car object with that type, "" is returned.

The last method returnACar() is taking a plate number and then finding the car with that number plate and then sets its availability to true as this means this car was given for rent and now it is returned.

In the ManageCars class, I am writing a main() method in which I am creating a 5 Car object array and setting the values.

Then I am renting two cars, one with type COMPACT and the other with MIDSIZE.

So, for size COMPACT, car with number plate P1 is given for rent and for size MIDSIZE, car with number plate P4 is given for rent.

And then I am returning a car with number plate P1.


Do comment if there is any query. Thank you. :)

Add a comment
Know the answer?
Add Answer to:
please need help with the java program A micro car rental company. package yourLastName.cis4110.assignment5; Please create...
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
  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • I need this in java please Create an automobile class that will be used by a...

    I need this in java please Create an automobile class that will be used by a dealership as a vehicle inventory program. The following attributes should be present in your automobile class: private string make private string model private string color private int year private int mileage. Your program should have appropriate methods such as: default constructor parameterized constructor add a new vehicle method list vehicle information (return string array) remove a vehicle method update vehicle attributes method. All methods...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • Can you please produce the Java code that solves this non-assessed homework problem? I'm very new...

    Can you please produce the Java code that solves this non-assessed homework problem? I'm very new to Java and Object-Oriented Programming and I have no idea how to construct the logic for this code. PROBLEM: "In this exercise, you will build a program that determines the validity of a select few simple English sentences, according to rules detailed below. A sentence will be represented as an array of words, which you will create as a static nested class `Word`. `Word`...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • How to write a program that create a car class with: instance variables name and mpg...

    How to write a program that create a car class with: instance variables name and mpg (make public) No need for a main() method In a separate file, create a CompareCars class with a main()method create 2 instances of car using new set the name and mpg for each instance variables are public, so use the name an mpg to access the instance variables compare the fuel efficiency of your 2 cars and print out which is more efficient to...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car...

    Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car class //Defined enum here enum Kind{business,maintenance,other,box,tank,flat,otherFreight,chair,seater,otherPassenger}; //Defined array of strings string KIND_ARRAY[]={"business","maintenance","other,box","tank,flat","otherFreight","chair","seater","otherPassenger"}; class Car { private: string reportingMark; int carNumber; Kind kind; //changed to Kind bool loaded; string destination; public: //Defined setKind function Kind setKind(string knd) { for(int i=0;i<8;i++) //repeat upto 8 kinds { if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array kind=(Kind)i; //setup that kind } return kind; } //Default constructor Car() { } //Parameterized constructor...

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