So rufen Sie mit boxTest Länge, Breite, Höhe, Fläche und Volumen einer Box aus der Box-Klasse aufJava

Java-Forum
Anonymous
 So rufen Sie mit boxTest Länge, Breite, Höhe, Fläche und Volumen einer Box aus der Box-Klasse auf

Post by Anonymous »

Ich schreibe einen Code, um die Oberfläche einer Box und das Volumen der Box zu messen. Endlich habe ich es zum Laufen gebracht. Die Herausforderung besteht nun darin, dass ich 4 Objekte erstellen und in einem Array speichern und dann die erweiterte for-Schleife verwenden muss, um jedes Feld im Array zu durchlaufen. Ich meine, wenn Sie das Array durchlaufen, gelangen Sie zum ersten Feld und werden aufgefordert, die Länge, Breite und Höhe einzugeben. Anschließend werden Länge, Breite, Höhe, Oberfläche und Volumen der ersten Box angezeigt. Ich versuche, ein Beispiel dafür zu finden, kann aber nichts finden. Ich versuche immer noch, es zum Laufen zu bringen. Ich danke Ihnen für Ihre Hilfe. Hier ist mein Box-Code.

Code: Select all

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());
}
}

und hier ist mein Hauptcode

Code: Select all

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));

}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post