class rectangle, problem Deitel set and get

3

They ask me to create a rectangle class with length and width.

  

8.4 ( Rectangle Class ) Create a class named Rectangle with the length and width attributes, each with a default value of    1 You must have methods to calculate the perimeter and area of the rectangle. You must have methods set and get for length   and width. The set methods should verify that the length and   width are floating point numbers greater than 0,0 , and smaller than    20,0 Write a program to test the Rectangle class.

What am I wrong with?

public class DeitelClaseRectangulo882Evaluacion {
    public static void main(String[] args) {
        // TODO code application logic here
        DeitelClasePrueba rectangulo = new  DeitelClasePrueba(2.0, 3.0 );
        System.out.printf("%d", rectangulo );
        //System.out.printf("%d", rectangulo );
        //rectangulo.obtenerLongitud();
        //rectangulo.obtenerAnchura();
    } // main
    class DeitelClasePrueba
    {
        private double longitud;
        private double anchura;
        public DeitelClasePrueba()
        {
            longitud = 1;
            anchura = 1;
        }
        // constructor con valor predeterminado de 1 
        public DeitelClasePrueba( double longitudPrueba, double anchuraPrueba ) 
        {
            longitud = longitudPrueba;
            anchura = anchuraPrueba;
        }
        public double calculaPerimetro() 
        {

            return 2 *( anchura + longitud );
        }
        public double calcularArea()
        {
            return anchura * longitud;
        }
        // metodo establecer debe verificar que longitud sea numero flotante
        // mayores de 0.0 y menores de 20.0
        public void establecerLongitud()
        {
            if ( longitud >= 0.0 && longitud < 20.0 )
                longitud = longitudPrueba;
            else
                throw new IllegalArgumentException(
                    "la longitud debe debe de ser > 0 < 20.0");
        } // establecerLongitud
        public void establecerAnchura()
        {
            if ( anchura >= 0.0 && anchura < 20.0 )
                anchura = anchuraPrueba;
            else 
                throw new IllegalArgumentException(
                    "el ancho debe de ser > 0 < 20 ");  
        }
        public double obtenerLongitud()
        {
            return longitud;
        }

        public double obtenerAnchura()
        {
            return anchura;
        }
    }
}

Error:

    
asked by Gerardo Bautista 18.10.2016 в 04:58
source

5 answers

1

Some considerations to improve your code and its errors that are probably presenting variables indefinidas , acceso a la clase DeitelClasePrueba

  • Separating classes from the Main class, although Java does not forbid it, is not a good practice
  • When talking about default values, the constructor empty should initialize the attributes to 1
  • When you are asked for methods obtener(get) and establecer(set) refer to the getters and setters that all your classes must have

Your Rectangle class would look like this

public class Rectangulo {
private double longitud;
private double anchura;

public Rectangulo(double longitud, double anchura) {
    this.longitud = longitud;
    this.anchura = anchura;
}

public Rectangulo() {
    this.longitud=1;
    this.anchura=1;
}

 /* Aquí van los Métodos calcular Area y Perimetro*/

public double getLongitud() {
    return longitud;
}
 public double getAnchura() {
    return anchura;
} 
/* Métodos Set incluida su Verificación */
public void setLongitud(double longitud) {
    if ( longitud > 0.0 && longitud < 20.0 )
        this.longitud = longitud;
    else
        throw new RuntimeException(
        "la longitud debe debe de ser > 0 < 20.0");
}

public void setAnchura(double anchura) {
    if (anchura > 0.0 && anchura < 20.0 )
        this.anchura = anchura;
    else
        throw new RuntimeException(
        "la Anchura debe debe de ser > 0 < 20.0");

}

@Override
public String toString() {
    return "Rectangulo{" + "longitud=" + longitud + ", anchura=" + anchura + '}';
 }
}

And in your Main File you would have

public static void main(String[] args)  {
  /* Constructor sin Parametros , atributos igual a 1 */
  Rectangulo rect = new Rectangulo();
  rect.setAnchura(-12.2);/*Exception menor a 0*/
  rect.setLongitud(12.2);/* Valor aceptado */
  System.out.println(rect.calcularArea()); /* Obtener Area*/
  System.out.println(rect.calculaPerimetro());/* Obtener Perimetro*/
}
    
answered by 18.10.2016 в 05:35
1

Your problem is that you did not put the parameter variable in the set methods.

public class DeitelClaseRectangulo882Evaluacion {

  public static void main(String[] args) {
    // TODO code application logic here
    DeitelClasePrueba rectangulo = new  DeitelClasePrueba();

    rectangulo.establecerLongitud(22);
    rectangulo.establecerAnchura(4);

    System.out.println(rectangulo.obtenerLongitud() );
    System.out.println( rectangulo.obtenerAnchura() );
    System.out.println("Perimetro "+rectangulo.calculaPerimetro() );
    System.out.println("Area: "+rectangulo.calcularArea() );
    //rectangulo.obtenerLongitud();
    //rectangulo.obtenerAnchura();



  } // main 

}

class DeitelClasePrueba
{

private float longitud;
private float anchura;


public DeitelClasePrueba()
{
    longitud = 1;
    anchura = 1;

}


public float calculaPerimetro() 
{

    return 2 *( anchura + longitud );
}

public float calcularArea()
{
    return anchura * longitud;
}

// metodo establecer debe verificar que longitud sea numero flotante
// mayores de 0.0 y menores de 20.0
public void establecerLongitud(float longitud)
{
    if ( longitud >= 0.0 && longitud < 20.0 )
        this.longitud = longitud;
    else
        throw new IllegalArgumentException(
            "la longitud debe debe de ser > 0 < 20.0");
} // establecerLongitud

public void establecerAnchura(float anchura)
{
    if ( anchura >= 0.0 && anchura < 20.0 )
        this.anchura = anchura;
    else 
        throw new IllegalArgumentException(
            "el ancho debe de ser > 0 < 20 ");

}

public float obtenerLongitud()
{
    return longitud;
}

 public float obtenerAnchura()
 {
    return anchura;
 }

}
    
answered by 18.10.2016 в 05:42
1

Your methods do not pass the necessary parameters to validate

public void establecerLongitud(double longitudPrueba) {
if (longitud >= 0.0 && longitud < 20.0)
 longitud = longitudPrueba;
else
 throw new IllegalArgumentException(
  "la longitud debe debe de ser > 0 < 20.0");
  } // establecerLongitud

 public void establecerAnchura(double anchuraPrueba) {
   if (anchura >= 0.0 && anchura < 20.0)
     anchura = anchuraPrueba;
   else
      throw new IllegalArgumentException(
 "el ancho debe de ser > 0 < 20 ");
  }
    
answered by 18.10.2016 в 05:53
1

The error that jumps is because inside the main method, which is static, you can not refer to the internal class (I think it is the inner class translation) DeitelClaseTest, since, as the error tells you, from a static context it does not You can reference a non-static element.

Just what the code is will work for you if you simply mark the DeitelClass Test class as static.

Additionally, there is also the problem referred by @ dev-joel and @ alexis-rodrigez, the parameter with the value to be set for length and width is missing.

Code

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

        DeitelClasePrueba rectangulo = new  DeitelClasePrueba(2.0, 3.0 );
        System.out.printf("%d", rectangulo );
    }

    // este static es el que habilita que puedas usar la clase desde el main
    static class DeitelClasePrueba
    {
        ...contenido...

        // metodo establecer debe verificar que longitud sea numero flotante
        // mayores de 0.0 y menores de 20.0
        public void establecerLongitud(float longitudPrueba)
        {
            if ( longitud >= 0.0 && longitud < 20.0 )
                longitud = longitudPrueba;
            else
                throw new IllegalArgumentException(
                "la longitud debe debe de ser > 0 < 20.0");
        } 

        // establecerLongitud
        public void establecerAnchura(float anchuraPrueba)
        {
            if ( anchura >= 0.0 && anchura < 20.0 )
                anchura = anchuraPrueba;
            else 
                throw new IllegalArgumentException(
                "el ancho debe de ser > 0 < 20 ");  
        }
    }
}

Note

  • As suggested by @ dev-joel if you separate DitelClaseTest in a separate file with the same name, you would not have this problem.
answered by 19.10.2016 в 09:18
1

Another possible solution I can think of is the following:

In the constructor, I solved the issue of calling set (set), to validate the input .

public class Rectangulo {
    private double longuitud, altura;

    public Rectangulo(double longuitud, double altura) {
        this.longuitud = setLonguitud(longuitud);
        this.altura = setAltura(altura);
    }
    public Rectangulo(){
        this.longuitud = 1;
        this.altura = 1;
    }
    public double getLonguitud() {
        return longuitud;
    }

    public double setLonguitud(double longuitud) {

        if (longuitud > 0.0 && longuitud <= 20.0) {
            return longuitud;
        } else {
            return longuitud = 1;
        }

    }

    public double getAltura() {
        return altura;
    }

    public double setAltura(double altura) {
        if (altura > 0.0 && altura <= 20.0){
            return this.altura = altura;
        }else{
            return this.altura = 1;
        }
    }

    public double calcularArea() {
        return getLonguitud() * getAltura();
    }

    public double calcularPerimetro() {

        return 2 * (getLonguitud() + getAltura());
    }

    public String toRectangulo() {
        DecimalFormat dosDigitos = new DecimalFormat("#.####");
        return "Longuitud: " + dosDigitos.format(getLonguitud()) + " Altura: "
            + dosDigitos.format(getAltura());
    }

    public String toArea() {
        DecimalFormat dosDigitos = new DecimalFormat("#.####");
        return "El area es: " + dosDigitos.format(calcularArea());
    }

    public String toPerimetro() {
        DecimalFormat dosDigitos = new DecimalFormat("#.####");
        return "El perimetro es: " + dosDigitos.format(calcularPerimetro());
    }
}

... and the main to do the test, it would be like this:

public class PruebaRectangulo {
    public static void main(String[] args) {
        Rectangulo r1 = new Rectangulo(-1, -22.06);
        Rectangulo r2 = new Rectangulo(3.15, 5.15);
        Rectangulo r4 = new Rectangulo(20.5, 0);

        System.out.println("R1");

        r1.calcularArea();
        r1.calcularPerimetro();
        System.out.println(r1.toRectangulo());
        System.out.println(r1.toArea());
        System.out.println(r1.toPerimetro());

        System.out.println("R2");
        r2.calcularArea();
        r2.calcularPerimetro();
        System.out.println(r2.toRectangulo());
        System.out.println(r2.toArea());
        System.out.println(r2.toPerimetro());

        System.out.println("R4");
        r4.calcularArea();
        r4.calcularPerimetro();
        System.out.println(r4.toRectangulo());
        System.out.println(r4.toArea());
        System.out.println(r4.toPerimetro());
    }

}
    
answered by 26.10.2016 в 03:24