[SOLVED] Help for java homework
Hi, I'm having a little problem doing my homework which is due in less than 24 hours. XD
Here are the attributes (please pay attention to the constant values which are the types of wine):
Code :
private String name;
private int type;
private String origin;
private double cost;
final static int red = 1, white = 2, pink = 3;
Method toString
Code :
public String toString(){
return "\n\t" + getName() + " is a " + getType() + " wine from " + getOrigin() + " and it costs " + getCost() + "$.";
My problem: I need to create a private method to get the type of wine as a string. toString will be using this private method. Can anyone please help me? Thank you in advance!
Re: Help for java homework
can you please elaborate more of what do you need in your program?
Re: Help for java homework
Sounds to me like you want to put all your toString code into a private method, then just call the private method from toString().
Regards,
Chris
Re: Help for java homework
Or even better, instead of having static ints represent the type of wine, use an enum :)
Code :
public enum WineType {
RED,
WHITE,
PINK;
}
However if I read this question correct you want some help on implementing the getTypeAsString method I take it.
Code :
private String getTypeAsString() {
if type equals 1 return "red";
else if type equals 2 return "white";
else if type equals 3 return "pink";
else return "unknown";
}
You have to figure out how to write the code for the actual thing but that's more or less what you can do. You can of course use a switch as well.
// Json
Re: Help for java homework
I don't like java... C programming was much easier for me. XD
So I did a switch...
Code :
final static int red = 1;
final static int white = 2;
final static int pink = 3;
private String getTypeAsString() {
switch(type){
case red: return "red";
break;
case white: return "white";
break;
case pink: return "pink";
break;
}
}
Method toString
Code :
public String toString(){
return "\n\t" + getName() + " is a " + getType(getTypeAsString()) + " wine from " + getOrigin() + " and it costs " + getCost() + "$.";
JCreator LE is telling me getType() cannot be applied to java.lang.string -_- The switch would have worked in C programming...
Re: Help for java homework
Its probably because you're trying to pass the value of getTypeAsString into the getType method in your toString method.
// Json
Re: Help for java homework
Quote:
I don't like java... C programming was much easier for me. XD
maybe its better if you ask your problem in C forums..