ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

Javaの課題丸投げコミュの質問させてください!

  • mixiチェック
  • このエントリーをはてなブックマークに追加
javaの授業の課題なのですが、わかりません。わかる方がいらっしゃいましたら、ぜひ教えてください。
円柱の半径,高さを入力し,円柱の体積と表面積を出力するクラス Problem2 を作成しなさい.
ただし,mainメソッドを含むクラスとは別に,円柱のクラスを作成すること.
円柱のクラスのコンストラクタは2変数であることに注意すること.

コメント(4)

こんなんでいいですか

public class Cylinder {
private double radius;
private double height;

public Cylinder(double radius , double height) {

this.radius = radius;
this.height = height;

}

public double calcVolume(){
// V = πr2h
return radius * radius * height * 3.14;
}

public double calcSurfaceArea(){
//S = 2πr(r + h)
return 2 * 3.14 * radius * (radius + height);
}


}
public class Problem2 {
public static void main(String[] args) {
double radius = Double.parseDouble(args[0]);
double height = Double.parseDouble(args[1]);

Cylinder cylinder = new Cylinder(radius , height);
System.out.println("体積:"+ cylinder.calcVolume());
System.out.println("表面積:"+ cylinder.calcSurfaceArea());


}
}
>かんくそさん

答えてくださって本当にありがとうございます。
ただ、作ってくださったプログラムが読み込めません。
初心者なので原因がわからないのですが、操作方法が間違っているのでしょうか・・・。
一応自分の書いたものも載せますが、動いてくれません。もうなにがなんだかわかりません・・。間違えている箇所などありましたら教えてください。


import java.util.*;

class Problem2 {
public static void main(String args[]) throws Exception {
Scanner scanner = new Scanner(System.in);

/* 半径の読み込み */
System.out.print("input radius: ");
double radius = scanner.nextDouble();

  /*高さの読み込み*/
System.out.print("input height: ");
double height = scanner.nextDouble()

     /* 円柱のオブジェクト生成 */
Cvlinder cvlinder = new Cvlinder(radius);


/* 円柱の体積 */
double volumeOfBall = ball.volume();
System.out.println("Volume of ball = " + volumeOfBall);

/* 円柱の表面積 */
double areaOfSurfaceOfCvlinder = cvlinder.areaOfSurface();
System.out.println("Area of Surface of Cvlinder = " + areaOfSurfaceOfCvlinder);
}
}


    /* 円柱のクラス */
class Cvlinder {

double radius;

/* コンストラクタ (オブジェクト生成時(new)に実行される) */
Cvlinder(double radius) {
this.radius = radius;
}

/* 円柱の体積の計算 */
public double volume() {
return( radius * radius * Math.PI * height);
}

/* 円柱の表面積の計算 */
public double areaOfSurface() {
return(radius * radius * Math.PI * 2.0 + 2.0 * Math.PI * radius * height);
}
}

コンパイルがとおってないですね。。。


double height = scanner.nextDouble()
→セミコロンがない

Cvlinderクラス
→Cylinderクラス

double volumeOfBall = ball.volume();
→ball変数は何?

ていうか
ball.volume();
cvlinder.areaOfSurface();

とかそんなメソッドないですよ。
>かんくそ さん

ありがとうごさいましたexclamation ×2なんとかコンパイルできる形にして提出しました!本当に感謝しています。

ログインすると、みんなのコメントがもっと見れるよ

mixiユーザー
ログインしてコメントしよう!

Javaの課題丸投げ 更新情報

Javaの課題丸投げのメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。