• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

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);
 }
 }
0
helpmeoot
Asked:
helpmeoot
  • 2
1 Solution
 
objectsCommented:
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.
0
 
helpmeootAuthor Commented:
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;
  }
0
 
objectsCommented:
> 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.

you could already do that by subclassing Car.
Unless there is a need for subclasses to not share attributes a Car thewn their is no need. Currently all your subclasses share the same attributes of Car so they can subclass Car. You could rename Car to Vehicle if you wanted to but functionally it would remain the same.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now