The Artima Developer Community
Sponsored Link

Java Answers Forum
java newbie hell

6 replies on 1 page. Most recent reply: Feb 18, 2006 6:54 AM by z h

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
joe auerbach

Posts: 14
Nickname: anivair
Registered: Feb, 2006

java newbie hell Posted: Feb 14, 2006 4:34 PM
Reply to this message Reply
Advertisement
All right, here's an basic java assignment that is a pain in my butt. Since it's an assignment, i'm hoping someone can tell me why this isn't working.

I'll just drop one example. We were asked to take a basic class that finds the volume of a shape (sphere) based on user input for the various doubles (radius, for example).

I think i've go thte volume section done just fine, no problem.

The problem is that we are also supposed to include a color for each shape. the color defaults to a preset one (Silver for a sphere) and the program contains the methods getColor and setColor to change and retreive the color. We are not supposed to have to import java.awk.Color to do so. The color is just a string.

I think I'm setting up the basic color ok, but the getColor isn't compiling and i can't tell whether I'm doing this right or not. I don't know why I'm having so much trouble with this.

::Begin Code::

import java.util.Scanner;

//a sphere
public class Sphere
{
private double radius;


//constructors
public Sphere(double rad)
{
radius = rad;
}

//determine volume
public double getVolume()
{
double volume = ((4) * Math.PI * (radius * radius * radius)) / 3;
return volume;
}

//set color
public void setColor(String newColor)
{
newColor = "Silver";
}

//retreive current color
public String getColor()
{
return newColor;
}

//volume calculation
public static void main(String [] args)
{
//Setup a Scanner object to read from the keyboard
Scanner sc = new Scanner(System.in);

System.out.println("This program calculates the volume of a sphere.");
System.out.print("Please enter the radius:");
double radius = sc.nextDouble();

Sphere s = new Sphere(radius);
double volume = s.getVolume();
System.out.println("The volume of your sphere is " + volume);

}
}

::end code::


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: java newbie hell Posted: Feb 14, 2006 10:46 PM
Reply to this message Reply
1. Reformat your post using the java tags as shown on the right of the inpuit field.

2. getColor has no access to newColor, since newColor is only declared (and therefore visible) inside setColor.

By the way, setColor is implemented completely wrong.

What you teacher expects you to do, is to create a String class variable. setColor must assign to this variable the value of newColor. getColor returns the value of this class variable.

What you did in setColor is to change the value of the input variable.

joe auerbach

Posts: 14
Nickname: anivair
Registered: Feb, 2006

Re: java newbie hell Posted: Feb 15, 2006 4:15 AM
Reply to this message Reply
It's distressing to hear that it's implemented wrong since that was part of the lab data the professor provided. hmm.

Sp of getColor has no access to newColor (since it's only implemented in the setColor method) how can I employ getColor to retreive the new color that is created in the setColor method. Bear in mind that the two methods are set up as we received them. I could do it pretty easily with just string variables that didn't include the getColor and setColor methods, or if we were supposed to get such input from the user later in hte program, but For hte life of me I can't figure out how ot make these two interact.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: java newbie hell Posted: Feb 15, 2006 4:37 AM
Reply to this message Reply
It was not just wrong, it was completely wrong.

Try to immagine this:

The setColor method is supposed to set the color of the Sphere.
Instead it did absolutely nothing. In fact, Sphere does not even have (= store the informations for) a color.

So your teacher made a mistake. Maybe it even was the scope that you find it?



setColor and getColor DO make sense.

1. public variables should be avoided. Instead get... and set... methods should be used.

2. If you want to do it right, don't use this syntax:
localColor = newColor;
but use
localColor = new String(myColor);


The second line creates a COPY of the String contained in newColor, the first line only redirects myString to the current content of newString.
This way, if someone modifies single characters of the String, localColor won't be affected.
The getColor method should also return a COPY of myColor's content.

joe auerbach

Posts: 14
Nickname: anivair
Registered: Feb, 2006

Re: java newbie hell Posted: Feb 15, 2006 7:05 AM
Reply to this message Reply
Here's what I ended up with.

is the class I set public String color.

Then in the setColor method I have

newColor = "Silver";
color = newColor;

Then in hte getColor method I have

return color;

I think, though, that I might actually need to set hte color to silver when I create the string and reset it to silver later (allowing that it could be reset away from silver using the setColor method).

Does that sound right?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: java newbie hell Posted: Feb 15, 2006 10:28 PM
Reply to this message Reply
remove newColor = "Silver"
Because this way it doensn't matter which color the user sets, it always results in "Silver".

Ok, you obviously tried, so I can give you the correct solution. You seem to be very near anyway.

public class Sphere {
.
.
.
    /** variable to store the sphere's color */
    private String color;
 
    /** Sets the color of the sphere
     * @param newColor the color information to be set
     */
    public void setColor(String newColor) {
        color = newColor;
        //color = new String(newColor);//alternative to separate the values
    }
 
    /** Retreives the current color
     * @return the Sphere object's color in String format
     */
    public String getColor() {
        return color;
        //return new String(color);//alternative to separate the values
    }
.
.
.
 
}

z h

Posts: 1
Nickname: zzzzzz
Registered: Feb, 2006

Re: java newbie hell Posted: Feb 18, 2006 6:54 AM
Reply to this message Reply
I am having problems writing test case for the setColor method. I would appreciate any help, iamalwaysgonnabe@mindless.com

Flat View: This topic has 6 replies on 1 page
Topic: chat server Previous Topic   Next Topic Topic: Java Newbie

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use