Alright basically I have an assignment due at 11:59 pm tonight and so I would appreciate all the help I can get. I have code that I wish for those to look over and see if its correct I also have a question about some of the questions and whether or not I interpreted them correctly. Also an example on how to do the last part of my assignment would be helpful.

Assignment:

1. Each instance of a Doodad has a color(String), a weight(int) and a boolean variable to indicate if it has been refurbished as private instance variables.

2. Use modifiers: setColor, setWeight, setRefurbished, and accessors: getColor, getWeight, getRefurbished.

3. There should be two constructors: one with the signature: (String, int, boolean); the other with no parameters which then uses the default values("red", 10, false)

4. Write a toString method that creates a string consisting of exactly the color on the first line, the weight on the second and the boolean on the third line(use no additional formatting or labeling). Each of the three lines ends with a newline character.

5.(example of something similar please) Include a public method named isHeavy which should return true if the Doodad has a weight greater than the weight passed to isHeavy as a parameter. If no parameter is passed, true is returned if the weight is greater than 12(use method overloading).

public class Doodad {
  // Data Fields
   /** The Color */
  private String Color;

   /** The Weight */
  private int Weight;
  
  /** The Refurbished */
  private boolean Refurbished;

  //Modifier Methods
  /** Sets the Color field.
       @param colors The Color
    */
  public void setColor(String colors) {
     Color = colors;
   }
   /** Sets the Weight field.
       @param theweight The Weight
    */
   public void setWeight(int theweight) {
     Weight = theweight;
   }
   /** Sets the Refurbished field.
       @param refurbish The Refurbished
    */
   public void setRefurbished(boolean refurbish) {
     Refurbished = refurbish;
   }
 
   // Accessor Methods
   /** Gets the doodads color.
       @return the color as a String
    */
   public String getColor() {
     return Color;
   }
   /** Gets the doodads weight.
       @return the weight as a int
    */
   public int getWeight() {
     return Weight;
   }
   /** Gets the doodads refurbished.
       @return the refurbished as a boolean
    */
   public boolean getRefurbished() {
     return Refurbished;
   }

   // Constructors
   /** Construct a Doodad with given values
       @param colors The Color
       @param theweight The Weight
       @param refurbish The Refurbished
      */
   public Doodad(String colors, int theweight, boolean refurbish) {
     Color = colors;
     Weight = theweight;
     Refurbished = refurbish;
   }

   /** Construct a doodad with nothing specefied.
       @param No parameter
    */
   public Doodad() {
   }

   /** Retrieves the information in a Person object.
       @return the object state as a string
    */
   public String toString() {
     return "The Colors: " + Color + "\n"
         + "The weight: " + Weight + "\n"
         + "boolean: " + boolean + "\n";
   }
   
   public void isHeavy(){

So now my questions for the very first part of the assignment is the boolean suppose to be Refurbished or not? I'm confused about that.

Second question: third part of the assignment is the first construct suppose to look like this: (String colors, int theweight, boolean refurbish) or this: (String, int, boolean)

Third question: fourth part of the assignment is the toString suppose to look like this:

public String toString() {
return "The Colors: " + Color + "\n"
+ "The weight: " + Weight + "\n"
+ "boolean: " + boolean + "\n";

or like this:

public String toString() {
return "The Colors: " + Color + "\n"
+ "The weight: " + Weight + "\n"
+ "boolean: " + Refurbished + "\n";

Last question of course deals with the very last part of the assignment I'm basically confused, I get the feeling I'm suppose to use a boolean:

Like this:

public boolean equals(Doodad doo){
if (Doodad> theweight)
return true;
else
(Doodad>12)
return true;

I don't know.

Thanks for the help and answering my questions.

Recommended Answers

All 11 Replies

You have some problems with your "toString" function:

public String toString() {
     return "The Colors: " + Color + "\n"
         + "The weight: " + Weight + "\n"
         + "boolean: " + boolean + "\n";
   }

One, you are using the word "Color" which has meaning in Java, so be careful that you make sure that you don't mix it up with the Color class from Java. You're probably fine but just keep that in mind if you get some error:

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Color.html

Consider refering to Color as:

this.Color

That may make things slightly less confusing.

You can't just add "Weight" and "boolean" since those aren't strings. Adding "boolean" in particular makes no sense since it's not even the name of a variable. Using "Refurbished" instead of "boolean" would make slightly more sense, but still would be incorrect. I imagine you need something like this inside toString:

String stringToReturn;
.
.
.

if (Refurbished)
     stringToReturn = stringToReturn + "TRUE";
else
     stringToReturn = stringToReturn + "FALSE";

.
.
.
return stringToReturn;

For the Weight variable, since Weight is an integer, you can use the toString function from the Integer class.

Ok, because I was going to say that it compiles with no problems so I wasn't sure. By that I mean having the:

public String toString() { return "The Colors: " + Color + "\n" + "The weight: " + Weight + "\n" + "boolean: " + refurbished + "\n"; }

Well this is what I came up with so far for my very last part of the assignment or question if you will. Here is everything that I have so far as code. Hoping those could review the very last part.

public class Doodad {
  // Data Fields
   /** The Color */
  private String Color;

   /** The Weight */
  private int Weight;
  
  /** The Refurbished */
  private boolean Refurbished;

  //Modifier Methods
  /** Sets the Color field.
       @param colors The Color
    */
  public void setColor(String colors) {
     Color = colors;
   }
   /** Sets the Weight field.
       @param theweight The Weight
    */
   public void setWeight(int theweight) {
     Weight = theweight;
   }
   /** Sets the Refurbished field.
       @param refurbish The Refurbished
    */
   public void setRefurbished(boolean refurbish) {
     Refurbished = refurbish;
   }
 
   // Accessor Methods
   /** Gets the doodads color.
       @return the color as a String
    */
   public String getColor() {
     return Color;
   }
   /** Gets the doodads weight.
       @return the weight as a int
    */
   public int getWeight() {
     return Weight;
   }
   /** Gets the doodads refurbished.
       @return the refurbished as a boolean
    */
   public boolean getRefurbished() {
     return Refurbished;
   }

   // Constructors
   /** Construct a person with given values
       @param colors The Color
       @param theweight The Weight
       @param refurbish The Refurbished
      */
   public Doodad(String colors, int theweight, boolean refurbish) {
     Color = colors;
     Weight = theweight;
     Refurbished = refurbish;
   }

   /** Construct a doodad with nothing specefied.
       @param No parameter
    */
   public Doodad() {
   }

   /** Retrieves the information in a Person object.
       @return the object state as a string
    */
   public String toString() {
     return "The Colors: " + Color + "\n"
         + "The weight: " + Weight + "\n"
         + "Refurbished: " + Refurbished + "\n";
   }
   
   public void isHeavy(String colors, int theweight, boolean refurbish){
     this.Weight = theweight;
     this.Color = colors;
     this.Refurbished = refurbish;
     
   }
   public void isHeavy(){
     this.Weight = 13;
     this.Color = "";
     this.Refurbished = true;
     
   }
 }

Ok, because I was going to say that it compiles with no problems so I wasn't sure. By that I mean having the:

public String toString() { return "The Colors: " + Color + "\n" + "The weight: " + Weight + "\n" + "boolean: " + refurbished + "\n"; }

It may compile but it may give you bizarre results. Does it give you what you want? If you don't use the Integer.toString method and add 8 to a string, it may do something strange. I'm actually surprised that it did compile, JAVA being so picky about type mismatches.

Ya I know that is what I hate is that it compiles but you really don't know that its going to do what you want. Like my book and my teacher says, a program that runs but gives the wrong answer is completely worthless even though it runs.

Here is the thing I'm trying right now to write a testfile but that so far is a work in progress lol.

Your isHeavy functions should return true or false and thus need to be boolean, not void. Also, in this function:

public void isHeavy(String colors, int theweight, boolean refurbish)

I think you are supposed to pass it just an integer, like this:

public boolean isHeavy (int aWeight)

At least that's how I interpret this line. Maybe there is more than one interpretation:

5.(example of something similar please) Include a public method named isHeavy which should return true if the Doodad has a weight greater than the weight passed to isHeavy as a parameter. If no parameter is passed, true is returned if the weight is greater than 12(use method overloading).

See bold words above. Weight is an integer. Just my interpretation.

But then I don't believe you are using methodoverloading like it says to use.

But then I don't believe you are using methodoverloading like it says to use.

You still have two functions with the same name that take two different sets of parameters. Hence isHeavy is overloaded:

boolean isHeavy ()    // first function
boolean isHeavy (int aWeight)   // second function

How is that different then what I have? theweight/Weight is still an int.

Hey how do I deal again with a .class expected error again?

How is that different then what I have? theweight/Weight is still an int.

Hey how do I deal again with a .class expected error again?

You have this:

public void isHeavy(String colors, int theweight, boolean refurbish)

You are passing it three parameters and it returns a void. I have this:

boolean isHeavy (int aWeight)

I'm only passing one parameter and returning a boolean. The fact that you called the integer theweight and I called it aWeight is irrelevant. I was actually trying to match your name and I got it slightly wrong. The name that you assign the parameters is irrelevant in terms of overloading. It's the parameter list that matters. Our functions both do overloading. I just think mine matched the wording of the spec better.

You are also using your function as a "set" function:

public void isHeavy(String colors, int theweight, boolean refurbish){
     this.Weight = theweight;
     this.Color = colors;
     this.Refurbished = refurbish;
     
   }

You don't want to do that. You don't want to change anything in this function. All it does is do a comparison and returns true or false based on that comparison. No values are changed. If you want to change values, write another set function. The isHeavy specification does not require you to change anything.

Not sure what error you are referring to.

Ya I fixed my error. Don't worry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.