0

Picture to show task: enter image description here

First I am sorry, for my bad to for expressing my mind.

I have such a task, I don't need that you do it for me.

Vehicle is parent class for Sedan (Cause Sedan class is String type).

  1. How to extend or implement Vehicle class with universal class?
  2. I forgot to ask my teacher, but maybe you will know, what means striped pointer to Owner class, and what is that: has a?

P.S. If you need code that I have written already, I will show you.

So this is my parent Vehicle class:

public class Vehicle {

    private int vehicleNumber;
    protected int fuelTankSize;
    protected int maxSpeed; 
    protected   Owner owner;

    //1
    public Vehicle(int vehicleNumber){
        this.vehicleNumber = vehicleNumber;
    }
    //2
    public Vehicle(int vehicleNumber, int fuelTankSize) {
        this.vehicleNumber = vehicleNumber;
        this.fuelTankSize = fuelTankSize;
    }
    //3
    public Vehicle(int vehicleNumber,  int fuelTankSize, int maxSpeed) {
        this.vehicleNumber = vehicleNumber;
        this.fuelTankSize = fuelTankSize;
        this.maxSpeed = maxSpeed;
    } 
    //4
    public Vehicle(int vehicleNumber, int fuelTankSize, int maxSpeed, Owner owner) {
        this.vehicleNumber = vehicleNumber;
        this.fuelTankSize = fuelTankSize;
        this.maxSpeed = maxSpeed;
            this.owner = owner;
    } 

    //1
    public  int getMaxSpeed() {
        return maxSpeed;
    }
    public void setMaxSpeed (int maxSpeed){
        this.maxSpeed = maxSpeed;
    }
    //2
    protected int getFuelTankSize(){
        return fuelTankSize;
    }
    protected void setFuelTankSize (int fuelTankSize){
        this.fuelTankSize = fuelTankSize;
    }
    //3
    public Owner getOwner(){
        return owner;
    }
    public void setOwner (Owner owner){
        this.owner = owner;
    }

}

child Sedan with:

public class Sedan extends Vehicle {

    private String registrationIndex;{      
    }

    public Sedan (int vehicleNumber, int fuelTankSize, int maxSpeed, String registrationIndex, Owner owner) {
        super(vehicleNumber, fuelTankSize, maxSpeed, owner);
        this.setRegistrationIndex (registrationIndex);
    }

    public String getRegistrationIndex (){
        return registrationIndex;
    }
    public void setRegistrationIndex (String registrationIndex) {
        this.registrationIndex = registrationIndex;
    }

}

second Universal child without an error:

public class Universal extends Vehicle {
    private int trunkSize;

    public Universal (int vehicleNumber, int fuelTankSize, int maxSpeed, int trunkSize, Owner owner) {
        super(vehicleNumber, fuelTankSize, maxSpeed, owner);
        this.setTrunkSize (trunkSize);
    }

    public int getTrunkSize() {
        return trunkSize;
    }
    public void setTrunkSize(int trunkSize) {
        this.trunkSize = trunkSize;
    }

    public void printDescription() {
        super.printDescription();
        System.out.println("Universalo bagažinės tūris: " + getTrunkSize() + "l.");
    }

}

and some misterious (to me) Owner class:

public class Owner  {
    public String firstName;
    public String lastName;

    public Owner (String firstName){
        this.firstName = firstName;
    }

    public Owner (String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

added VechileTest for testing:

public class VehicleTest {

        public static void main(String[] args) {


            Vehicle vehicleInf = new Vehicle (1, 45, 260);
            Universal universalInf = new Universal(2, 50, 220, 70);
            Sedan sedanInf = new Sedan (3, 40, 180, "AVA 123");

            vehicleInf.printDescription();
            universalInf.printDescription();
            sedanInf.printDescription();
        }
    }
galvakojis
  • 408
  • 1
  • 5
  • 19

2 Answers2

2

Well, 1st of all I recommend you read a good tutorial / explanation of UML class diagrams, like this here for example.

After you know the basics, it should be easy to translate that into Java code. I'll give you the code for the Universal class and a start for your Vehicle. The rest you'll have to do on your own.

The class Universal:

public class Universal extends Vehicle {

  private int trunkSize;

  public int getTrunkSize() {
    return this.trunkSize;
  }

  public void setTrunkSize(int trunkSize) {
    this.trunkSize = trunkSize;
  }
}

As you can see the first block inside a class box refers to the variables. The - and + indicates the visibility (private for -, public for +).

The next block is about the methods, specifying visibility, return type, method name and parameters (type and name).

The arrow between Universal and Vehicle indicates a inheritance relationship (see in code that Universal extends Vehicle).

So all in all the diagram is a construction plan for your classes; at least for the static part, meaning the relationships and state they can have.

The start of class Vehicle:

public class Vehicle {
    private int vehicleNumber;
    // the rest here ...
}

Edit: Well, now that I see your code, you seem to have a few misconceptions:

  1. The Sedan type is not from type String, it is from type Sedan (which extends Vehicle). Just the new member variable in the Sedan type is of type String, does not matter.

  2. To your 1st question: The Vehicle class is the base (parent) class of Sedan. You do not to do anything with it, inheritance is expressed from the child towards the parent, not the other way around. Vehicle should usually be declared abstract (as you cannot create an instance of a generic Vehicle), but this is not in the diagram.

  3. To your 2nd question: The has a relationship is just this. It expressed that one class has another class as it's member (which is redundantely expressed inside the class diagram already), so nothing to do for that.

Additionally your code has a few issues:

  1. I do not see any constructors declared in Vehicle class, those 4 can go.
  2. Your Sedan has a superflous pair of {} after declaration of your registrationIndex variable.
  3. Since your Vehicle has no default constructor, you must call this constructor from your Sedan class (or remove the constructors from Vehicle.
  4. Your Universal class calls the Vehicle constructor with the trunkSize while the Vehicle constructor expects the vehicleNumber there.
Matthias
  • 3,582
  • 2
  • 30
  • 41
  • Thanks, but this is not exactly what a was looking for – galvakojis Nov 26 '13 at 08:25
  • @user2956929 Added some further explanation specific to your code. – Matthias Nov 26 '13 at 08:38
  • Maybe you can write how to call Vehicle from Sedan P.S. made some changes to Sedan class – galvakojis Nov 26 '13 at 09:58
  • You should handle Sedan's state in Sedan constructor (the registrationIndex) and pass the state of Vehicle to that class by invoking an constructor of Vehicle with super. You have to match one of the existing ones in Vehicle like `public Vehicle(int vehicleNumber)`. So for this you'll have to do something like this `super(4711);` Read up on constructors and inheritance. E.g. here: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html – Matthias Nov 26 '13 at 10:04
  • silly me and my bad, thank you for showing it! now i have just need to find out more inf about Owner – galvakojis Nov 26 '13 at 10:30
  • Added some changes. Now i have problems with Owner class in my VehicleTest file: Universal(int int int int0 is undentufied. – galvakojis Nov 26 '13 at 12:06
  • Well, you probably should pass an instance of Owner to the Sedan constructor. Something like `new Owner("Sep", "Meier")`. But really read up on the links I provided above. When you understand these things, it gets easier for you and those things get resolved quicker. – Matthias Nov 26 '13 at 12:32
0

Your Vehicle class doesn't have a parameterless constructor, which means that Universal and Sedan must explicitly call one of them (super(...);). You're doing this in Universal (albeit incorrectly as you're passing the trunk size instead of the vehicle number expected by Vehicle's constructor) but not in Sedan.

As for the second question: The two major relations in OOP are is a and has a. The difference can be easily explained like this:

  • A Sedan is a vehicle
  • A vehicle has an owner

is a means it inherits some properties of something else, has a means that it has a reference to something else.

BambooleanLogic
  • 7,530
  • 3
  • 30
  • 56
  • Made some changes(you can see it) to Sedan and now it is better, but i get error: "Vehicle(string) is undentified" – galvakojis Nov 26 '13 at 09:56