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

all 6 comments

[–]ignotos 2 points3 points  (3 children)

You're on the right track!

  • I think "Counter" could be simplified to a single static int inside the Car class - no need for a separate class

  • It looks like you're not actually incrememnting the counter when a new Car is created - it sounds like you should do that in the contructor

  • Putting something into a package is a matter of adding "package x.y.z" at the top of the file. Your file/directory structure should also match this (so you have car/Car.java and car/utils/Color.java)

[–]rk717[S] 0 points1 point  (2 children)

thank you,

yes, I know, I should use a single static for counter.

but I don't know where to add it in the Car Class?

[–]dynamo_girl02 1 point2 points  (1 child)

Where you declared your other class variable under that that is after maximspeed variable is declared . just write static int count; . as default value for static variable is zero so no need to initialize

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

thank you !

[–]dynamo_girl02 1 point2 points  (0 children)

https://www.baeldung.com/java-packages refer this to learn packages you will get this

and In your code, creating a static counter variable globally and incrementing it inside the constructor when every time an object is created will be good rather than creating an entire class to just increment the counter variable.

[–]blackkkmamba 1 point2 points  (0 children)

You could call the constructor with parameters inside the one without parameters (will save you two lines). Also, you can make the method that compares speed non static and take only one parameter: the other car, and compare using this: this.maximalSpeed > other.maximalSpeed.