Question

to build a vehicle factory that allows us to abstract a class called Vehicle, create a...

to build a vehicle factory that allows us to abstract a class called Vehicle, create a concrete class called Truck and then a main program that can create Trucks and store them in a garage (ArrayList). We then want to loop through the garage and print out all the stored trucks.

A) Create a Truck class and provide concrete details in Truck about trucks.

B) Create a main program class called MainProgram

C) Create 10 instances of trucks that will be stored in the ArrayList

D) Loop through your ArrayList and print out all the truck details to the screen.

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

import java.util.ArrayList;
abstract class Vehicle
{
  
private String manufacturerName;
private int year;
private String owner;
  
public Vehicle(String manufacturerName,int year,String owner)
{
   this.manufacturerName = manufacturerName;
   this.year = year;
   this.owner = owner;
}
  
public String getManufacturerName()
{
return manufacturerName;
}
public String getOwner()
{
return owner;
}
  
public void setManufacturerName(String manufacturerName)
{
this.manufacturerName = manufacturerName;
}
public void setOwner(String owner)
{
this.owner = owner;
}
public void setYear(int year)
{
this.year = year;
}
public int getYear()
{
return year;
}

public Vehicle()
{
manufacturerName = "Ford";
year = 2000;
}
public void printMe()
{
System.out.print("Manufacturer = "+manufacturerName +" Year = "+year+" Owner's Name = "+ owner);
}
  
}
class Truck extends Vehicle
{
private int MPG;
  
public Truck()
{
super();
}
public Truck(String manufacturerName,int year,String owner,int MPG)
{
   super(manufacturerName,year,owner);
   this.MPG = MPG;
}
//set and get methods
public int getMPG()
{
return MPG;
}
public void setMPG(int MPG)
{
this.MPG = MPG;
}
  
public void printMe()
{
super.printMe();
System.out.print(" MPG : "+MPG);
System.out.println();
}
  
  
}
class Test
{
public static void main (String[] args)
{
ArrayList<Truck> trucks = new ArrayList<Truck>();

Truck T1 = new Truck("Honda",2009,"James Gosling",12);
trucks.add(T1);

Truck T2 = new Truck("Ford",2012,"Smith Jones ",11);
trucks.add(T2);

Truck T3 = new Truck("Tata",2019,"Adam Hawkings",9);
trucks.add(T3);


Truck T4 = new Truck("Honda",2009,"Bob Rich",12);
trucks.add(T4);

Truck T5 = new Truck("Honda",2005,"David Johnson",11);
trucks.add(T5);

Truck T6 = new Truck("Heno",2016,"Joy Wilkinson",9);
trucks.add(T6);

Truck T7 = new Truck("Volvo Group",2014,"Kevin Smith",12);
trucks.add(T7);

Truck T8 = new Truck("PACCAR",2010,"Mike",11);
trucks.add(T8);

Truck T9 = new Truck("Honda",2017,"Oliver",9);
trucks.add(T9);

Truck T10 = new Truck("Tata Motors",2009,"Ron",9);
trucks.add(T10);


for(int i = 0; i < trucks.size(); i++)
           trucks.get(i).printMe();
          
          
          
}
}

Output:

Manufacturer = Honda Year = 2009 Owner's Name = James Gosling  MPG : 12
Manufacturer = Ford Year = 2012 Owner's Name = Smith Jones   MPG : 11
Manufacturer = Tata Year = 2019 Owner's Name = Adam Hawkings  MPG : 9
Manufacturer = Honda Year = 2009 Owner's Name = Bob Rich  MPG : 12
Manufacturer = Honda Year = 2005 Owner's Name = David Johnson  MPG : 11
Manufacturer = Heno Year = 2016 Owner's Name = Joy Wilkinson  MPG : 9
Manufacturer = Volvo Group Year = 2014 Owner's Name = Kevin Smith  MPG : 12
Manufacturer = PACCAR Year = 2010 Owner's Name = Mike  MPG : 11
Manufacturer = Honda Year = 2017 Owner's Name = Oliver  MPG : 9
Manufacturer = Tata Motors Year = 2009 Owner's Name = Ron  MPG : 9

Do ask if any doubt. Please up-vote.

Add a comment
Know the answer?
Add Answer to:
to build a vehicle factory that allows us to abstract a class called Vehicle, create a...
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
  • Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a...

    Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a float Speed and string Name, an abstract method Drive, and include a constructor. Create a Tesla class - Tesla inherits from Vehicle. - Tesla has an int Capacity. - Include a constructor with the variables, and use super to set the parent class members. - Override Drive to increase the speed by 1 each time. - Add a toString to print the name, speed,...

  • 7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food...

    7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food to represent a Lunch food item. Fields include name, calories, carbs In a main method (of a different class), ask the user "How many things are you going to eat for lunch?". Loop through each food item and prompt the user to enter the name, calories, and grams of carbs for that food item. Create a Food object with this data, and store each...

  • An abstract class doesn't have a constructor (because you cannot make an object of the abstract...

    An abstract class doesn't have a constructor (because you cannot make an object of the abstract class). It should have at least one method, which then has to be overridden in all derived classes. Here is an example:   abstract void run();   class Honda4 extends Bike{   public static void main(String args[]){   obj.run();   } You are to create an abstract class called Shape, which has an abstract method called computeArea(). Derive a Circle class from Shape. (Circle is similar to your previous...

  • Write a program in C++ with comments! Create an abstract class Property, containing the data items...

    Write a program in C++ with comments! Create an abstract class Property, containing the data items owner, address and price. Add an abstract method showOwner(). Also provide getter/setters for all the data items. Make validations if necessary in the setter so that no invalid values are entered. Create a class House, to inherit the Property class. This class should contain additional data item – yardSize – an integer – the size of the yard of the house, getter and setter...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

  • (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...

    (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...

  • Your job is to do the following: build a Monster class as your base class, along...

    Your job is to do the following: build a Monster class as your base class, along with two derived classes, Undead and Animal. All Monsters have names and origins. Undead monsters have the year their heart stopped beating. Animals have a species. Your Undead class should be extended to create a Zombie class and a Vampire class. Your Animal Class will extend to include a Werewolf class. Zombies have a favorite weapon, Vampires have a number of humans they have...

  • Write a new program called TrickOr Treat that will do the following 1. In a while...

    Write a new program called TrickOr Treat that will do the following 1. In a while loop, say "Trick or Treat!" and allow the user to type in the name of a candy and store it in an ArrayList. Once the user types "Trick", then the loop should stop. Then, print out the total number of candies on the screen. (See example below) [1 points] 2. Write a method called countSnickers that will take an ArrayList of candy as an...

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

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