Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Help for java homework

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [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):

    private String name;
    private int type;
    private String origin;
    private double cost;
     
    final static int red = 1, white = 2, pink = 3;

    Method toString
    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!
    Last edited by Dark_Shadow; December 6th, 2009 at 05:13 PM. Reason: I solved my problem. Thanks!


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Help for java homework

    can you please elaborate more of what do you need in your program?

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default 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
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Help for java homework

    Or even better, instead of having static ints represent the type of wine, use an enum

        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.

    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

  5. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help for java homework

    I don't like java... C programming was much easier for me. XD

    So I did a switch...

    	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
    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...

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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

  7. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Help for java homework

    I don't like java... C programming was much easier for me. XD
    maybe its better if you ask your problem in C forums..

Similar Threads

  1. MORE java homework help.
    By TommyFiz in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: October 13th, 2009, 08:15 PM
  2. Homework help
    By cd247 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2009, 06:56 PM
  3. need help with homework!
    By programmer12345 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 27th, 2009, 06:34 AM
  4. Homework - using 'IF' for 'For Loops'
    By Enzo in forum Loops & Control Statements
    Replies: 5
    Last Post: September 16th, 2009, 11:52 AM
  5. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 07:11 AM