Я пишу код для измерения площади поверхности и объема коробки. Наконец, я заставил это работать. Теперь задача состоит в том, что мне нужно создать 4 объекта и сохранить их в массиве, а затем использовать расширенный цикл for для прохождения каждого поля в массиве. Я имею в виду, что когда вы просматриваете массив, он доберется до первого поля и попросит вас ввести длину, ширину и высоту. Затем он покажет вам длину, ширину, высоту, площадь поверхности и объем первой коробки. Я пытаюсь найти пример, но ничего не могу найти. Я все еще пытаюсь заставить это работать. Спасибо за помощь. вот мой код ящика.
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());
}
и вот мой основной код
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
Как вызвать длину, ширину, высоту, площадь, объем коробки из класса коробки с помощью boxTest ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как с помощью NetTopologySuite вычислить площадь многоугольника в квадратных метрах?
Anonymous » » в форуме C# - 0 Ответы
- 39 Просмотры
-
Последнее сообщение Anonymous
-