This assignment must be done in Java. Main Task o Use a factory pattern to suppo
ID: 3726494 • Letter: T
Question
This assignment must be done in Java.
Main Task
o Use a factory pattern to support a rental car program
o Create RentalCar class, with following attributes
o Name
o # of people can fit
o Write a toString() method for the RentalCar class so that it prints out the name of the car and the # of people that can fit
o Create 4 types of rental cars
o Toyota Corolla, 4
o Dodge Minivan, 7
o Ford Escape Hybrid, 5
o Yamaha Motorcycle, 1
o Create a RentalCarFactory class that takes number of people as a parameter and returns the smallest car that will fit everyone
o Write a Test class that does the following in its main()
Extra Credit
o Use the observer pattern to support a mythical broadcast system
o Create a Broadcaster class that supports:
o void register (Receiver r) – objects of type Receiver can register
o void broadcast (String message) – sneds message to all registered Receivers
o The Receiver class should support:
o Receiver(String name) – constructor that sets the name
o void receive (String message) – accepts messages sent to it and prints them out
o Write a Test class that does the following in its main()
This should result in the output below
for iExplanation / Answer
public class RentalCar
{
String name;
int capacity;
public RentalCar(String name,int capacity)
{
this.name=name;
this.capacity=capacity;
}
RentalCar(){};
public String getName()
{
return name;
}
public int getfit()
{
return capacity;
}
public String toString()
{
return(" Car name is "+this.getName()+ " and capacity is "+this.getfit() );
}
public String rent(int fit)
{
int Ycapacity=1;
int Tcapacity=4;
int Fcapacity=5;
int Dcapacity=7;
fit= fit;
if(fit<=Ycapacity)
return "yamaha";
else if(fit<=Tcapacity)
return "toyota";
else if(fit<=Fcapacity)
return "ford";
else
return "dodge";
}
public static void main(String[] args)
{
RentalCar Toyota=new RentalCar("Toyota Corolla",4);
RentalCar Dodge=new RentalCar("Dodge Minivan",7);
RentalCar Ford=new RentalCar("Ford Escape Hybrid",5);
RentalCar Yamaha=new RentalCar("Yamaha Motorcycle",1);
System.out.println(Toyota.toString());
System.out.println(Dodge.toString());
System.out.println(Ford.toString());
System.out.println(Yamaha.toString());
RentalCar car=new RentalCar();
for(int i=1;i<7;i++)
{
System.out.println("Car that holds " +i +" people :"+car.rent(i));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.