Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why aren't my variables using the input value

For a project I have to use a constructor to set default values for a rectangle height and width and also be able to input numbers. however when I input the program continues to use default values any way to get it to only use default values when there is no input?

package geometry;
// file name Rectangle.java
/**
 *
 * @author 12484
 */
public class Rectangle {

 // **********************instance variables (properties)
 private double myWidth;
 private double myLength;
// ********************************* constructors
 // default constructor

 public Rectangle() {
  myWidth = 1;
  myLength = 1;
 }
 // "other" constructor

 public Rectangle (double width, double length) {
  myWidth = width;
  myLength = length;
 }

 // ********************************* accessor methods
 public double getWidth() {
  return myWidth;
 }

 public double getLength() {
  return myLength;
 }

 // ********************************* modifier methods
 public void setWidth(double width) {
  myWidth = width;
 }

 public void setLength(double length) {
  myLength = length;
 }

 // ********************************* interesting methods
 public double computeArea() {
  getLength();
  getWidth();
  double area = Math.pow(getLength(), getWidth());
  return area;
 }

 public double computePerimeter() {
  getLength();
  getWidth();
  double perimeter = Double.sum(getLength(), getWidth());
  return perimeter;
 }
}


package geometry;
// filename Rectest.java
public class Rectest {
    public static void main(String[]args){
        Rectangle len = new Rectangle();
        Rectangle wid = new Rectangle();
        Rectangle a = new Rectangle();
        len.setLength(5.2);
        wid.setWidth(7.5);
        System.out.println(a.computeArea());
    }
}

default values are 1 for length and 1 for width

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

len,wid and a are all different objects(rectangles in this case) with their own instance variables(width and length).

What you want to do is create a Rectangle object, set its length and width and then execute the computeArea on that object.

public class Rectest {
    public static void main(String[]args){
        Rectangle a = new Rectangle();
        a.setLength(5.2);
        a.setWidth(7.5);
        System.out.println(a.computeArea());
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading