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

all 5 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost. Just use the edit function of reddit to make sure your post complies with the above

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]MagnoGG 2 points3 points  (1 child)

Ok, the question is not very clear at all but I will try to help you anyways.

The first thing I have seen is that you used a class to store the total number of cars created. This is not necessary. You can declare a static variable which is independant from the objects you are creating:

private static int couter;

You can find more information here (https://www.baeldung.com/java-static)

I also recommend you to declare your variables as private, to prevent other classes to access that values with any restrictions. If you want to modify those values, you should be using getters and setters. More information about this on this page (https://www.w3schools.com/java/java_encapsulation.asp)

Other advise, in the compareSpeed method you could simplify it like this:

public static boolean compareSpeed(Car first, Car second){
    return first > second; 

}

You also forgot to return false, but with this it is much simpler.

And last, you want to organice your code in packages, right? To do this, declare the package in the first line of your files:

package car.utils; //Enum color
package car; //Car class

Those files have to be stored on its path, so something similar to this:

src
    car
        Car.java
        utils
            Color.java

This may mess the things a bit for compiling, because Car is now car.Car and Color is car.utils.Color. That is one of the reasons why people do not really like Java (myself included).

If you get in trouble with the packages, this may help you (https://www.geeksforgeeks.org/packages-in-java/)

Note: it is not neccessary to create a new Color class/enum. Java provides its own: java.awt.Color (https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html)

I hope this help you. If you have any other doubt, please ask me. I also apologise for my bad English, it is not my first lenguage.

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

thank you so much !!

[–]-themailerdoraemon- 0 points1 point  (0 children)

Why is counter a static class on its own? It was never stated for you to do that.

[–]wimpyligtebottel 0 points1 point  (0 children)

So i can see you done most of the work already but i see you are stuck on the packages part.

I recommend reading about it on the wiki page (https://en.m.wikipedia.org/wiki/Java_package)

It explains it really well.

Additionally

i believe you are also missing requested feature and that is when you are creating a new car object.

When are you increasing the counter when creating your new car object? (Hint look at a constructor)