Question:
Need help with a Java programming problem.?
Anthro Fan #1
2014-02-09 15:10:36 UTC
Okay, I'm going to be honest here, it's been a while since I last took a Java class and now I need to do an advanced version of that class...which sucks considering I've pretty much forgotten everything I've learned in the basic class. I need help getting back into this. Here's what I need to do for my current assignment. I need to create a class called Horse that contains data fields for the name, color, and birth year. I need to include get and set methods for those fields. Next I need to create a subclass named RaceHorse, which contains an additional field that holds the number of races in which the horse has competed and additional methods to get and set the new field. Then I need to write an application that demonstrates using objects of each class.

I'm resorting to this because I'm pretty much completely clueless as to how to start this, let alone how to complete it. Coding isn't my thing but I cannot fail this course or I may lose my grant, and I literally cannot afford to lose it in this economy.

Please guys, I need help here.
Six answers:
david
2014-02-09 16:25:28 UTC
Half of coding is the code - and half is knowing what the code should be.



Name is obviously a string.



Year should be an integer - years can only be whole numbers. Nobody says their horse was born in the year 2011.75.



Color, in your case, should be a string. If you said the color was 42 nobody would know what you were talking about. (It's possible to represent a color with numbers - RGB format, for example - but in this case they just want to know the color of the horse, like "chestnut" or "white".)



You could make them protected if you wanted to. The question is, would a class like RaceHorse which inherits from Horse need to use those variables without going through the getter/setter methods? If the answer is yes, then make them protected. If the answer is no, they should be private.
Cat On Fire
2014-02-09 17:30:24 UTC
The nice thing with Java is it makes stuff like this virtually hundreds of times easier than C++ does.

To declare a class, just simply include the code



public class {

}



For example, a Horse class would be put into a file called Horse.java and would be declared in the code as



public class Horse {

}



For whatever reason, Java does not like it when the public class has a different name than the file.



If you want one class to inheret another, you would code the class inhereting the second class as



public class extends {

}



So if Racehorse is the sub-class, it would be



public class Racehorse extends Horse {

}



As for the types of data to use, it's best to think of which is a more natural way to express the data you need to express. For example we don't think of the color blue on the rainbow as the 8th color (or whatever number you'd assign to it, I just picked that at random), we think of it as "blue." So I'd recommend using a string for the name and color, and an int for birth year and number of races.



Eventually when you get into more advanced stuff you'll have to worry about other types of data, but for most applications int, boolean, and string are the most important to remember.



And I'll spare the explanation on the get/set methods because it looks like those have already been explained in great detail
brilliant_moves
2014-02-09 16:05:33 UTC
Here's a solution for the Horse class.

The RaceHorse class extends Horse, with an extra field, and setter an getter methods for this field.

Then just write an application with a main method, to test both Horse and RaceHorse. Good luck!



public class Horse {



private String name;

private String color;

private int birthYear;



public void setName(String name) {

this.name = name;

} // end setName()



public String getName() {

return name;

} // end getName()



public void setColor(String color) {

this.color = color;

} // end setColor()



public String getColor() {

return color;

} // end getColor()



public void setBirthYear(int birthYear) {

this.birthYear = birthYear;

} // end setBirthYear()



public int getBirthYear() {

return birthYear;

} // end getBirthYear()

} // end class Horse
Bob
2014-02-09 15:17:05 UTC
Just to get you started:



public class Horse {

private String name;

public String getName () {

return name;

}



...etc...

}



public class RaceHorse extends Horse {



... you fill in the rest...



}
taminglis
2014-02-09 15:17:56 UTC
Google search (or yahoo ;-)) the words "java inheritance" and all will be revealed
2016-03-10 03:52:22 UTC
ummmm whats ur question?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...