• 締切済み

Javaの問題

(1) /* CarApp.java */ public class CarApp{ public static void main(String[] args){ Car c2 = new Car(4,45,5); System.out.println("乗員数:"+c2.getPeple()); System.out.println("タイヤ数:"+c2.getTire()); System.out.println("ガソリン残量:"+c2.getGas()); c2.setPeople(8); c2.ridePeople(10); c2.ridePeople(3); for(int i=0; i<179; i++) c2.speedUp(); for(int i=179; i<182; i++){ c2.showSpeed(); c2.speedUp(); } for(int i=0; i<179; i++) c2.speedDown(); for(int i=179; i<182; i++){ c2.showSpeed(); c2.speedDown(); } } } ____________________________________ /* Car.java */ class Car{ private int tire , people , speed ; //タイヤ数、乗員数、速度 private int 【****】,【***】; private double gas; //ガソリン public Car(int tire, double gas, int people){ this.tire = tire; this.gas = gas; this.people = people; speed = 180; nowPeople = 0; nowSpeed = 0; } public double getGas(){ 【****】} public int getPeople() { 【****】 } public int getTire() { 【****】 } public int getSpeed() { 【****】 } public void setSpeed(int speed){ this.speed = speed; } public void speedUp( ) { if(speed < nowSpeed+1 ){ 【****】 【****】 } else{ nowSpeed++; } } public void speedDown( ) { if(nowSpeed-1<0){ 【****】 else{ nowSpeed--; } } public void showSpeed( ) { 【****】 } public void setPeople(int people){ 【****】 showPeople( ); } public void ridePeople(int ride) { int tmp = nowPeople + ride; if(【****】 ) { 【****】; } else { 【****】; } showPeople( ); } public void showPeople( ) { System.out.println(【****】) ; } public void show(){ System.out.println("タイヤ数" + tire); System.out.println("ガソリン" + gas); System.out.println("乗員数:" + people); } } 実行結果 >java CarApp2 ガソリン残量:45.0 定員数:8 現在の乗員数:0 定員オーバーです 定員数:8 現在の乗員数:0 定員数:8 現在の乗員数:3 速度:180 現在の速度:179 速度:180 現在の速度:180 最大速度です 速度:180 現在の速度:180 最大速度です 速度:180 現在の速度:1 速度:180 現在の速度:0 既に停止しています 速度:180 現在の速度:0 既に停止しています ___________________ (2) /* RaceCarApp2.java */ public class RaceCarApp2{ public static void main(String[] args){ RaceCar2 rc2 = new RaceCar2("JapanTour", "TouringCar",350); System.out.println("乗員数:" + rc2.getPeople()); System.out.println("タイヤ数:" + rc2.getTire()); System.out.println("ガソリン残量:"+rc2.getGas()); rc2.show(); for(int i=0; i<349; i++) rc2.speedUp(); for(int i=349; i<352; i++){ rc2.showSpeed(); rc2.speedUp(); } for(int i=0; i<349; i++) rc2.speedDown(); for(int i=349; i<352; i++){ rc2.showSpeed(); rc2.speedDown(); } } } _______________________________ class RaceCar2 extends Car{ String raceName; String type; public RaceCar2( String raceName, String type , int speed ){ 【****】; //親のコンストラクタ this.raceName = raceName; this.type = type; 【****】; //スピード設定 } public void show(){ 【****】; 【****】; 【****】; } } 実行結果 >java RaceCarApp2 乗員数:0 タイヤ数:4 ガソリン残量:30.0 Type:TouringCar RaceName:JapanTour タイヤ数4 ガソリン30.0 乗員数:1 速度:350 現在の速度:349 速度:350 現在の速度:350 最大速度です 速度:350 現在の速度:350 最大速度です 速度:350 現在の速度:1 速度:350 現在の速度:0 既に停止しています 速度:350 現在の速度:0 既に停止しています __________________________ この様になるように【****】部分を埋め回答せよという問題です。 途中まで自分でやってみましたが、エラーが出てしまうため断念してしまいました。 回答お願いします。

  • Java
  • 回答数3
  • ありがとう数0

みんなの回答

  • jjon-com
  • ベストアンサー率61% (1599/2592)
回答No.3

質問文において,(2)の実行結果の★の箇所が異なっているのは間違いでしょう。 -------- >java RaceCarApp2 乗員数:0 ★ タイヤ数:4 ガソリン残量:30.0 Type:TouringCar RaceName:JapanTour タイヤ数4 ガソリン30.0 乗員数:1 ★ -------- 質問文の(2)では,setPeople()で乗車定員を変化させている箇所はありませんから,★はどちらも同じ値が出力されるはずです。

  • jjon-com
  • ベストアンサー率61% (1599/2592)
回答No.2

(1) public void speedUp( ) { if(speed < nowSpeed+1 ){ System.out.println("最大速度です"); } else{ nowSpeed++; } } public void speedDown( ) { if(nowSpeed-1<0){ System.out.println("既に停止しています"); } else{ nowSpeed--; } } public void setPeople(int people){ this.people = people; showPeople( ); } public void ridePeople(int ride) { int tmp = nowPeople + ride; if(people >= tmp) { nowPeople = tmp; } else { System.out.println("定員オーバーです"); } showPeople( ); }

  • wormhole
  • ベストアンサー率28% (1619/5653)
回答No.1

>途中まで自分でやってみましたが、エラーが出てしまうため断念してしまいました。 その途中までやったことやエラーの内容を書かれないのはなぜでしょうか? 回答だけ聞いて何が原因だったのか自分で理解できるようになれますか?

route_32
質問者

補足

エラーはこちらで対処出来ました。 class Car { private int tire , people , speed ; private int nowPeople,nowSpeed; private double gas; public Car2(int tire, double gas, int people){ this.tire = tire; this.gas = gas; this.people = people; speed = 180; nowPeople = 0; nowSpeed = 0; } public double getGas(){ return gas;} public int getPeople() { return people; } public int getTire() { return tire; } public int getSpeed() { return speed; } public void setSpeed(int speed){ this.speed = speed; } public void speedUp( ) { if(speed < nowSpeed+1 ){ nowSpeed = speed ; //** speed ++; //** } else{ nowSpeed++; } } public void speedDown( ) { if(nowSpeed-1<0){ nowSpeed=0; System.out.println("既に停止しています"); //** }else{ nowSpeed--; } } public void showSpeed( ) { System.out.println("速度:"+speed+"現在"+nowSpeed); //** } public void setPeople(int people){ //**** showPeople( ); } public void ridePeople(int ride) { int tmp = nowPeople + ride; if(tmp >= nowPeople){//if(【****】 ) { nowPeople++; //【****】; } else { System.out.println("定員オーバーです"); // 【****】; } showPeople( ); } public void showPeople( ) { System.out.println("定員数:"+people+" 現在の乗員数:"+nowPeople) ; //*** } public void show(){ System.out.println("タイヤ数" + tire); System.out.println("ガソリン" + gas); System.out.println("乗員数:" + people); } } /*実行結果 乗員数:5 タイヤ数:4 ガソリン残量:45.0 定員数:5 現在の乗員数:0 定員数:5 現在の乗員数:1 定員数:5 現在の乗員数:2 速度:180現在179 速度:180現在180 速度:181現在180 速度:181現在2 速度:181現在1 速度:181現在0 既に停止しています */ ________ class RaceCar2 extends Car{ String raceName; String type; public RaceCar2( String raceName, String type , int speed ){ super(4,30,0); //【***】; this.raceName = raceName; this.type = type; setSpeed(speed); //【***】; } public void show(){ System.out.println("Type:"+type); //【***】; System.out.println("RaceName:"+raceName); //【***】; super.show(); //【***】; } } /* 乗員数:0 タイヤ数:4 ガソリン残量:30.0 Type:TouringCar RaceName:JapanTour タイヤ数4 ガソリン30.0 乗員数:0 速度:350現在349 速度:350現在350 速度:351現在350 速度:351現在2 速度:351現在1 速度:351現在0 既に停止しています */ 現状ではこうなっています。

関連するQ&A

  • javaの問題

    import java.util.Scanner; class test6{ static void printArray(int[] a) { for(int i=0;i<a.length;i++) System.out.print(String.format("%1$6s", (a[i]+" "))); System.out.println(); } static void printArray(int[][] a) { for(int[] i:a) { for(int j:i) System.out.print(String.format("%1$6s",(j+"\t"))); System.out.println(); } } public static void main(String[] args) { int[] a = {12,536,-8,7}; int[][] c = {{32,-1,32,45,67}, {535,99999,2}, {2,5,-123,9}}; printArray(a); printArray(c); } } このプログラムを表示するとこのようになるのですが、 12 536 -8 7 32 -1 32 45 67 535 99999 2 2 5 -123 9 これに一工夫加えて見やすくしたいです。 このような形にするのはどのようにすればよいのでしょうか + + | 32 -1 45 67 | | 535 99999 2 | | 2 5 -123 9 | + +

  • 至急!java オブジェクト指向

    昨日、質問させていただいた者です。おかげさまで、試合の状況を出力するプログラムが完成しました。あとはこれをオブジェクト指向のプログラムにしたいので、BSO()、Runner()、Score()のメソッド等を別々のクラスに記述して動作させるようにしたのですが、エラーばかりで起動できませんでした。インターフェースや継承などを使っていただいて構わないので、できれば改良のほうをお願いします。 import java.io.*; public class test1 { public static void main(String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int sc=0,bc=0,oc=0,rc1=0,rc2=0,rc3=0,tenA=0,tenB=0,i=0; BSO(sc, bc, oc); Runner(rc1,rc2,rc3); Score(tenA,tenB,i); while(true){ i++; while(true){ System.out.println("ピッチャー、投げた!"); String str=br.readLine(); char hantei=str.charAt(0); if(hantei=='s'){ System.out.println("ストライク!"); sc++; }else if(hantei=='b'){ System.out.println("ボール!"); bc++; }else if(hantei=='o'){ System.out.println("アウト!"); oc++; bc=sc=0; }else if(hantei=='h'){ System.out.println("ヒット!"); bc=sc=0; }else if(hantei=='x'){ System.out.println("ホームラン!"); bc=sc=0; }else{ System.out.println("入力が違います。"); continue; } if(sc==3){ System.out.println("ストライク!バッターアウト!"); oc++; sc = bc =0; }else if(bc==4){ System.out.println("フォアボール!"); sc = bc =0; } if(oc >= 3){ System.out.println("スリーアウト!"); break; } System.out.println("走者は出たか、又は進塁したか?"); String str6=br.readLine(); char kakuninn=str6.charAt(0); if(kakuninn=='y'){ System.out.println("一塁は"); String str2=br.readLine(); char r1=str2.charAt(0); if(r1=='y'){ rc1=1; }else if(r1=='n'){ rc1=0; }else{ System.out.println("入力が違います。"); continue; } System.out.println("二塁は"); String str3=br.readLine(); char r2=str3.charAt(0); if(r2=='y'){ rc2=1; }else if(r2=='n'){ rc2=0; }else{ System.out.println("入力が違います。"); continue; } System.out.println("三塁は"); String str4=br.readLine(); char r3=str4.charAt(0); if(r3=='y'){ rc3=1; }else if(r3=='n'){ rc3=0; }else{ System.out.println("入力が違います。"); continue; } }else{ } System.out.print("得点は? :"); String str7=br.readLine(); int tokuten=Integer.parseInt(str7); if(i%2==1){ tenA=tenA+tokuten; }else{ tenB=tenB+tokuten; } BSO(sc, bc, oc); Runner(rc1,rc2,rc3); Score(tenA,tenB,i); } oc=0; rc1=rc2=rc3=0; BSO(sc, bc, oc); Runner(rc1,rc2,rc3); Score(tenA,tenB,i); if(i==6){ System.out.println("ゲームセット!"); break; }else{ } } Score(tenA,tenB,i); } public static void BSO(int sc, int bc, int oc){ System.out.println("---- BSOカウント ----"); System.out.print("B "); for(int i=1;i<=bc;i++){ System.out.print("〇"); } System.out.println(""); System.out.print("S "); for(int i=1;i<=sc;i++){ System.out.print("〇"); } System.out.println(""); System.out.print("O "); for(int i=1;i<=oc;i++){ System.out.print("〇"); } System.out.println("\n------------------"); } public static void Runner(int rc1,int rc2,int rc3){ System.out.println("***走者の有無***"); if(rc2==1){ System.out.println(" ▲ "); }else if(rc2==0){ System.out.println("  △ "); } if(rc3==1){ System.out.print("▲  "); }else if(rc3==0){ System.out.print("△  "); } if(rc1==1){ System.out.println("▲"); }else if(rc1==0){ System.out.println("△"); } System.out.println("***************"); } public static void Score(int tenA,int tenB,int i){ if(i==0||i==1||i==2){ System.out.print("1回"); }else if(i==3||i==4){ System.out.print("2回"); }else if(i==5||i==6){ System.out.print("3回"); } if(i==0||i%2==1){ System.out.println("表"); }else{ System.out.println("裏"); } System.out.println(tenA+" "+"-"+" "+tenB); } }

    • ベストアンサー
    • Java
  • Javaの戻り値のコード

    Java初心者です。returnの例文で分からない所があります。 (1)考え方として car1.setNumGas(1234, 20.5); の時点で void setNumGas(int n, double g) はvoid setNumGas(1234, 20.5)になり 次にgetNum( ) そのものを引っ張ってきているので、 ナンバーを調べました という文字まで一緒に引っ張らないのでしょうか? なぜ 次の行のretun文だけを拾うのですか? (2)returnのイメージがまだ出来ていません。 int number = car1.getNum(); とした時点で int getNum()  にジャンプし、 その中の returnを見つけ、 そしてreturnが numを使っている所をコードを下に降りて順に探しに行く というイメージで合ってますか? -------------------------------------------------------------------- return num; →よーし探しに行こう! まずdouble getGas()の中 → うーんない 次にvoid setNumGas(int n, double g) の中 { num = n;    →   お、あった! nは今1234だな、だから表示結果は1234 --------------------------------------------------------------------------- こんな認識で合ってますでしょうか? class Car { int num; double gas; int getNum() { System.out.println("ナンバーを調べました。"); return num; } double getGas() { System.out.println("ガソリン量を調べました。"); return gas; } void setNumGas(int n, double g) { num = n; gas = g; System.out.println("車のナンバーを" + num + "にガソリン量を" + gas + "にしました。"); } void show() { System.out.println("車のナンバーは" + num + "です。"); System.out.println("ガソリン量は" + gas + "です。"); } } class Sample6 { public static void main(String[] args) { Car car1 = new Car(); car1.setNumGas(1234, 20.5); int number = car1.getNum(); double gasoline = car1.getGas(); System.out.println("サンプルから車を調べたところ"); System.out.println("ナンバーは"+ number + "ガソリン量は" + gasoline + "でした。"); } }

    • ベストアンサー
    • Java
  • 初心者で練習問題をしています

    Javaで練習問題をしています Carというclassを作成して(public, privateを明示) フィールドはStringでmake(型式),colour(色),intでfuel(燃料)です。 コントラクタを作成して、 car1:make(Ferarri),colour(red),intでfuel(100) car2:make(Volvo),colour(blue),intでfuel(200) car3 = car2、 ↓ car2では色をgreenにし、car3では燃料を20使うと car2:make(Volvo),colour(green),intでfuel(180) car3:make(Volvo),colour(green),intでfuel(180) 両方とも180になってしまいますが、 (greenはいっしょになってかまいません) car2の燃料を200のまま減らないようにするには どうしたらいいでしょうか どうぞよろしくお願いします。 Public class Car { . . public void setColour(String newColour) { colour = newColour; } public void useFuel(int amount) { fuel = fuel - amount; } public static void main(String[] args) { Car car2 = new Car(); car2.make = "Volvo"; car2.colour = "blue"; car2.fuel = 200; System.out.println("car2make="+car2.make); System.out.println("car2colour="+car2.colour); car2.toString(car2.colour,car2.make); Car car3 = new Car(); car3 = car2; car2.setColour("green"); System.out.println("car2colour=blue -> green"); System.out.println("car2colour="+car2.colour); System.out.println("car3colour="+car3.colour); car3.useFuel(20); System.out.println("car2fuel="+car2.fuel); System.out.println("car3fuel="+car3.fuel); } . .

    • ベストアンサー
    • Java
  • java

    次のコード中の括弧内で下に示す11通りの各コードを実行した場合の実行画面を正確に答えよ。 class A{ public void func1(){System.out.println("A1");} public void func2(){System.out.println("A2");} } class B extends A{ public void func1(){System.out.println("B");} } class C{ public int x=0, y=1; } class D extends C{ public int x=2; public void func1(int x){System.out.println(x);} public void func2(int x){System.out.println(this.x);} public void func3(int x){System.out.println(super.x);} public void func4(int x){System.out.println(this.y);} public void func5(int x){System.out.println(super.y);} } class E{ public void func1(int n){ try{ System.out.println("E1"); int[] ary=new int[n]; System.out.println("E2"); }catch(NegativeArraySizeException e){ System.out.println("E3"); }finally{ System.out.println("E4"); } } } (1) A a=new A(); a.func1(); (2) A a=new B(); a.func1(); (3) B b=new B(); b.func1(); (4) B b=new B(); b.func2(); (5) D d=new D(); d.func1(3); (6) D d=new D(); d.func2(3); (7) D d=new D(); d.func3(3); (8) D d=new D(); d.func4(3); (9) D d=new D(); d.func5(3); (10) E e=new E(); e.func1(5); (11) E e=new E(); e.func1(-2); (1) A1 (2)B (3)B (4) A2 (5) 3 (6) 2 (7) 0 (8) 1 (9) 1 (10) E1E2E4(11) E1E3E4 と答えになるんですがなぜこうなるのかわかりません。教えてください

    • ベストアンサー
    • Java
  • javaのプログラムについて

    java始めたばかりの者です。 class sample1 { public static void main(String[] args) { for(int i=1; i<=5; i++){ for(int j=0; j<i; j++){ System.out.println("*"); } System.out.println("\n"); } } } これを実行すると * * * * * * * * * * * * * * * こうなってしまい * ** *** **** ***** このようにするにはどうしたらいいのでしょうか??

    • ベストアンサー
    • Java
  • 簡単なマインスイーパーをJavaで作りたいです。

    Javaの勉強をしたいと思い下のソースのマインスイーパーを ウィンドウ上で実行できるようにしたいです。 そのときに他のソースから呼び出す形にしたいです。 どのようにしたら良いでしょうか?教えてください。 出来ればソースがあるとありがたいです。 import java.util.Scanner; public class MineSweeper extends MinePrinter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); MineSweeper game = new MineSweeper(); game.displayStrblock(); while (!game.dead) { System.out.println("\n:爆弾数・・・" + game.mines); System.out.println("\n:場所を選んでください"); System.out.println("縦の場所の数字を入力"); int tate = Integer.parseInt(sc.next()); System.out.println("横の場所の数字を入力"); int yoko = Integer.parseInt(sc.next()); game.mineClick(tate, yoko); game.displayStrblock(); } } } class MinePrinter { String[][] strblock = new String[5][5]; int[][] blocks = new int[5][5]; int mines = 0; boolean dead = false; public MinePrinter() { mineSet(); } void mineClick(int i, int j) { if (aroundManyMines(i, j) == 99) { System.out.println("burst!!!"); System.out.println("You Dead!!\n"); strblock[i][j] = "*"; dead = true; } else { strblock[i][j] = Integer.toString(aroundManyMines(i, j)); } } void mineSet() { for (int i = 0; i < blocks.length; i++) { for (int j = 0; j < blocks[i].length; j++) { if (Math.random() < 0.2) { blocks[i][j] = 99; mines += 1; } else { blocks[i][j] = 0; } } } for (int i = 0; i < strblock.length; i++) { for (int j = 0; j < strblock.length; j++) { strblock[i][j] = "#"; } } } void displayStrblock() { for (int i = 0; i < strblock.length; i++) { for (int j = 0; j < strblock.length; j++) { System.out.print(strblock[i][j] + " "); } System.out.println(":" + i); } } int aroundManyMines(int i, int j) { if (blocks[i][j] == 99) { return 99; } int ans = 0; for (int r = -1; r <= 1; r++) for (int c = -1; c <= 1; c++) { try { if (blocks[i + r][j + c] == 99) { ans += 1; } } catch (ArrayIndexOutOfBoundsException e) { continue; } } return ans; } }

    • ベストアンサー
    • Java
  • 間違い箇所を教えてください。

    サブクラスを記述する下記のプログラムについて質問します。 class Car { //タイヤ private String tire = ""; public Car() { tire = "タイヤ"; } /* *どんなタイヤであるかを設定 *value タイヤの種類 */ public void setTire(String value) { tire = value + "のタイヤ"; } /* *作成されたタイヤを返します *return タイヤ */ public String getTire() { return tire; } } class Motorcar extends Car { private String tire = ""; private String body = ""; private String handle = ""; String type = ""; public Motorcar() { tire = "タイヤ"; body = "車体"; handle = "ハンドル"; } public String getTire() { return tire; } public String getBody() { return body; } public String getHandle() { return handle; } public String createMotorcar(String t) { type = t; tire = type + "のタイヤ"; return tire; } } public class Sample1 { public static void main(String atgs[]) { Motorcar car = new Motorcar(); String type = "トラック"; String tire = car.getTire(); String body = car.getBody(); String handle = car.getHandle(); System.out.println("タイヤ=" + tire); System.out.println("車体=" + body); System.out.println("ハンドル=" + handle); //どんな車かを設定 car.createMotorcar(type); tire = car.getTire(); body = car.getBody(); handle = car.getHandle(); System.out.println(type + "の作成!!"); System.out.println("タイヤ=" + tire); System.out.println("車体=" + body); System.out.println("ハンドル=" + handle); } } このまま実行すると、後半の System.out.println("タイヤ=" + tire); System.out.println("車体=" + body); System.out.println("ハンドル=" + handle); この部分の出力結果が タイヤ = タイヤ 車体 = 車体 ハンドル = ハンドル となりますよね? それを タイヤ = トラックのタイヤ 車体 = トラックの車体 ハンドル = トラックのハンドル という風にしたいのですが、どう記述したらよいのでしょうか? //どんな車かを設定 car.createMotorcar(type); tire = car.getTire(); body = car.getBody(); handle = car.getHandle(); ここでメソッドを呼び出したときに tire・body・handleのそれぞれに「トラックの」という言葉が 追加されるようにメソッドを記述しなければいけないと 思うのですが、最初に呼び出すときは必要ないので どうすればよいのかが分かりません。 教えて下さい、お願いします。 長くてすみません・・・

  • クラスのフィールドの値を配列に代入する方法

    フィールドの値を代入したいのですが、 java.lang.ArrayIndexOutOfBoundsException というエラーが出てしまい、どうすればいいのかわかりません。 どのように対処すればいいのでしょうか? class Car { String carname; int height; int width; int length; } class public static void main(String[] args) throw IOException { Car car[]; car = new Car[5]; System.out.println("車の情報を入力してください。"); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in); for(int i=0; i<5; i++){ car[i] = new Car(); System.out.println(i+1 + "台目の車の名前を入力してください。"); String str1 = br1.readLine(); System.out.println("車の高さを入力してください。"); String str2 = br1.readLine(); System.out.println("車の幅を入力してください。"); String str3 = br1.readLine(); System.out.println("車の長さを入力してください。"); String str4 = br1.readLine(); int h1 = Integer.parseInt(str2); int w1 = Integer.parseInt(str3); int l1 = Integer.parseInt(str4); car[i].carname = str1; car[i].height = h1; car[i].weith =w1; car[i].length =l1; } for(int i=0; i<5; i++){ int c_h[] = new int[5]; c_h[i] = car[i].height; } } よろしくお願いします。

    • ベストアンサー
    • Java
  • JAVAコンストラクタについて

    JAVA のコンストラクタ定義の際に、下記のようにコンストラクタにvoidを付けた時と付けない時の処理結果が変わるのはなぜでしょうか。 (1) class O { protected int d; O(){ System.out.println("O = " + d); } } class Study_6_3main_void { public static void main(String args[]) { int i = 1; O OO = new O(); System.out.println("O = "); } } (2) class O { protected int d; void O(){ System.out.println("O = " + d); } } class Study_6_3main_void { public static void main(String args[]) { int i = 1; O OO = new O(); System.out.println("O = "); } } 実行結果 (1) O = 0 O = (2) O = 以上です。回答の程よろしくおねがいします。

    • ベストアンサー
    • Java

専門家に質問してみよう