1
\$\begingroup\$

The Helloworld class executes all of the methods. Human is the interface, and Nate & Ryan are two classes that implement the Human interface. Are there any deficiencies or faults that my code contains that could help me in the long run?

public class Helloworld{
 public static void main(String[] args) {
 Human ryan = new Ryan();
 ryan.setHeight(12);
 System.out.println("Ryan's height is " + ryan.getHeight());
 Human nate = new Nate();
 nate.setHeight(32);
 System.out.println("Nate's height is " + nate.getHeight());
 nate.hit(ryan);
 /*
 * OUTPUT --------------------------------------
 * 
 * Ryan's height is 12
 * Nate's height is 32
 * Nate has hit Ryan
 * 
 */
 }
}
interface Human{
 public void sleep();
 public void eat();
 public void wakeUp();
 public void walk();
 public void hit(Human name);
 public void setHeight(int height);
 public int getHeight();
 public String getName();
}
class Nate implements Human{
 int height = 0;
 final String name = "Nate";
 public String getName() {
 return this.name;
 }
 public void setHeight(int height) {
 if(height > 0) {
 this.height = height;
 }else {
 System.out.println("You can't set Nate's height to less than 0.");
 }
 }
 public int getHeight() {
 return this.height;
 }
 public void sleep() {
 System.out.println("Nate has fallen asleep");
 }
 public void eat() {
 System.out.println("Nate has eaten");
 }
 public void wakeUp() {
 System.out.println("Nate has woken up");
 }
 public void walk() {
 System.out.println("Nate is walking");
 }
 public void hit(Human name) {
 System.out.println("Nate has hit " + name.getName());
 }
}
class Ryan implements Human{
 int height = 0;
 final String name = "Ryan";
 public String getName() {
 return this.name;
 }
 public void setHeight(int height) {
 if(height > 0) {
 this.height = height;
 }else {
 System.out.println("You can't set Ryan's height to less than 0.");
 }
 }
 public int getHeight() {
 return this.height;
 }
 public void sleep() {
 System.out.println("Ryan has fallen asleep");
 }
 public void eat() {
 System.out.println("Ryan has eaten");
 }
 public void wakeUp() {
 System.out.println("Ryan has woken up");
 }
 public void walk() {
 System.out.println("Ryan is walking");
 }
 public void hit(Human name) {
 System.out.println("Ryan has hit " + name.getName());
 }
}
t3chb0t
44.6k9 gold badges84 silver badges190 bronze badges
asked Aug 6, 2018 at 4:50
\$\endgroup\$
1
  • 2
    \$\begingroup\$ From what data you are storing, Nate and Ryan should be objects of Human class, they shouldn't be two different classes violating DRY principle. \$\endgroup\$ Commented Aug 6, 2018 at 6:27

3 Answers 3

1
\$\begingroup\$

What is the purpose of your code ? Without clear intentions it is harder to review it because many decisions may be taken regarding a context that we don't have.

From what I see, both Ryan and Nate can be two instances of the same Human class. The thing that vary are the name and height, so there is two variables required to create an Human. Since they are required, you should add them as constructor parameters so that once your instance is created, it is ready to be used.

If you want to control your instances, be sure that only Ryan and Nate are created, you can use a factory of factory method.

Interfaces with different implementations are used when the behavior is different. In your case, there is no need to have those two implementations, they complexify your code were it is not needed (see K.I.S.S.) and they duplicate the "logic". Never try to use patterns or style when this is not necessary.

Do you know the meaning of S.O.L.I.D. ? Take a look at it and specially the Liskov part. You can also search for _K.I.S.S. and Y.A.G.N.I. they are all useful.

answered Aug 6, 2018 at 6:30
\$\endgroup\$
2
  • \$\begingroup\$ This purpose of my code was to demonstrate my knowledge of interfaces and to ask you guys if it was efficient enough, where Ryan and Nate had methods that did the exact same things. Also, will look further into "S.O.L.I.D." and etc etc because I've NEVER heard of those before. \$\endgroup\$ Commented Aug 6, 2018 at 15:53
  • \$\begingroup\$ Happy to read that you learned from my answer. Accept it or another if you think your question is answered. \$\endgroup\$ Commented Aug 6, 2018 at 21:16
1
\$\begingroup\$
  1. A class is a blueprint or template or set of instructions to build a specific type of object. Every object is built from a class.

  2. An instance is a specific object built from a specific class. It is assigned to a reference variable that is used to access all of the instance's properties and methods.

Ryan and nate should both be an instance. They have the same properties. From the 2 classes you can extract one template and make it a Human class.

What you are doing now is hard coding their properties into the methods. Try to make it more dynamic by creating one class.

public void eat() {
 System.out.println("Nate has eaten");
}

Can then be tranformed into:

public void eat() {
 System.out.println(this.name + " has eaten");
}
answered Aug 6, 2018 at 7:56
\$\endgroup\$
-3
\$\begingroup\$

To get started you can use this as reference.

class Human {
 private int height;
 private String name;
 public Human(String name, int height) {
 this.name = name;
 this.height = height;
 }
 public Human(String name) {
 this(name, 0);
 }
 public String getName() {
 return name;
 }
 public void setHeight(int height) {
 if (height > 0) {
 this.height = height;
 } else {
 System.out.println("You can't set " + this.getName() + "'s height to less than 0.");
 }
 }
 public int getHeight() {
 return this.height;
 }
 public void sleep() {
 System.out.println(this.getName() + " has fallen asleep");
 }
 public void eat() {
 System.out.println(this.getName() + " has eaten");
 }
 public void wakeUp() {
 System.out.println(this.getName() + " has woken up");
 }
 public void walk() {
 System.out.println(this.getName() + " is walking");
 }
 public void hit(Human name) {
 System.out.println(this.getName() + " has hit " + name.getName());
 }
}

You can construct objects like this.

Human Ryan = new Human("Ryan");

Humane Nate = new Human("Nate");

PS: Solution may not be exact, but hope you got the idea.

answered Aug 6, 2018 at 6:37
\$\endgroup\$
1
  • 2
    \$\begingroup\$ You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process. You got the start of a decent answer down, but it's lacking the most important part. \$\endgroup\$ Commented Aug 6, 2018 at 6:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.