Matriz de objetos: tabla Aprende programación con ejercicios Java

Lección:

Más sobre Clases


Ejercicio:

Matriz de objetos: tabla 34


Objetivo:

Cree una clase denominada "Table". Debe tener un constructor, indicando el ancho y alto de la placa. Tendrá un método "ShowData" que escribirá en la pantalla el ancho y la altura de la tabla. Cree una matriz que contenga 10 tablas, con tamaños aleatorios entre 50 y 200 cm, y muestre todos los datos.


Código:

package ArrayOfObjects;
import java.util.*;

public class Table
{
	private float width, height;

	public Table()
	{
	}
	public Table(float width, float height)
	{
		this.width = width;
		this.height = height;
	}

	public final void setWidth(float value)
	{
		width = value;
	}
	public final float getWidth()
	{
		return width;
	}
	public final void setHeight(float value)
	{
		height = value;
	}
	public final float getHeight()
	{
		return height;
	}

	public final void ShowData()
	{
		System.out.printf("Width: %1$s, Heigth: %2$s" + "\r\n", width, height);
	}
}

public class Main
{
	public static void main(String[] args)
	{
		boolean debug = false;

		Table[] myTables = new Table[10];
		Random rnd = new Random();

		for (int i = 0; i < 10; i++)
		{
			myTables[i] = new Table(rnd.nextInt(50, 201), rnd.nextInt(50, 201));
			myTables[i].ShowData();
		}

		if (debug)
		{
			new Scanner(System.in).nextLine();
		}
	}
}