Link to home
Start Free TrialLog in
Avatar of helpmeoot
helpmeoot

asked on

Abstract Classes

I have created an Abstract Class 'Vehicle' to cover Cars, Minibuses and Trucks. Unfortunatley I have no experience with using Abstract classes and can't get the code to function.

Any assistance would be greatly appreciated!

import javax.swing.JOptionPane;
public abstract class Vehicle{

//instance variables
public String make, colour, registration, motDue;


// Constructor
public Vehicle(String make, String colour, String registration, String motDue){
      setMake(make);
      setColour(colour);
      setRegistration(registration);
      setMot(motDue);
}

// Create Text for Output
public String toString() {
String output;

output = "Description:                  Car\n";
output = output + "Make:                             " + make + "\n";
output = output + "Colour:                           " + colour + "\n";
output = output + "Registration:                " + registration + "\n";
output = output + "MOT Due:                      " + motDue + "\n";

return output;
}


public void printDetails(){
String output;

output = toString();
JOptionPane.showMessageDialog(null,output,"Details",JOptionPane.INFORMATION_MESSAGE);
}

//Set methods for properties

  public void setMake(String aMake){
    make = aMake;
  }

  public void setColour(String aColour){
    colour = aColour;
  }

  public void setRegistration(String aRegistration){
    registration = aRegistration;
  }

  public void setMot(String aMot){
    motDue = aMot;
  }


} //end class


import javax.swing.JOptionPane;
public class Car extends Vehicle{

//instance variables
private String make, colour, registration, motDue;


// Constructor
public Car(String make, String colour, String registration, String motDue){
      setMake(make);
      setColour(colour);
      setRegistration(registration);
      setMot(motDue);
}

// Create Text for Output
public String toString() {
String output;

output = "Description:                  Car\n";
output = output + "Make:                             " + make + "\n";
output = output + "Colour:                           " + colour + "\n";
output = output + "Registration:                " + registration + "\n";
output = output + "MOT Due:                      " + motDue + "\n";

return output;
}


public void printDetails(){
String output;

output = toString();
JOptionPane.showMessageDialog(null,output,"Details",JOptionPane.INFORMATION_MESSAGE);
}

//Set methods for properties

  public void setMake(String aMake){
    make = aMake;
  }

  public void setColour(String aColour){
    colour = aColour;
  }

  public void setRegistration(String aRegistration){
    registration = aRegistration;
  }

  public void setMot(String aMot){
    motDue = aMot;
  }



} //end class



public class Main {
 public static void main (String args[]){

 VehicleHireCompany vhc = new VehicleHireCompany("Vehicle Hire Ltd.");
 Vehicle v1 = new Car("Vauxhall", "Red", "MD03 PUV", "12th March 2009");
 Vehicle v2 = new Car("Subaru", "Silver", "ST05 HNX", "14th January 2007");
 Vehicle v1 = new Minibus("Ford", "White", "ST03 HGF", "14th January 2007", 15);
 Vehicle v4 = new Minibus("Mercedes", "Blue", "GH05 HKL", "27th July 2008", 13);
 Truck v5 = new Truck("Iveco", "Green", "SK05 GDL", "21st August 2008", 3);


 // Make Association
 vhc.ownsVehicle(v1);
 vhc.ownsVehicle(v2);
 vhc.ownsVehicle(v3);
 vhc.ownsVehicle(v4);
 vhc.ownsVehicle(v5);

 // Print List of Cars
 vhc.printDetails();


 System.exit(0);
 }
 }
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Looks like your Car class can just be:

public class Car extends Vehicle{
// Constructor
public Car(String make, String colour, String registration, String motDue){
     setMake(make);
     setColour(colour);
     setRegistration(registration);
     setMot(motDue);
}
}

There appears to be no difference between a Car and a Vehicle so I don't see why you need a Vehicle class.

You'll also need to define your Minibus and Truck classes.
Avatar of helpmeoot
helpmeoot

ASKER

Objects. This is a continuation from some help you gave me last week.

I have classes for Minibus and Truck but didnt post the code. I felt if I got the car class working I could amend the rest.

If i take the code out for creating the output text in each vehicle, how can I print the different attributes?

There isnt a difference between Car and a 'Standard Vehicle'. However, I have to set the hierarchy up to be able to add different vehicle types in the future.

I had the code working with Car as the superclass and truck/minibus as subclasses. It was pointed out to me though that this was incorrect.


import javax.swing.JOptionPane;

public class Truck extends Vehicle{

//instance variables
private String make, colour, registration, motDue;
private int payload;

// Constructor
public Truck(String make, String colour, String registration, String motDue, int payload){
     super(make, colour, registration, motDue);
     setPayload(payload);

}


// Create Text for Output
public String toString() {
String output;


output = "Make:                             " + make + "\n";
output = output + "Colour:                           " + colour + "\n";
output = output + "Registration:                " + registration + "\n";
output = output + "MOT Due:                      " + motDue + "\n";
output = output + "Max Payload (t):          " + payload + "\n";

return output;
}


public void printDetails(){
String output;

output = toString();
JOptionPane.showMessageDialog(null,output,"Vehicle Hire - Truck Details",JOptionPane.INFORMATION_MESSAGE);
}

  //Set methods for properties

  public void setMake(String aMake){
    make = aMake;
  }

  public void setColour(String aColour){
    colour = aColour;
  }

  public void setRegistration(String aRegistration){
    registration = aRegistration;
  }

  public void setMot(String aMot){
    motDue = aMot;
  }

  public void setPayload(int aPayload){
    payload = aPayload;
  }

   //Get methods for properties

  public String getMake(){
    return make;
  }
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial