Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment must be done in Java. Main Task Use a factory pattern to support

ID: 3725055 • Letter: T

Question

This assignment must be done in Java.

Main Task

Use a factory pattern to support a rental car program

Create RentalCar class, with following attributes

Name

# of people can fit

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

Create 4 types of rental cars

Toyota Corolla, 4

Dodge Minivan, 7

Ford Escape Hybrid, 5

Yamaha Motorcycle, 1

Create a RentalCarFactory class that takes number of people as a parameter and returns the smallest car that will fit everyone

Write a Test class that does the following in its main()

Extra Credit

Use the observer pattern to support a mythical broadcast system

Create a Broadcaster class that supports:

void register (Receiver r) – objects of type Receiver can register

void broadcast (String message) – sneds message to all registered Receivers

The Receiver class should support:

Receiver(String name) – constructor that sets the name

void receive (String message) – accepts messages sent to it and prints them out

Write a Test class that does the following in its main()

This should result in the output below

for i

Explanation / Answer

import java.util.*;

class RentalCar
{
String name;
int numberPeople;
int smallest;

RentalCar(){}

RentalCar(String nm,int noP)
{
name=nm;
numberPeople=noP;
}

public String toString()
{

RentalCarFactory rcf=new RentalCarFactory();
smallest=rcf.rent(numberPeople);
return "Car Name is "+name+" "+"Number of People : "+numberPeople;
}

void smallestCar()
{
System.out.println("Smallest car is "+smallest);
}
public class RentalCarFactory
{
int rent(int no)
{
int[] noPeople=new int[4];
int i;
int small;
for(i=0;i<=3;i++)
{
noPeople[i]=no;
}


small=noPeople[0];
for(i=0;i<noPeople.length;i++)
{
if(noPeople[i]<small)
{
small=noPeople[i];
}
}
return small;
}
}
}

class TestClass
{
public static void main(String args[])
{
String nm;
int noP,smallestCar;
Scanner sc=new Scanner(System.in);
RentalCar car=new RentalCar();
RentalCar[] r1=new RentalCar[4];

for(int i=0;i<=3;i++)
{
sc.nextLine();
System.out.print("Enter name of the car : ");
nm=sc.nextLine();

System.out.print("Enter number of people that can fit inside the Car : ");
noP=sc.nextInt();

r1[i]=new RentalCar(nm,noP);
}
System.out.println();
for(int i=0;i<=3;i++)
{
String details=r1[i].toString();
System.out.println(details);
}
car.smallestCar();
}
}


class Broadcaster
{
void register(Receiver r)
{
r=new Receiver();

}
void broadcast(String message)
{
Receiver r1=new Receiver("First");
r1.receive(message);
Receiver r2=new Receiver("Second");
r2.receive(message);
}
}

class Receiver extends Broadcaster
{
String name;

Receiver(){}

Receiver(String name1)
{
name=name1;
}

void receive(String message)
{
System.out.println(name+" recieved : "+message);
}
}

class Test
{
public static void main(String args[])
{
Broadcaster b=new Broadcaster();
Receiver r1=new Receiver("First");
Receiver r2=new Receiver("Second");
b.register(r1);
b.register(r2);
b.broadcast("Hello world!");
}
}