Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java Inheritance

phm_357
phm_357 


In need of assistance with this Java assignment - First Part: Create a class called Vehicle in Vehicle.java with instance variables make (such as Ford, GM, Chrysler, Toyota, or Honda), year and horsepower (200, 300, and 400). Add the necessary constructor and set and get methods. Then create classes Bus and Truck in program files Bus.java and Truck.java, respectively. Bus has an instance variable called numOfPassengers (1 to 50) and Truck has an instance variable called towingCapacity (0.5, 1.0, 2.0, or 3.0 tons). Both Bus and Truck inherit from Vehicle and contain their own toString methods for displaying all their attributes (including the inherited ones). Compile all three classes. Second Part - ( I have attached an image of what the VehicleAPP.java output should look like) Modify Vehicle.java so that it includes an instance variable called milesPerGal. Both Bus and Truck have a method called calculateMGP. The formula for a bus's milesPerGal is 10000/numOfPassengers/horsepower, but for a truck it is 5000/towingCapacity/horsepower. Create a program called VehicleApp.java that displays all information about a specific bus and truck of your choice. *It looks like I will need a copy of the Vehicle.java before the modification and also one after the modification. Comments are always appreciated. Thanks very much, Chris Attachments Tags Clarifications

Answers

Java Vehicle Inheritance Solution

Seeker
Seeker 



Java source code is attached.

First iteration of Vehicle class is commented out at top of class.
Attachments
Vehicle.zip (1 K) Download
Preview
System.out.println("Enter the number of passengers (1 through 50): "); int busPass = s.nextInt(); System.out.println(); Vehicle bus = new Bus(busMake, busYear, busHP, busPass); System.out.println(bus.toString()); System.out.println(); s = new Scanner(System.in); System.out.println("Creating a Truck object"); System.out.println(); System.out.println("Enter the make: "); String

Preview
Vehicle{ private int numPassengers; public Bus(String make, int year, int hp, int numPassengers){ super(make, year, hp); this.numPassengers = numPassengers; calculateMPG(); } @Override

Preview
public void setTowingCapacity(double towingCapacity) { this.towingCapacity = towingCapacity; } public double getTowingCapacity() { return towingCapacity; } @Override public void calculateMPG() {

Preview
public void setMake(String make) { this.make = make; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getHorsepower() { return horsepower; } public

Preview.jpg (68 K) Download Preview

Screenshots
Screenshot
Purchase Answer

Answer

mariobeka
mariobeka 



Attached you will find two Eclipse projects archives. To get them running use the import option if you are using Eclipse IDE. Comments have been made in the Second part of the project which would be the same for the first part as well. Attachments
Vehicle.zip (6 K) Download







Preview
public void setNumOfPassengers(int numOfPassengers) { this.numOfPassengers = numOfPassengers; } public String toString() { return String.format("%s %s, %d hp, %d passengers",

Preview
towingCapacity; /** * @param make * @param year * @param horsepower * @param towingCapacity */ public Truck(String make, String year, int

Preview
this.horsepower = horsepower; } public String getMake() { return make; } public void setMake(String make) { this.make = make; } public String getYear() {

Preview
300 or 400): "); horsepower = in.nextInt(); System.out.print("Enter the number of passenger (1 through 50): "); numOfPassengers = in.nextInt(); Bus bus = new Bus(make, year, horsepower, numOfPassengers); System.out.println(bus); System.out.println("Creating a Truck object:"); System.out.print("Enter the make: "); make = in.next();

VehicleSecondPart.zip (8 K) Download







Preview
numOfPassengers; } public double calculateMGP(int numOfPsg, double mpg) { milesPerGal =(double) 10000 / numOfPassengers / horsepower; return milesPerGal; } @Override public String toString() { return String.format("%s %s, %d hp, %d passengers, %.2f mpg",

Preview
this.towingCapacity = towingCapacity; } public double calculateMGP(double townCap, double mpg) { milesPerGal =5000/towingCapacity/horsepower; return milesPerGal; } @Override public String toString() { return String.format("%s %s, %d hp, %.1f tons, %.2f mpg", make,year,horsepower,towingCapacity,

Preview
@param milesPerGal */ public Vehicle(String make, String year, int horsepower, double milesPerGal) { super(); this.make = make; this.year = year; this.horsepower = horsepower; this.milesPerGal = milesPerGal; } public String getMake() { return make; } public

Preview
0, 0, 0); Truck truck = new Truck(null, null, 0, 0, 0); //Create variable to store the input data String make; String year; int horsepower; int numOfPassengers; double towingCapacity; System.out.println("Creating a Bus object:"); System.out.print("Enter the make: "); make = in.nextLine(); bus.setMake(make); System.out.print("Enter the year: "); year = in.next(); bus.setYear(year); System.out.print("Enter

Screen Shot 2013-11-26 at 9.36.23 PM.png (28 K) Download Preview

Screenshots
Screenshot
Purchase Answer