Go Back   Java Programming Forums > Java Standard Edition Programming Help > Object Oriented Programming


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-12-2009, 06:34 AM
Junior Member
 

Join Date: Dec 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Dark_Shadow is on a distinguished road
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):

Java Code
private String name;
private int type;
private String origin;
private double cost;

final static int red = 1, white = 2, pink = 3;
Method toString
Java 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!




Last edited by Dark_Shadow; 06-12-2009 at 09:13 PM. Reason: I solved my problem. Thanks!
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 06-12-2009, 07:00 AM
chronoz13's Avatar
Java kindergarten
 

Join Date: Mar 2009
Location: Squatters Mansion
Posts: 405
Thanks: 113
Thanked 21 Times in 21 Posts
chronoz13 is on a distinguished road

I'm feeling Stressed
Default Re: Help for java homework

can you please elaborate more of what do you need in your program?
Reply With Quote
  #3 (permalink)  
Old 06-12-2009, 07:13 AM
Freaky Chris's Avatar
Senile Half-Wit
 

Join Date: Mar 2009
Location: UK
Posts: 621
Thanks: 2
Thanked 64 Times in 60 Posts
Freaky Chris will become famous soon enoughFreaky Chris will become famous soon enoughFreaky Chris will become famous soon enough

I'm feeling Cheerful
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

University of Wales, Bangor
Reply With Quote
  #4 (permalink)  
Old 06-12-2009, 08:19 AM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,188
Thanks: 57
Thanked 137 Times in 133 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
Default Re: Help for java homework

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

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

Java 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
Reply With Quote
  #5 (permalink)  
Old 06-12-2009, 05:35 PM
Junior Member
 

Join Date: Dec 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Dark_Shadow is on a distinguished road
Default Re: Help for java homework

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

So I did a switch...

Java 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
Java 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...
Reply With Quote
  #6 (permalink)  
Old 06-12-2009, 06:39 PM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,188
Thanks: 57
Thanked 137 Times in 133 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
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
Reply With Quote
  #7 (permalink)  
Old 07-12-2009, 08:08 AM
chronoz13's Avatar
Java kindergarten
 

Join Date: Mar 2009
Location: Squatters Mansion
Posts: 405
Thanks: 113
Thanked 21 Times in 21 Posts
chronoz13 is on a distinguished road

I'm feeling Stressed
Default 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..
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
MORE java homework help. TommyFiz File I/O & Other I/O Streams 10 14-10-2009 12:15 AM
Homework help cd247 What's Wrong With My Code? 2 11-10-2009 10:56 PM
need help with homework! programmer12345 Java Theory & Questions 2 27-09-2009 10:34 AM
Homework - using 'IF' for 'For Loops' Enzo Loops & Control Statements 5 16-09-2009 03:52 PM
[SOLVED] Need A Little Homework Help Please napenthia Java Theory & Questions 11 27-04-2009 11:11 AM


100 most searched terms
Search Cloud
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html java 2d arraylist java actionlistener java addactionlistener java double format java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programmers forum java programming forum java programming forums java programming help java project ideas java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextarea font color jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 11:53 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.