I print toString but with value 0

1

I have a small problem. In class design a small program which consists of 3 classes. Point, Circle and Test. I already have the methods for each class and I print the values except one that is the center attribute of the class Point, which must show which are the condemned of this, everything else that asked me to make the program as area, perimeter and radius I get it right, but I do not get what is the center.

Point Class

public class Punto {
    private int X;
    private int Y;

    public Punto(int x, int y){
        this.X = X;
        this.Y = Y;
    }

    public int getX() {
        return X;
    }

    public void setX(int X) {
        this.X = X;
    }

    public int getY() {
        return Y;
    }

    public void setY(int Y) {
        this.Y = Y;
    }

    public String toString(){
        return "Eje X: " +this.X +"Eje Y: " +this.Y;
    }
}

Circle Class

public class Circulo {
    public Punto centro;
    private double radio;
    private double area;
    private double perimetro;

    public Circulo(int radio, int area, int perimetro){
        this.radio = radio;
        this.area = area;
        this.perimetro = perimetro;

    }

    public Punto getCentro() {
        return centro;
    }

    public void setCentro(Punto centro) {
        this.centro = centro;
    }

    public double getRadio() {
        return radio;
    }

    public void setRadio(double x, double y) {

        this.radio = Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2));
    }

    public double getArea() {
        return area;
    }

    public void setArea(double area) {
        this.area = Math.PI*Math.pow(this.radio, 2);
    }

    public double getPerimetro() {
        return perimetro;
    }

    public void setPerimetro(double radio) {
        this.perimetro = 2*Math.PI*this.radio;
    }

    public String toString(){
        return "El radio es: " +radio +" con un área de: " +area +" y un perímetro de: " +perimetro;
    }


}

Test Class

import java.util.Scanner;
public class Prueba {
    private static Punto punto = new Punto(0,0);
    private static Circulo circulo = new Circulo(0,0,0);
    private static int corX, corY;
    private static double radio;
    private static Scanner entrada = new Scanner(System.in);

    public static void main(String [] args){
        introducirCordenadas();
        calcularRadio();
        calcularPerimetro();
        calcularArea();
        imprimirDatos();
    }

    public static void introducirCordenadas(){
        System.out.println("A continuación introduzca la cordenada de X");
        corX = entrada.nextInt();
        punto.setX(corX);
        System.out.println("A continuación introduzca la cordenada de Y");
        corY = entrada.nextInt();
        punto.setY(corY);

      circulo.centro = new Punto(corX, corY); 
    }

    public static void calcularRadio(){
        circulo.setRadio(punto.getX(), punto.getY());
    }

    public static void calcularPerimetro(){
        circulo.setPerimetro(circulo.getRadio());
    }

    public static void calcularArea(){
        circulo.setArea(circulo.getRadio());
    }

    public static void imprimirDatos(){
        System.out.println("El centro " +circulo.centro.toString()); //Sale X=0, Y=0
        System.out.println("Los valores son: " +circulo.toString());
    }
}
    
asked by Carlos Carrete 28.09.2017 в 06:43
source

1 answer

1

The error is in the constructor of the class point, this because the attributes of the class you are assigning the same attributes and not the various that you pass as parameters

public class Punto {
  private int X;
  private int Y;

  public Punto(int x, int y){
    this.X = X; //cambiar por x minuscula
    this.Y = Y; //cambiar por y minuscula
  }
//demas código

Here the modified point class

public class Punto {
  private int X;
  private int Y;
  public Punto(int x, int y){
    this.X = x;
    this.Y = y;
  }
  public int getX() {
    return X;
  }
  public void setX(int X) {
    this.X = X;
  }
  public int getY() {
    return Y;
  }
  public void setY(int Y) {
    this.Y = Y;
  }
  public String toString(){
    return "Eje X: " +this.X +"Eje Y: " +this.Y;
  }
}
    
answered by 28.09.2017 / 07:05
source