• 締切済み

インスタンスの情報表示について

Java初心者です。 例えば次のようなソースがあって、double型の変数numの情報を表示するためにtoStringをオーバーライドするとすると、どのようにすればStringを返せるのでしょうか。 public class Test { private double[] num; public Test(double a, double b, double c) { num = new double[3]; this.num[0]=a; this.num[1]=b; this.num[2]=c; } public toString() { // return "インスタンスは: "+ } public static void main(String args[]) { Test a = new Test(4.3, 5.6, 12.7); System.out.println(a.toString()); } }

  • Java
  • 回答数1
  • ありがとう数1

みんなの回答

  • kozikoji
  • ベストアンサー率29% (5/17)
回答No.1

java.lang.Stringクラスの、 「ValueOf」メソッドで、double値のStringが取得できます。

rio_grande
質問者

お礼

格好悪いコードですがとりあえず自己解決しました。 public String toString() { String str=""; for(int i=0; i<this.num.length; i++) { str=str+this.num[i]+" "; } return "インスタンスは: "+str; }

関連するQ&A

  • 内部クラスのインスタンスを動的に取得したい

    class test {   public test(String str){     try{       Class c = Class.forName("test$TESTSUB" + str);       c.newInstance();  // ココでエラー     }catch(Exception e){       e.printStackTrace();     }   }   public static void main(String[] args){     new test(args[0]);   }   class TESTSUB1 {     public TESTSUB1(){       System.out.println("# " + toString());     }   }   class TESTSUB2 {     public TESTSUB2(){       System.out.println("# " + toString());     }   } } 上記のようなコードにより、内部クラスのインスタンスを 動的に得たいと思っていますが、エラーとなってしまいます。 エラー回避方法、または他にいい方法がありましたら、 ご教示のほどよろしくお願いいたします。

    • ベストアンサー
    • Java
  • javaプログラム 合ってるかお願いします

    プログラムは非常に苦手なため合ってないと思いますが、合ってないのは何が正解かを教えてください。 よろしくお願いします 1 ローカル変数を全て答えよ args,num 2 インスタンス変数を全て答えよ num 3 インスタンスメソッド名を答えよ Example2 4インスタンスメソッドを呼び出している行 public class Example2{ int num; public static viod main(String[] args){ Example2 ex2 = new Example2(5); int a = ex2.getNum(); System.out.println(a); } Example2(int num){ this.num=num; } int getNum(){ return this.num; } }

    • ベストアンサー
    • Java
  • コマンドライン引数の*(アフタリスク)の表示について教えて下さい

    皆様どうぞよろしくお願い申し上げます。アドバイス下さい。 java言語で質問です。調べたかぎりわからないことが起こりました。次のものです。コマンドライン引数で引数1は数字、引数2は数字、引数3は* で表示させるプログラムを作りました。引数3番目の演算子*(アフタリスク)を表示する場合にうまく表示できません。 C:\java>java test 2.0 15.0 * 2.0 15.0 Calculation.class ←表示されません。 プログラムは以下のものを作りました。見てください。 /** * 実行クラス:引数テストクラス */ class test{ public static void main(String args[]) { //引数を取得 double num1 = Double.parseDouble(args[0]); //引数1(数値) double num2 = Double.parseDouble(args[1]); //引数2(数値) String kigou = args[2]; //引数3(計算記号) //画面に出しています。 System.out.println(num1); System.out.println(num2); System.out.println(kigou); } }

  • Java 型指定を伴わないインスタンスの作成?

    問題集で以下のプログラム(一部)を見かけました。 class Test{ public static void main(String args[]){ new Animal("A"); } } Animalクラスのインスタンスを作成しているということなのですが、 これまでであれば、 Animal a = new Animal(); というようにイコールの左で インスタンスの型と変数を指定する形で学んで来ました。 型や変数を指定しないでインスタンスの作成をすることが できるのですか? 回答をお願いします。

    • ベストアンサー
    • Java
  • サブクラスで.newInstance()でインスタンス化できない

    以下のようにClass.forNameで取得したクラスをインスタンス化 しようとしていますが、できません サブクラスは.newInstance()でインスタンス化できないのでしょうか? 代替の方法はありますか? ーーーーーーーーーーーーーーーー public class test4121 { public static void main(String[] args) throws Throwable { new test4121(); } public test4121() throws Throwable { //これは実行できるが test4121_2 test = new test4121_2(); //これはできない Class.forName("test4121$test4121_2").newInstance(); } class test4121_2{ } } ーーーーーーーーーーーーーーーー

  • 小数点以下の表示が正しく表示されなくて困ってます

    public class test{ public static void main(String args[]){ double a = .05; System.out.println(a*3.0); } } というプログラムなんですけども。 アウトプットを表示すると >java test 0.15000000000000002 っというふうに表示されてしまいます。 プログラム自体を変えずに、0.15という表示を出したいのですが、どうしてもでません。誰か、教えていただければありがたいと思っています。よろしくお願いします。

    • ベストアンサー
    • Java
  • [Java]Javaの文法が間違っているのでしょうか・・・?

    6人分の小テストの点数を記録し,平均点,最高点,平均点以下の人の一覧を計算するプログラムを作成したいのです。 入力はコマンドラインから行います。 しかし,TEST配列がうまく初期化がうまく記述できないため,コンパイル時点で”シンボルが見つけられません”というエラーが出てしまいます。 Javaの経験が浅いので、文法そのものが間違っているか心配です。C言語についてはある程度知識がありますから、C言語と対比して教えて頂けたりすると大変たすかります。 宜しくお願い致します。 class Lecture { static Lecture[] TEST; static double avg=0; static int max=0; static int i = 0; static String kamoku; int scorebox; String name; String student_number; Lecture(int size){ TEST = new Lecture[size]; for (int i = 0; i < size; i++) { Exercise a = new Exercise(); Student b = new Student(); TEST[i] = new Lecture(a,b); } } static void add(Exercise score, Student aStudent) { TEST[i].name = aStudent.name; TEST[i].student_number = aStudent.student_number; TEST[i].scorebox = score.score; i++; } static void avg() { int sum=0; for (int i = 0; i < 6; i++) { sum += TEST[i].scorebox; } avg = sum / 6; } static void max() { int max = 0; int temp = 0; for (int i = 0; i < 6; i++) { if (max < TEST[i].scorebox) { temp = i; max = TEST[i].scorebox; } } max = i; } static void show_kamoku(){ System.out.println("科目:"+kamoku); } static void show(){ Lecture.max(); System.out.println("平均点:"+avg); System.out.println("最高得点者:"+TEST[max]); } static void under_avg(){ Lecture.avg(); System.out.println("平均点を下回った者"); for(int i=0 ; i<6 ; i++){ if(avg > TEST[i].scorebox){ System.out.println(TEST[i]); } } } } class Exercise { int score; Exercise(int score) { this.score = score; } Exercise() { } public String toString() { return " 得点:" + score; } } class Student { String name; String student_number; Student(String student_number,String name ) { this.name = name; this.student_number = student_number; } Student() { } public String toString() { return "学籍番号:" + student_number + " 名前:" + name; } } class ExerciseEvaluation { public static void main(String args[]){ Lecture.kamoku=args[0]; Lecture[] lec = new Lecture[6]; Student Y0 = new Student(args[1],args[2]); Exercise X0 = new Exercise(Integer.parseInt(args[3])); lec[0].add(X0, Y0); Student Y1 = new Student(args[4], args[5]); Exercise X1 = new Exercise(Integer.parseInt(args[6])); lec[1].add(X1, Y1); Student Y2 = new Student(args[7], args[8]); Exercise X2 = new Exercise(Integer.parseInt(args[9])); lec[2].add(X2, Y2); Student Y3 = new Student(args[10], args[11]); Exercise X3 = new Exercise(Integer.parseInt(args[12])); lec[3].add(X3, Y3); Student Y4 = new Student(args[13], args[14]); Exercise X4 = new Exercise(Integer.parseInt(args[15])); lec[4].add(X4, Y4); Student Y5 = new Student(args[16], args[17]); Exercise X5 = new Exercise(Integer.parseInt(args[18])); lec[5].add(X5, Y5); Lecture.show_kamoku(); Lecture.show(); Lecture.under_avg(); } }

  • equalsメソッドの実装

    いくつかの本をインスタンスで生成します。この本を4つ生成するときに、3つ目に用意したインスタンスと4つ目に用意したインスタンスの中身が等しい=trueという処理をおこないたいのですが、調べてみた結果、自分でequalsを実装しなくてはならないらしく、その実装ができずにこまっています。equalsメソッドがどんな形になるか、教えていただけないでしょうか? class Book{ String name;//書名 String author;//著者 String publisher;//出版 /*コンストラクタ*/ Book( String name, String author, String publisher, int number){ this.name = name;//書名 this.author = author;//著者 this.publisher = publisher;//出版社 } } class BookShelf{ public static void main(String[] args){ /*インスタンスを作成*/ Book aBook_A = new Book("書名1","Aさん","A出版"); Book aBook_B = new Book("書名2","Bさん","B出版"); Book aBook_C = new Book("書名3","Cさん","C出版"); Book aBook_D = new Book("書名3","Cさん","C出版"); System.out.println(aBook_C.equals(aBook_d));//falseが返される。 } }

  • インスタンス変数について

    JAVA勉強中のものです。 インスタンス変数についてわからないのですが、 class Data{int d;} classSample{ public static void main(String[] args){ Data data1 =new Data(); Data data2 =new Data(); data1.d = 100; data2 = data1; data2.d = 200; System.out.println(data1.d); } } 実行すると200が表示されますが、new演算子によってできたオブジェクトの実体を参照する ID がdata1およびdata2に入るのは理解できるのですが、 インスタンス変数のdはメモリ上でどう設定されているかイメージがわきません。 わかる方どうぞ教えて下さい。

    • ベストアンサー
    • Java
  • Runnableのインスタンス化について

    下記のコードについてです。 Runnableがインスタンス化されていますが、 どうしてそれが可能なのかが分かりません。 分からない点は 1.Runnableはインタフェースであるから、本来、直接インスタンス化は不可能であるはず。 2.しかし、Runnableはクラスライブラリjava.langパッケージに含まれている。 だからインスタンス化は可能なのかもしれない。 3.あるいは、下記のコードではメソッド内の無名クラスであるから、「new Runnable(){」の 部分でスーパークラスとしてのRunnableを継承したサブクラスを生成しているのかもしれない。 アドバイスをよろしくお願い致します。 public class Main{ public static void main(String[]args){ Runnable task = new Runnable(){ public void run(){ System.out.println("run"); } }; Thread thread = new Thread(task){ public synchronized void start(){ System.out.println("start"); } }; thread.start(); } }

    • ベストアンサー
    • Java

専門家に質問してみよう