2 つのインスタンス変数 (幅と高さ) と 2 つのインスタンス メソッド (面積と円周) を持つJava クラスを作成しましたRectangle
。両方のメソッドはパラメーターを取りませんが、double 値を返します。area メソッドは長方形の面積 (幅 * 高さ) を返し、円周は (2*幅 + 2*高さ) を返します。次に、main メソッドを使用して Demo クラスを作成し、4 つのオブジェクトをインスタンス化してクラス Rectangle をテストし、ユーザーに各インスタンスの幅と高さを入力するように求めます。次に、各インスタンスの面積と円周を出力します。
2 つのクラスを作成し、最初のクラスは Rectangle です。
public class Rectagle {
private double width;
private double height;
public double area() {
return width * height;
}
public double circumference() {
return 2*width+2*height;
}
}
そして、クラスをテストするための 2 番目のクラス Demo を作成します。
import java.util.Scanner;
public class Demo {
public static void main(String []args){
Scanner console=new Scanner(System.in);
Rectagle R1=new Rectagle();
Rectagle R2=new Rectagle();
Rectagle R3=new Rectagle();
Rectagle R4=new Rectagle();
}
}
私の問題、この点がわかりません」というメッセージが表示され、ユーザーは各インスタンスの幅と高さを入力するように求められます。次に、各インスタンスの面積と円周を出力します。