Initialize Array that is private property of one class in another!

0

I have a project for a structured exercise in three packages:

  • Application (Main)

    package aplicacion;
    
    import mountainbike.MountainBike;
    
    public class ProbandoMountainBike {
    
    public static void main(String[] args) {
    
    MountainBike mb = new MountainBike();
    
    mb.setTamanoRueda(1);
    mb.setNumPlatos(1);
    mb.setNumPinones(11);
    
    mb.setNumDientes() //**************************
    
    
    
    double diametro = 0;   // Declaro esta variable para sacar el valor en pulgados según tamaño de rueda
    
    if (mb.getTamanoRueda() == 1) {  // Damos valor al diametro según el tamaño rueda
    
        diametro = 26;
    
    } else if (mb.getTamanoRueda() == 2) {
    
        diametro = 27.5;
    
    } else if (mb.getTamanoRueda() == 3) {
    
        diametro = 29;
    }
    
    System.out.println("Objeto utilizando el constructor parametrizado");
    
    System.out.println("\nDiámetro de la rueda: " + diametro);
    
    }
    
    }
    
  • Bike (father)

    package bicicleta;
    
    public class Bicicleta {
    
    private int tamanoRueda ;   // 1 - 2 - 3 para tamaños 26 - 27.5 - 29
    private double velocidad;   // Velocidad Bicicleta
    private int numPlatos;      // Platos que tiene la bicicleta
    private int numPinones;     // Piñones que tiene la bicicleta
    
    public Bicicleta() {
    
    }
    
    public Bicicleta(int tamanoRueda, double velocidad, int numPlatos, int numPinones) {
        this.tamanoRueda = tamanoRueda;
        this.velocidad = velocidad;
        this.numPlatos = numPlatos;
        this.numPinones = numPinones;
    }
    
    public int getTamanoRueda() {
    
        return tamanoRueda;
    }
    
    public void setTamanoRueda(int tamanoRueda) {
        this.tamanoRueda = tamanoRueda;
    }
    
    public double getVelocidad() {
        return velocidad;
    }
    
    public void setVelocidad(double velocidad) {
        this.velocidad = velocidad;
    }
    
    public int getNumPlatos() {
        return numPlatos;
    }
    
    public void setNumPlatos(int numPlatos) {
        this.numPlatos = numPlatos;
    }
    
    public int getNumPinones() {
        return numPinones;
    }
    
    public void setNumPinones(int numPinones) {
        this.numPinones = numPinones;
    }
    
    public void acelera(double aceleracion) {
        this.velocidad += aceleracion;
    }
    
    public void frena(double desaceleracion) {
        this.velocidad -= desaceleracion;
    }
    

    }

  • Mountain Bike (subclass inherits Bike properties)

    package mountainbike;
    
    import bicicleta.Bicicleta;
    import java.lang.reflect.Array;
    
    
    public class MountainBike extends Bicicleta {
    
    private int [] numDientes = new int [15];
    private int plato;
    private int pinon;
    
    public MountainBike() {
    
    }
    
    
    public MountainBike(int[] numDientes, int plato, int pinon, int tamanoRueda, double velocidad, int numPlatos, int numPinones) {
        super(tamanoRueda, velocidad, numPlatos, numPinones);
        this.plato = plato;
        this.pinon = pinon;
    }
    
    
    
    public int[] getNumDientes() {
        return numDientes;
    }
    
    public void setNumDientes(int[] numDientes) {
        this.numDientes = numDientes;
    }
    
    public int getPlato() {
        return plato;
    }
    
    public void setPlato(int plato) {
        this.plato = plato;
    }
    
    public int getPinon() {
        return pinon;
    }
    
    public void setPinon(int pinon) {
        this.pinon = pinon;
    }
    
    public double cmAvanza(){
    
        double cm = 0;
    
    
        cm = (numDientes[plato-1] / numDientes[pinon+2])* 2 * Math.PI * ((getTamanoRueda()*2.54)/2);;        
    
        return cm;
    }
    

    }

The problem is that one of the properties of Mountain Bike is a private Array [15] which is not initialized in the Main. Since in the enenunciado of the problem I am told:

  

Schedule a MountainBike name class, which inherits from Bicycle, and   Also add the following properties and methods: Properties   (private):
  - numDientes: array of 15 integers that will store in its first three positions the number of teeth of each of the three possible   dishes that can have a Mountain Bike. In the case of having less than   three plates will only have values the necessary positions of the array.   In the remaining positions, the number of teeth of the   pine nuts that have the bicycle

So the array I must declare as private in MoutainBike and then in the last point I'm asked:

  

Make a program that declares an object of the class    MountainBike Create the object using the default constructor.   Assign the values to the object: sizeRave: 1 numPlates: 1   numPinons: 11 numDientes: 22-xx-xx-11-13-15-17-19-21-24-28-32-36-42-xx   (xx could have any value, since in this object it is not   use) plate: 1 pinon: 5

     

Visualize the data of this bicycle, and the centimeters it advances   in each pedalada.

     

Create another object by passing its initial values through the   builder. The values will be: sizeRave: 3 numPlates: 2   numPinons: 11 numDientes: 22-36-xx-11-13-15-17-19-21-24-28-32-36-42-xx   (xx could have any value, since in this object it is not   use) plate: 2 pinon: 1

    
asked by Ivan Vieites Lores 09.05.2017 в 17:46
source

2 answers

3

From what I understand, from the main you want to assign the 15 values to numDientes, then you could change the constructor to receive an array like this:

public MountainBike(int plato, int pinon, int tamanoRueda, double velocidad, int numPlatos, int numPinones,int[] numDientes) {

    super(tamanoRueda, velocidad, numPlatos, numPinones);
    this.plato = plato;
    this.pinon = pinon;
    this.numDientes = numDientes;
}

and from the main you simply create an array with the values you want and pass them as a parameter, something like this:

int[] dientes = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
MountainBike mb = new MountainBike(plato,pinon,tamanoRueda,velocidad,numPlatos,numPinones,dientes);
    
answered by 09.05.2017 в 18:26
0

In the end what I was looking for was the following I declare the constructor in the following way:

public class MountainBike extends Bicicleta {

    private int [] numDientes = new int [15];
    private int plato;
    private int pinon;

    public MountainBike() {

    }


    public MountainBike(int[] numDientes, int plato, int pinon, int tamanoRueda, double velocidad, int numPlatos, int numPinones) {
        super(tamanoRueda, velocidad, numPlatos, numPinones);
        this.numDientes = numDientes;
        this.plato = plato;
        this.pinon = pinon;
    }

And to initialize the array from main I had to use the following expression:

public class ProbandoMountainBike {

    public static void main(String[] args) {

        MountainBike mb = new MountainBike();

        mb.setTamanoRueda(1);
        mb.setNumPlatos(1);
        mb.setNumPinones(11);
        mb.setNumDientes(new int[]{22, 0, 0, 11, 13, 15, 17, 19, 21, 24, 28, 32, 36, 42, 0});
        mb.setPlato(1);
        mb.setPinon(5);

I did not know why I had to use the " new int [] {xxx} ". Now what I need is to print by screen, for example:

System.out.println("Número de dientes de los 11 piñones: " + b.getNumDientes());

It comes from position [3] to [14] ?????

    
answered by 10.05.2017 в 09:57