-
Notifications
You must be signed in to change notification settings - Fork 1.5k
completed tasks #1555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
completed tasks #1555
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job on defining the Color
enum according to Java conventions! 🎉 While there are some suggestions for improvements, none of them are critical issues that would require a change request. Keep up the good work, and consider the feedback to enhance your code for the future. 👍 Remember, using static fields for shared resources and creating instances for non-static methods can help make your code more efficient and object-oriented. Happy coding!
|
||
public class Ball { | ||
private int number; | ||
private String color; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The color field should be of the enum type Color, not String, to enforce the use of predefined color values.
return number; | ||
} | ||
|
||
@Override |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The toString() method should return a string that represents the entire Ball object, not just the color. Consider including both the number and the color in the returned string.
for (int i = 0; i < 3; i++) { | ||
System.out.println(Lottery.getRandomBall()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use static methods in your solution. Instead of calling a static method on the Lottery class, you should create an instance of the Lottery class and call the method on that instance.
|
||
public class Application { | ||
public static void main(String[] args) { | ||
// create three balls using class Lottery and print information about them in console |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't add redundant empty lines to your code. There's no need for an empty line after the main method signature.
public static String getRandomColor() { | ||
int index = new Random().nextInt(Color.values().length); | ||
Ball ball = new Ball(); | ||
ball.setColor(Color.values()[index].toString()); | ||
return ball.getColor(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid using static methods as they are generally considered bad practice in object-oriented programming. You should create an instance of ColorSupplier
in the class where you want to use it.
return null; | ||
public static String getRandomColor() { | ||
int index = new Random().nextInt(Color.values().length); | ||
Ball ball = new Ball(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unnecessary to create a new instance of Ball
in this method. You can directly return the color as a string using Color.values()[index].name()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good advice
ball.setColor(Color.values()[index].toString()); | ||
return ball.getColor(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using toString()
, use name()
for getting the String representation of enum constants to avoid issues if toString()
is overridden in the enum.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not fixed
public class Lottery { | ||
public static String getRandomBall() { | ||
Ball ball = new Ball(); | ||
ball.setColor(ColorSupplier.getRandomColor()); | ||
ball.setNumber(new Random().nextInt(100)); | ||
return "Ball color is " + ball.getColor() + " and the number is " + ball.getNumber(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use static methods in your solution. Instead, create an instance of the class whose method you want to call. This will make your code more flexible and object-oriented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice hint
import java.util.Random; | ||
|
||
public class Lottery { | ||
public static String getRandomBall() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method name getRandomBall
suggests that it should return a Ball
object, but it currently returns a String
. Consider changing the return type to Ball
and moving the string representation to a toString
method in the Ball
class.
public static String getRandomBall() { | ||
Ball ball = new Ball(); | ||
ball.setColor(ColorSupplier.getRandomColor()); | ||
ball.setNumber(new Random().nextInt(100)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of creating a new instance of Random
every time the method is called, consider defining it as a private static final field in the class. This will be more efficient as you will reuse the same Random
instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not fixed
public String getRandomColor() { | ||
return null; | ||
public static String getRandomColor() { | ||
int index = new Random().nextInt(Color.values().length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not fixed
public class ColorSupplier { | ||
public String getRandomColor() { | ||
return null; | ||
public static String getRandomColor() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public static String getRandomColor() { | |
public String getRandomColor() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not fixed - we should avoid static methods, this will become evident in Spring section
ball.setColor(Color.values()[index].toString()); | ||
return ball.getColor(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not fixed
return null; | ||
public static String getRandomColor() { | ||
int index = new Random().nextInt(Color.values().length); | ||
Ball ball = new Ball(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good advice
ball.setColor(ColorSupplier.getRandomColor()); | ||
ball.setNumber(new Random().nextInt(100)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try to create ball using constructor
public static String getRandomBall() { | ||
Ball ball = new Ball(); | ||
ball.setColor(ColorSupplier.getRandomColor()); | ||
ball.setNumber(new Random().nextInt(100)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not fixed
public class Lottery { | ||
public static String getRandomBall() { | ||
Ball ball = new Ball(); | ||
ball.setColor(ColorSupplier.getRandomColor()); | ||
ball.setNumber(new Random().nextInt(100)); | ||
return "Ball color is " + ball.getColor() + " and the number is " + ball.getNumber(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice hint
private final int randomNum = new Random().nextInt(100); | ||
private final ColorSupplier colorSupplier = new ColorSupplier(); | ||
private final Ball ball = new Ball(randomNum, colorSupplier.getRandomColor()); | ||
|
||
public Ball getRandomBall() { | ||
return ball; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
100 should be a constant
Random should be a private field
random num should a local variable be generated in getRandomBall method. Same for Ball. Right now you only create one unique ball per each lottery instance - it doesn't fulfil our requirements, we want lottery to generate a new Ball every time the method is called
public String getRandomColor() { | ||
return null; | ||
public static String getRandomColor() { | ||
int index = new Random().nextInt(Color.values().length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for this Random, it should be a field
for (int i = 0; i < 3; i++) { | ||
Lottery lottery = new Lottery(); | ||
Ball ball = new Ball(lottery.getRandomBall()); | ||
System.out.println(ball.toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can remove .toString() from println, it's called under the hood anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, but it make up a sentence at once, is it bad?
I thought that I should use .toString() somewhere, or I just need to override it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I didn't really get your question :) You should override toString in the Ball.java class, but when you put something in system out println, .toString of this object will be called
import java.util.Random; | ||
|
||
public class Lottery { | ||
public static final int border = 100; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why public?
BORDER - user uppercase for constants
public class ColorSupplier { | ||
public String getRandomColor() { | ||
return null; | ||
public static String getRandomColor() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not fixed - we should avoid static methods, this will become evident in Spring section
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put ball package inside of core/basesyntax, everything should be under one root package
import java.util.Random; | ||
|
||
public class Lottery { | ||
static final int BORDER = 100; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static final int BORDER = 100; | |
private static final int BORDER = 100; |
public static void main(String[] args) { | ||
// create three balls using class Lottery and print information about them in console | ||
for (int i = 0; i < 3; i++) { | ||
Lottery lottery = new Lottery(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't create new lottery instance in a loop - do it outside of the loop
// create three balls using class Lottery and print information about them in console | ||
Lottery lottery = new Lottery(); | ||
|
||
for (int i = 0; i < 3; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 is a magic number, create constant
Ball ball = new Ball(lottery.getRandomBall()); | ||
System.out.println(ball); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ball ball = new Ball(lottery.getRandomBall()); | |
System.out.println(ball); | |
System.out.println(lottery.getRandomBall())); |
this.number = number; | ||
} | ||
|
||
public Ball(Ball ball) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove
public static void main(String[] args) { | ||
// create three balls using class Lottery and print information about them in console | ||
Lottery lottery = new Lottery(); | ||
int amountBalls = 3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not constant
make it constant
No description provided.