Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This assignment is on Inheritance and Containment. I have given you the superclass Vehicle. You must write the 2 subclasses Car and Airplane. All vehicles

This assignment is on Inheritance and Containment. I have given you the superclass Vehicle. You must write the 2 subclasses Car and Airplane. All vehicles contain an Engine. You must write the class Engine. Only Car contains a Radio. You must write Radio.

By reading the main() function and examining the desired output you can discover the required semantics of the methods needed.

I.e.,

Airplane has 5 constructors: 1. Default 2. Name only 3. Name and cost per hour. 4. Name, cost per hour and Engine 5. Copy constructor - takes another Airplane for an argument.

You can deduce what the default argument values are for constructors 1 thru 3 by comparing the main() with the desired output.

Car has 5 constructors: same guidelines except the second argument is Miles Per Gallon instead of cost per hour and the last argument is a Radio.

ALl default Radio's have name "Original".

In order to write "getTripCost(int numOfMiles)" you will need to know that Airplanes fly at 500 MPH and Car fuel cost $3.00 per gallon. Those values must defined as constants in their appropriate class.

I have given you the code for Vehicle equals().

2 Cars are equal iff their engines are equal and their Miles Per Gallon are equal.

2 Airplanes are equal iff their engines are equal and their Cost Per Hour are equal.

2 Engines are equal if their Makers are equal and their fuels are equal.

Radios play no role in Vehicle equality.

You Must submit only: Car.java, Airplane.java, Engine.java and Radio.java. I will use my Vehicle.java and main() for grading. main:

import java.util.ArrayList;

public class AssignmentOne {

/** * @param args */ public static void main(String[] args) { Airplane a1 = new Airplane(); Airplane a2 = new Airplane("a2"); Airplane a3 = new Airplane("a3", 1000); Airplane a4 = new Airplane("a4", 200, new Engine("GE", "kerosine")); Airplane a5 = new Airplane(a1); Car c1 = new Car(); Car c2 = new Car("c2"); Car c3 = new Car("c3", 100); Car c4 = new Car("c4", 200, new Engine("Ford", "Gas")); Car c5 = new Car(c1); Radio r1 = new Radio("Upgraded Radio"); c1.setRadio(r1); ArrayList vehicles = new ArrayList(); vehicles.add(a1); vehicles.add(a2); vehicles.add(a3); vehicles.add(a4); vehicles.add(a5); vehicles.add(c1); vehicles.add(c2); vehicles.add(c3); vehicles.add(c4); vehicles.add(c5); for(Vehicle v : vehicles) { sop(" " + v); sop("Name: " + v.getName()); sop("3000 Mile Trip Cost: " + v.getTripCost(3000)); } sop(""); Engine e1 = new Engine(); Engine e2 = new Engine("Honda"); Engine e3 = new Engine("GMC", "Water"); sop("Engine e1: " + e1); sop("Engine e2: " + e2); sop("Engine e3: " + e3); sop(""); sop("a1 == a1? " + a1.equals(a1)); sop("a1 == a2? " + a1.equals(a2)); sop("a1 == a5? " + a1.equals(a5)); sop("c1 == c1? " + c1.equals(c1)); sop("c1 == c5? " + c1.equals(c2)); sop("a1 == c1? " + a1.equals(c1)); }

static private void sop(String s) { System.out.println(s); } }

Vehicle:

public abstract class Vehicle { public Vehicle() { this("No Name Vehicle"); } public Vehicle(String name) { this(name, new Engine("GE", "Gas")); } public Vehicle(String name, Engine engine) { mName = name; mEngine = engine; } public String getName() { return mName; } public Engine getEngine() { return mEngine; } @Override public String toString() { return "Name = " + mName + ", " + mEngine; } @Override public boolean equals(Object other) { if(!(other instanceof Vehicle)) return false; else { Vehicle v = (Vehicle)other; return mEngine == v.mEngine; } } public abstract int getTripCost(int miles); private String mName; private Engine mEngine; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

4. Identify cultural variations in communication style.

Answered: 1 week ago

Question

9. Understand the phenomenon of code switching and interlanguage.

Answered: 1 week ago

Question

8. Explain the difference between translation and interpretation.

Answered: 1 week ago