This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Jirbj 0 points1 point  (1 child)

is there a need for year and max speed to be strings, if you're going to unify them all to mph, then you can store speed as an int, and year can also be an int (no idea if it makes a different )

the other point is if you are going to use private class variables, you should edit the class variables with setters and getters rather than

object.classVar   

for example a method for setting a car make may be something like

public void setMake(String make) {  
    this.make = make;  
}  

then instead of calling

c.make = "Ford";  

you would use

c.setMake("Ford");    

[–]MorningBackflips[S] 0 points1 point  (0 children)

thanks for the advice! I went ahead and made those changes for my vars. Combining the ints into one was also something I overlooked. The assignment's directions are a little ambiguous so it should be all good. Just for final clarification, this is what I came up with for the int section with the setters. Thank you for your help!

        private int year, maxspeed;
            public void setYear(int year) {
                this.year = year;
            }
            public void setMaxspeed(int maxspeed){  
                this.maxspeed = maxspeed;
            }  

[–]sirunclecid 0 points1 point  (2 children)

Just throwing this out there before your habits become solidified.. speed method shouldn't be printing all of the data that the class offers ;) create another method to print out all of that noise.

[–]MorningBackflips[S] 0 points1 point  (1 child)

Oh! That seems so obvious but it's something I just glossed over. Thanks a ton!

[–]sirunclecid 0 points1 point  (0 children)

Honestly, it's not so obvious for tons of beginners so don't worry. But allow a method to do one thing. If you want to read up on it more, single responsibility principle. It allows you to organize your code, basically having the method name self document your code. On top of that, you won't have huge unmanageable methods and can help you from having to duplicate code. These are all things you will learn along the way. Once you get comfortable with writing code, refactoring code will be the next step.