0

Ok so for class I have been working on some oblong questions based off an oblong class. The question I am having an issue with is to create a method to increase the height and width of the oblong by a user defined amount.

This is my main:

    import java.util.Scanner;

public class Oblong6
{
   public static void main(String [] args)
   {

      Oblong ob1 = new Oblong();

      Scanner keyboardIn = new Scanner(System.in);

      Oblong ob = new Oblong();

      double h, w, x;
      System.out.print ("Enter the height of the oblong:");
      h = keyboardIn.nextDouble();

      System.out.print ("Enter the width of the oblong:");
      w = keyboardIn.nextDouble();

      System.out.print (" Enter the amount to increment by ");
      x = keyboardIn.nextInt();

      ob.setHeight(h);
      ob.setWidth(w);
      ob.setX(x);

      System.out.println (ob.incHeight());
      System.out.println (ob.incWidth());

      System.out.println("Height " + h);
      System.out.println("Width " + w);


   }
}

And this is the method I have created in the oblong class to increase them:

public class Oblong
{
    // instance variables
    private double height;
    private double width;
   private double x;

    // constructor
    public Oblong()
   {

        height = 0.0;
        width = 0.0;
      x = 0.0;
    }

    // methods
    public double getHeight()
    {   
        return height;      
    }

    public double getWidth()
    {   
        return width;       
    }
   public double setX(double x)
   {
      return x;
   }
    public void setWidth(double w)
    {   
        width = w;
    }

    public void setHeight(double h)
    {   
        height = h;
    }

    public double calculateArea()
    {   
        return width * height;
    }   

   public double calculatePerimeter()
   {
      return width + height * 2;

   }

   public boolean isSquare()
   {
      if(height == width)
      {
         return true;
      }
      else
      {
         return false;
      }
    }
   public double incHeight()
    {
      { 
        return height + x ; 

       }
   }
   public double incWidth()
   {
      {
         return width + x ;
      }
   }
}// end of class

but it only ever prints out the original height and width.

Ant695
  • 71
  • 1
  • 2
  • 10
  • Can we see the full code of `Oblong`? Also please post the input you used, the actual output and the expected one –  Dec 29 '15 at 15:00
  • I've added the full code for oblong. Input I was using was height 10 width 10 increase by 5. Which would return 10,10 or I would swap them and use 5, 5 increase by 10 and result would be 5,5. – Ant695 Dec 29 '15 at 15:06
  • Side note: if you would call it "rectangle" instead of "oblong" ... would have saved me some seconds of "wtf is an oblong"? – GhostCat Dec 29 '15 at 15:08
  • @ It does not work because ``x` is not setted, check the `setX(double x)`, it should be `this.x = x` – 54l3d Dec 29 '15 at 15:08
  • @Jägermeister yeah I know that is just what it's called in our notes. Had fun on some of the other questions trying to find formulas for perimeter of oblong etc... as nobody calls it that. – Ant695 Dec 29 '15 at 20:09

3 Answers3

2

Whit this:

 public double incWidth()
   {
      {
         return width + x ;
      }
   }

You are returning width + 1 but you are not modifying the private attribute. Just do this:

public double incWidth(){
    this.width = this.width + 1;
    return this.width;
}

Also in setters you don't need to return anything. To change an attribute inside a class do something like:

private double value;

private void setValue( double value ) {
   this.value = value;
}

With this.value you are refering to the private value inside the class. Without this you are refering to the parameter value of the method setValue.

Further reading: How do getters and setters work?

Community
  • 1
  • 1
2

Your instance variable x has not been set, hence the height + x will return height + 0.0.

change this:

public double setX(double x)
   {
      return x;
   }

To this:

public double setX(double x)
   {
      this.x = x;
   }

This will return value to be displayed, but it should be set for later use, so you need this :

public void incHeight()
    {
        setHeight(height + x) ; 
    }

and then :

System.out.println("Height " + height); // height  not h
54l3d
  • 3,913
  • 4
  • 32
  • 58
2

Your code:

public double incHeight()
{
  { 
    return height + x ; 

   }
}

That just adds two numbers and returns the result. It doesn't do anything else. The same is true for your other methods.

Whereas the purpose of the method seems to be to alter the state of the underlying object. But as said; the current implementation does not alter that state.

Hope that is good enough to help you to resolve your problem on your own.

Side note: read about Java syntax. Your extra pair of braces ... doesn't do anything either.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Thanks yeah I've got it working now. Yeah I think I added the extra ones because above that method is an if/else that has extra {} wasn't paying attention. – Ant695 Dec 29 '15 at 20:33
  • Please note: I am a big favor of always using `{ blocks }` ... even when we are talking about if/then/else or loops with just a single statement. – GhostCat Dec 30 '15 at 08:56
  • What exactly do you mean by using blocks? – Ant695 Dec 30 '15 at 10:20
  • I mean that `if (foo) { bar(); }` is better than `if (foo) bar();` – GhostCat Jan 04 '16 at 12:55