質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

1442閲覧

タワーのブロックの数を数えるプログラム

TomofumiKimura

総合スコア65

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2017/03/19 07:39

最後のcountのmethod はinteger を返します。それはこのタワーのブロックの数と同じでそしてこのmethod が受けっとている、charとも同じと自分は理解しているのですが。自分の英語の理解は正しいのでしょうか?あるプログラムがチェックすると
2が返ってくるはずが 5が出ていますと出ます。どうしてでしょうか?

java

1 2package week03; 3 4/** 5 * A recursive representation of a tower of blocks. 6 * 7 * @author Michael Albert 8 */ 9public class Tower{ 10 11 /** The top block. */ 12 private char top; 13 14 /** The rest of the tower. */ 15 private Tower rest; 16 17 /** 18 * Creates a new empty Tower. 19 */ 20 public Tower() { 21 this.top = ' '; 22 this.rest = null; 23 } 24 25 /** 26 * External classes can only create empty towers and manipulate 27 * them using public methods, because this constructor is 28 * private. 29 * @param top the top block in this tower 30 * @param rest the rest of the tower 31 */ 32 private Tower(char top, Tower rest) { 33 this.top = top; 34 this.rest = rest; 35 } 36 37 /** 38 * Returns true if this tower is empty, otherwise false. Empty 39 * towers are represented with the top block being a space 40 * character. 41 * @return whether the tower is empty or not. 42 */ 43 public boolean isEmpty() { 44 return top == ' '; 45 } 46 47 /** 48 * Creates a new tower by adding the given block to the top of 49 * this tower. 50 * @param block a block to add to the top of this tower. 51 * @return a new tower created by adding a block to the top of 52 * this tower. 53 */ 54 public Tower add(char block) { 55 return new Tower(block, this); 56 } 57 /** 58 * A method that returns an int equal to the height, 59 * i.e., number of blocks, in the tower. 60 * @return an int equal to the height. 61 */ 62 public int height(){ 63 int count =0; 64 if (!(this.isEmpty())){ 65 count ++; 66 if (!(this.rest.isEmpty())){ 67 count += this.rest.height(); 68 69 } 70 71 } 72 return count; 73 74 } 75 /** 76 * A method that returns an int equal to the number of blocks equal to c in the tower. 77 * @return int equal to the number of blocks in the tower. 78 */ 79 80 public int count(char c){ 81 int count =0; 82 83 if (this.top == c){ 84 count = this.height(); 85 86 } 87 88 return count; 89 90 91 } 92 93 94 95 96 97 98} 99

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

can110

2017/03/19 08:21

「2が返ってくるはずが 5が出ています」となる具体的なコードを提示ください。
akabee

2017/03/19 08:22

なぜ2が返ってくるはずと思ったのかの経緯と、TowerクラスではなくTowerクラスを用いて処理を実行しているプログラムも記載していただかないとなんとも言えません。>そしてこのmethod が受けっとている、charとも同じ それは同じとは言えませんよ。topにAが入っていたとして、Aはそもそもintegerですらありませんからタワーのブロックの数と同じではありません。
guest

回答1

0

ベストアンサー

データ構造がややこしいと感じたらコツとして「絵にかいてみる」ことをお勧めします。(すでにそうされてるかも知れませんが・・・)

Tower empty = new Towoer(); ==> empty --> +----+ |' ' | |null| +----+ Tower one = empty.add('a'); ==> one --> +----+ |'a' | | ----->+----+ <-- empty +----+ |' ' | |null| +----+ Tower tow = one.add('b'); ==> two --> +----+ |'b' | | ----->+----+ <-- one +----+ |'a' | | ----->+----+ <-- empty +----+ |' ' | |null| +----+

もちろんテキストエディターとかじゃなくて紙にペンで書けば充分です!

  • topとheightの結果は関係ありません

回答コメントでakabeeさんがおっしゃっているように、topフィールドに入っているのは単にaddした際に渡した適当な文字ですので高さとは関係ないです。

  • 2が返ってくるはずが 5が出ています

heightメソッドを実装されたのですね。heightメソッドは(処理に若干冗長な点はあるものの)結果自体は正しく書けていると思います。多分上の絵の構造の「どのノードに対してheightを呼び出すと何が返ってくるか」という点に混乱があるのではないでしょうか?

上の絵の例ですと以下のようになれば正しいと言えます。

着目するTowerインスタンスheightを求めるコード結果着目するTowerのtopの値
emptyint h = empty.height();0' '
oneint h = one.height();1'a'
twoint h = two.height();2'b'

投稿2017/03/19 09:45

KSwordOfHaste

総合スコア18394

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問