Как вызовать длину, ширину, высоту, область, громкость коробки из класса Box с помощью BoxtestJAVA

Программисты JAVA общаются здесь
Anonymous
Как вызовать длину, ширину, высоту, область, громкость коробки из класса Box с помощью Boxtest

Сообщение Anonymous »

Я пишу код для измерения площади поверхности коробки и объема коробки. Наконец, я получил это до работы. Теперь проблемы я должен сделать 4 объекта и хранить его в массиве, а затем использовать Enhanced для цикла, чтобы пройти через каждую коробку в массиве. Я имею в виду, когда вы пройдете через массив, он попадет в первую коробку и попросит вас войти в длину, ширину и высоту. Затем он покажет вам длину, ширину первой коробки, высоту, площадь поверхности и объем. Я пытаюсь найти пример для этого, но я ничего не могу найти. Я все еще пытаюсь заставить это работать. Спасибо за помощь. Вот мой код ящика. < /p>

public class Box
{
private double length = 1.0;
private double width = 1.0;
private double height = 1.0;

//constructor
public Box (double l, double w, double h)
{
setLength(l);
setWidth(w);
setHeight(h);
}

//set length method
public void setLength(double l)
{
if(l > 0)
{
length = l;
}
else
{
length = 1.0;
}
}

//set width method
public void setWidth(double w)
{
if(w > 0)
{
width = w;
}
else
{
width = 1.0;
}
}

//set height method
public void setHeight(double h)
{
if(h > 0)
{
height = h;
}
else
{
height = 1.0;
}
}

//calculate area method
public double calculateArea(double length, double width)
{
return (length*width);
}

//calculate volume method
public double calculateVolume(double length, double width, double height)
{
return (length*width*height);
}

//get length method
public String getLength()
{
return String.format("%f", length);
}

//get width method
public String getWidth()
{
return String.format("%f",width);
}

//get height
public String getHeight()
{
return String.format("%f",height);
}

public String toString()
{
return String.format("Length is %s.\nWidth is %s.\nHeight is %s.\n", getLength(), getWidth(), getHeight());
}
< /code>

} < /p>

А и вот мой основной код < /p>

import java.util.Scanner;

public class BoxTest
{
public static void main(String[] args)
{
//Box boxOne, boxTwo, boxThree, boxFour;
double l;
double w;
double h;

Scanner input = new Scanner(System.in);
int[] boxes = new int[4];
System.out.print ("Enter the length of your box:");
l= input.nextDouble();
System.out.print ("Enter the width of your box:");
w= input.nextDouble();
System.out.print ("Enter the height of your box:");
h= input.nextDouble();

Box boxOne = new Box(l, w, h);
System.out.println(boxOne.toString());
System.out.printf("The surface area of the box is %f.\nThe volume of the box is %f.\n",
boxOne.calculateArea(l, w), boxOne.calculateVolume(l, w, h));

}
}


Подробнее здесь: https://stackoverflow.com/questions/273 ... s-using-bo

Вернуться в «JAVA»