Turn on thread page Beta

Programming in Java HELP!!!! - UML diagram & Creating Vehicle class watch

    • Community Assistant
    • Very Important Poster
    • Thread Starter
    Offline

    20
    ReputationRep:
    Community Assistant
    Very Important Poster
    I am in desperate need of some help with my code.

    so I was given this UML diagram to follow:
    Name:  uml diagram.png
Views: 47
Size:  29.4 KB

    This is the code for the Vehicle class (Note: the code for the Vehicle class was pre-written and in the same project):
    Code:
    public class Vehicle { private String regNumber; private String make; private int yearMade; private double value; public Vehicle(String regNumberIn, String makeIn, int yearMadeIn, double valueIn) { regNumber = regNumberIn; make = makeIn; yearMade = yearMadeIn; value = valueIn; } public void setValue(double valueIn) { value = valueIn; } public String getRegNumber() { return regNumber; } public String getMake() { return make; } public int getYear() { return yearMade; } public double getValue() { return value; } public int calculateAge(int currentYearIn) { return currentYearIn - yearMade; } }
    And I was asked to write the code for the SecondHandVehicle subclass but to leave out the last two methods (sellVehicle, getNumberOwners), this is my code so far:

    //This is my constructor
    Code:
    public class SecondHandVehicle { private int numberOFOwners; public SecondHandVehicle (String Registration, String Make, int Manufacture, double value, int numberOfOwners) { } }
    Now I was then asked to create another class called TestSHV:
    Code:
    public class TestSHV { public static void main(String[] args) { SecondHandVehicle myCar = new SecondHandVehicle(); } }
    But I'm receiving errors with this object, and I do not understand why? Additionally, while creating this object, I am also supposed to fill out the following details:
    Registration: jhf72935eu Make: Peugeot 208 Year of manufacture: 2001 Value: 10 Number of owners: 5, which I don't understand how I am supposed to do this?

    There are more parts to do this which I do not understand how to do either.
    Go to the SecondHandVehicle class and add in the
    getNumberOfOwners method. Then return to your TestSHV program and display the number of owners by using this method.

    j) Go back to the SecondHandVehicle class for the last time and add in the sellVehicle method. This method accepts a new value for the vehicle when it is sold and increases the number of owners by one.

    k) Finally, assume that the car has now been sold for 4856. Using the appropriate methods of the SecondHandVehicle object, update the myCar object accordingly and then display the car’s new value and the new number of owners
    If someone could just point me to the right direction as I am honestly so confused, I've been trying to do this for the past 4 days and still do not understand any of it :emo:

    *Error with my object image*
    Attached Images
     
    • Community Assistant
    • Very Important Poster
    • Thread Starter
    Offline

    20
    ReputationRep:
    Community Assistant
    Very Important Poster
    javaKing winterscoming Would any of you be able to provide me with some help with this, please? I'm confused with all of this - I'm relatively new to Java.
    Offline

    4
    ReputationRep:
    I think the object should be of type TestSHV, which is the class name.
    is SecondHandVehicle the name of another class you are working with?
    • Community Assistant
    • Very Important Poster
    • Thread Starter
    Offline

    20
    ReputationRep:
    Community Assistant
    Very Important Poster
    .
    (Original post by javaKing)
    I think the object should be of type TestSHV, which is the class name.
    is SecondHandVehicle the name of another class you are working with?
    Yeah, the SecondHandVehicle and TestSHV are both in the same project file - I am supposed to create a SecondHandVehicle object within the TestSHV class with the following details mentioned above i.e. Registration, Make, Value etc
    Offline

    4
    ReputationRep:
    Try adding "extends SecondHandVehicle" between "public class TestSHV" and {
    Offline

    17
    ReputationRep:
    Sorry, I couldn’t program to save my life. It’s sad. And the stuff u wrote up there is gibberish to me but I’m sure someone smart will be able to help you.
    Good luck, smart person!
    Posted on the TSR App. Download from Apple or Google Play
    Offline

    18
    ReputationRep:
    (Original post by squirrology)
    But I'm receiving errors with this object, and I do not understand why? Additionally, while creating this object, I am also supposed to fill out the following details:
    Registration: jhf72935eu Make: Peugeot 208 Year of manufacture: 2001 Value: 10 Number of owners: 5, which I don't understand how I am supposed to do this?
    It looks to me as if you're being asked to use those values in the constructor for your class. (I'd really recommend reading the link if constructors aren't familiar to you). But to provide the short version - a constructor is just a block of code belonging to a class which is run when you use the new keyword to create an object. (i.e. think of it as being a bit like a method whose purpose is to let you run some code at the same time the object is created - like an initialiser).

    Just to elaborate on this - your error is here:
    Code:
    SecondHandVehicle myCar = new SecondHandVehicle();
    When you use the new keyword, two things happen:
    1. A new object is created in memory
    2. The constructor for the object's class is called

    In the case of your SecondHandVehicle class, the constructor (being similar to a method), has a number of arguments, which you've defined here:
    Code:
    public SecondHandVehicle (String Registration, String Make, int Manufacture, double value, int numberOfOwners) 
    { 
    }
    To pass data into a constructor, you need to use similar syntax that you'd use for calling a method.

    The actual problem you've got is that this code is attempting to use the 'default constructor' - the term 'default constructor' is just a way of describing a constructor which can be used without any arguments - i.e. there are no arguments with new SecondHandVehicle() therefore it uses the default constructor.

    The reason this is breaking is that you've already defined a different constructor - the one above which has those arguments in its list. In Java, when you provide your own constructor, Java will get rid of the default constructor. In other words, the existence of your own constructor means that new SecondHandVehicle() is no longer valid.

    Given the task you've been asked to complete, it looks like you don't actually need a default constructor - you want to use the one you've been given, so you just need to pass those arguments. i.e.
    • Registration: jhf72935eu
    • Make: Peugeot 208
    • Year of manufacture: 2001
    • Value: 10
    • Number of owners: 5

    So with that in mind, try doing this instead, matching the arguments, and being aware of the types in your constructor signature -

    public SecondHandVehicle (String Registration, String Make, int Manufacture, double value, int numberOfOwners)
    Code:
    SecondHandVehicle myCar = new SecondHandVehicle("jhf72935eu", "Peugeot 208", 2001, 10.0, 5);
    Does this make sense?
    • Community Assistant
    • Very Important Poster
    • Thread Starter
    Offline

    20
    ReputationRep:
    Community Assistant
    Very Important Poster
    (Original post by winterscoming)
    ...
    Thank you! It does slightly make sense!

    Sorry! One last question, how can I display those values I.e "Peugeot 208", 2001 etc. onto the screen - To my understanding, you have to call the appropriate methods of the myCar object but I don't even know how to do this?

    I've tried doing
    Code:
    myCar.SecondHandVehicle();
    And also
    Code:
    System.out.println("Registration: " + myCar.*all the variables I defined in the constructor*;
    but display errors.

    Additionally, do you recommend any resources for me to use in order to become proficient in Java, as you can probably tell I'm struggling with most of its concepts?
    Offline

    18
    ReputationRep:
    Glad it's making sense

    There seem to be a couple of other things missing too from your code - firstly, (I assume) that your SecondHandVehicle is supposed to inherit (extend) your Vehicle class?

    In which case, you're missing the relationship between those classes:

    Code:
    // No relationship here..!
    public class SecondHandVehicle
    To create the 'SecondHandVehicle-IS-A-Vehicle' relationship, you need to use the extends keyword:

    Code:
    // OK!  SecondHandVehicle now has an IS-A relationship to Vehicle using inheritance.
    public class SecondHandVehicle extends Vehicle
    But, in your code, this will cause another error, because creating an inheritance relationship means that the SecondHandVehicle class needs to defer to the Vehicle's constructor.
    Code:
    // Nope!  This won't work now...
         public SecondHandVehicle(String Registration, String Make, int Manufacture, double value, int numberOfOwners) { 
    }
    So, similar to the post above, you need to tell SecondHandVehicle's constructor how to handle its Vehicle parent:
    Code:
    // OK!  The constructor is using the 'super' keyword to defer to the Vehicle constructor
         public SecondHandVehicle(String Registration, String Make, int Manufacture, double value, int numberOfOwners) {
        super(Registration, Make, Manufacture, value); 
    }
    This fixes the new error and completes the SecondHandVehicle -> Vehicle relationship. SecondHandVehicle now really "IS A" Vehicle, and its constructor defers to the Vehicle constructor using the 'super' keyword (it's called "super" because a base-class is often known as a "superclass". Conversely, a derived-class is often known as a "subclass").

    ...Back to the issue of printing out your myCar data...

    Your myCar object, being based on the SecondHandVehicle class (a class which now inherits from Vehicle), has got a whole load of 'accessor' methods (methods whose name begins with "get" and whose purpose is just to access the data stored in that object).

    So, from the Vehicle base class (superclass), you've got all of these:
    Code:
    public String getRegNumber() { 
        return regNumber; 
    }
    
    public String getMake() { 
        return make; 
    } 
    
    public int getYear() { 
                return yearMade; 
    } 
    
    public double getValue() { 
        return value; 
    }
    Of course, your myCar object is a SecondHandVehicle - but ths is OK because the new IS-A relationship between Vehicle and SecondHandVehicle now means that anything a Vehicle might be able to do, a SecondHandVehicle is also able to do (because SecondHandVehicle is just an extension of Vehicle..)

    So if you want to use any of those Vehicle accessor methods, then you just need to call the accessor with a dot . after your myCar reference - for example:
    Code:
    String myCarMake = myCar.getMake();
    double myCarValue = myCar.getValue();
    If you want to build a String using those, then you could use System.out.println, and it would work, but a more "Java" way of doing that would be to use System.out.printf instead

    Note - 'printf' is usually described as "print formatted" - where the term 'format' means the conversion of raw data into some human-readable text representation - you don't need to use it, but it's a more convenient way of mixing up data with other strings:

    Something like this perhaps:
    Code:
    System.out.printf("myCar is a %s with registration %s, manufactured in %d, worth £%.2f ", 
        myCar.getMake(), myCar.getRegNumber(), myCar.getYear(), myCar.getValue());
    The first argument to the printf function is the 'format string' - it contains the text and a bunch of place-holders using a % character. All the other arguments are the data to use for those place-holders.

    The format string with its various %s, %d and %f "format specifiers" might look a bit strange, but those are just like magic wildcards which printf uses to interpret the various arguments into the string - i.e. %s means "string", %d means "integer", %f means "floating point" (and %.2f means "floating point with two decimal-places")

    For some reason, Java's documentation doesn't mention printf - it mentions the print and format methods - but printf is really just a combination of these:
    https://docs.oracle.com/javase/tutor...ormatting.html
    https://docs.oracle.com/javase/tutor...berformat.html


    Don't worry if you're struggling. The best way to learn is to keep trying, and then search through books, webpages, Q+A sites, tutorials, documentation, etc. if you get stuck. Then if you really get stuck, you can always ask for help (Your tutor, on StackOverflow, on here, or anywhere else..)

    Are you learning from any particular text books as part of your course? If your tutor hasn't recommended one to you, then have a look at Head First Java:
    http://www.headfirstlabs.com/books/hfjava/

    There's always the official Java documentation if you need to find out how to use particular features of Java:
    https://docs.oracle.com/javase/tutorial/java/index.html

    Otherwise, have a look at some of these online courses:
    https://www.codecademy.com/learn/learn-java
    https://www.sololearn.com/Course/Java/
    https://www.udemy.com/java-tutorial/

    But ultimately, it comes down to practice. The best way to learn is to try to solve problems by yourself - when you get a compiler error, put that error into google and see what you find - you'll probably see people on StackOverflow who have had the same error before. Learning to understand compiler errors will also help you learn the language.
 
 
 
Poll
Cats or dogs?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.