• 締切済み

JAVAのComparableについて・・・

compareto()を使って名前、身長、体重を「身長昇順」拡張for文でだしたいのですが・・・ (1)returnを使って何を返せばいいのか? (2)System.out.println()でなにをすればいいのか? (3)他の文で間違っているのか? この3点が分かりません。JAVAに詳しい方よろしくお願いします。。。 package class; public class NewClass { public static void main(String[] args) { NewClass2 A = new NewClass2("aaa", 160, 40); NewClass2 B = new NewClass2("bbb", 170, 60); NewClass2 C = new NewClass2("ccc", 150, 70); System.out.println("---Comparable---"); Set<Comparable> ts3 = new TreeSet<Comparable>(); ts3.add(A); ts3.add(B); ts3.add(C); for(Comparable cm : ts3) { System.out.println(?????????????); } package class; public class NewClass2 implements Comparable< NewClass2> { private String name; private int Weight; private int Height; public String getName() { return this.name; } public void setName(String name) { this.name = name; } public int getWeight() { return this.Weight; } public void setWeight(int Weight) { this.Weight = Weight; } public int getHeight() { return this.Height; } public void setHeight(int Height) { this.Height = Height; } public StudentBean(String name, int Height, int Weight) { this.name = name; this.Weight = Weight; this.Height = Height; } @Override public int compareTo(Object arg0) { StudentBean sb = (StudentBean)arg0;     String name1 = this.name; int Height1 = this.Height; int Weight1 = this.Weight;     int returnval = 0; if(Height1 > sb.getHeight()) { returnval = 1; } else if(Height1 < sb.getHeight()) { returnval = -1; } else returnval = 0; } return ???????; } 文は以上です 出力は aaa 160 40 ・ ・ ・ なかんじでお願いします。

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

みんなの回答

回答No.2

(1)returnを使って何を返せばいいのか? No.1の方がおっしゃる通りです。 (2)System.out.println()でなにをすればいいのか? System.out.println()って何だかわかってらっしゃいます? (3)他の文で間違っているのか? 意味がよく分かりませんが、構文で間違っているところを… StudentBeanクラスなんですか? NewClass2クラスなんですか? このままだと、コンパイルが通りません。 どちらかに統一しましょう。

  • kmee
  • ベストアンサー率55% (1857/3366)
回答No.1

変数returnvalは何のために用意したのですか?

kingj1123
質問者

補足

え~と・・・returnvalの値でTreeSetが並び替える。って感じです

関連するQ&A

  • JAVAのComparableについて・・・

    compareto()を使って名前、身長、体重を「身長昇順」拡張for文でだしたいのですが・・・ (1)returnを使って何を返せばいいのか? (2)System.out.println()でなにをすればいいのか? (3)他の文で間違っているのか? この3点が分かりません。JAVAに詳しい方よろしくお願いします。。。 package class; public class NewClass { public static void main(String[] args) { NewClass2 A = new NewClass2("aaa", 160, 40); NewClass2 B = new NewClass2("bbb", 170, 60); NewClass2 C = new NewClass2("ccc", 150, 70); System.out.println("---Comparable---"); Set<Comparable> ts3 = new TreeSet<Comparable>(); ts3.add(A); ts3.add(B); ts3.add(C); for(Comparable cm : ts3) { System.out.println(?????????????); } package class; public class NewClass2 implements Comparable< NewClass2> { private String name; private int Weight; private int Height; public String getName() { return this.name; } public void setName(String name) { this.name = name; } public int getWeight() { return this.Weight; } public void setWeight(int Weight) { this.Weight = Weight; } public int getHeight() { return this.Height; } public void setHeight(int Height) { this.Height = Height; } public StudentBean(String name, int Height, int Weight) { this.name = name; this.Weight = Weight; this.Height = Height; } @Override public int compareTo(Object arg0) { StudentBean sb = (StudentBean)arg0;     String name1 = this.name; int Height1 = this.Height; int Weight1 = this.Weight;     int returnval = 0; if(Height1 > sb.getHeight()) { returnval = 1; } else if(Height1 < sb.getHeight()) { returnval = -1; } else returnval = 0; } return ???????; } 文は以上です 出力は aaa 160 40 ・ ・ ・ なかんじでお願いします。

  • java これはオーバーロードすればいいのでは?

    以下のようなコードについて質問です。 class Person { public static int count = 0; public String firstName; public String middleName; public String lastName; public int age; public double height; public double weight; Person(String firstName, String lastName, int age, double height, double weight) { Person.count++; this.firstName = firstName; this.lastName = lastName; this.age = age; this.height = height; this.weight = weight; } Person(String firstName, String middleName, String lastName, int age, double height, double weight) { this(firstName, lastName, age, height, weight); this.middleName = middleName; } public String fullName() { if(middleName == null){ return this.firstName + " " + this.lastName; }else{ return this.firstName + " "+middleName+ ""+ this.lastName; } } fullName() メソッドを条件分岐して、以下のようなメソッドで、ミドルネームを入れるかどうかで、条件分岐するということなのですが… class Main { public static void main(String[] args) { Person person1 = new Person("Kate", "Jones", 27, 1.6, 50.0); person1.printData(); Person person2 = new Person("John", "Christopher", "Smith", 65, 1.75, 80.0); person2.printData(); Person.printCount(); } } これって、引数にfullNameメソッドに何も指定してなくていいのでしょうか。 疑問に感じてしまいました。 また、同じfullNameメソッドという名前のメソッドで引数でmiddleNameの有無だけ変えてオーバーロードしてはだめなのでしょうか。

  • Javaの質問です。

    データに0より小さい数値がある場合、その行を取り除いて集計させ、エラーがある行だけを別ファイルに書きだしたいのですが、うまくいきません。 なにかアドバイスいただけますでしょうか? よろしくお願いします。 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; class Data { double value; Data(double value) { this.value = value; } void setData(double value) { this.value = value; } public String toString() { return String.format("%5.1f", value); } } class Statistics { Data min = new Data(999.9); Data max = new Data(0.0); double total = 0.0; void add(double value) { if (min.value > value) { min.setData(value); } if (max.value < value) { max.setData(value); } total += value; } } class Category { String category; Statistics height = new Statistics(); Statistics weight = new Statistics(); Statistics bmi = new Statistics(); int num = 0; Category(String category) { this.category = category; } void add(double height, double weight) { this.height.add(height); this.weight.add(weight); this.bmi.add(weight / (height / 100.0 * height / 100.0)); num++; } public String toString() { return category + "," + "身長" + "," + "最高" + "," + height.max + "\n" + category + "," + "身長" + "," + "最低" + "," + height.min + "," + "\n" + category + "," + "身長" + "," + "平均" + "," + String.format("%5.1f\n", height.total / num) + category + "," + "体重" + "," + "最高" + "," + weight.max + "," + "\n" + category + "," + "体重" + "," + "最低" + "," + weight.min + "\n" + category + "," + "体重" + "," + "平均" + "," + String.format("%5.1f\n", weight.total / num) + category + "," + "BMI" + "," + "最高" + "," + bmi.max + "\n" + category + "," + "BMI" + "," + "最低" + "," + bmi.min + "\n" + category + "," + "BMI" + "," + "平均" + "," + String.format("%5.1f\n", bmi.total / num); } } class Health { Category male = new Category("男"); Category famale = new Category("女"); Category total = new Category("全体"); void add(String sex, double height, double weight) { if (sex.equals("男")) { male.add(height, weight); } else { famale.add(height, weight); } total.add(height, weight); } public String toString() { return "" + male + "\n" + famale + "\n" + total + "\n"; } } public class Main { static final String FILENAME = "hw.csv"; static final String SEPARATOR = ","; static final int SEX = 2, HEIGHT = 3, WEIGHT = 4; public static void main(String[] args) throws Exception { Health health = new Health(); String line; BufferedReader file = new BufferedReader(new FileReader(FILENAME)); for (int number = 1; (line = file.readLine()) != null; number++) { String[] item = line.split(SEPARATOR); try { if(Integer.parseInt(item[HEIGHT]) <= 0 || Integer.parseInt(item[WEIGHT]) <= 0 || item[HEIGHT] == null || item[WEIGHT] == null) { PrintWriter pw = new PrintWriter( new BufferedWriter (new FileWriter("out_error.csv"))); pw.println(number); pw.close(); health.add(item[SEX],Double.parseDouble(item[HEIGHT]),Double.parseDouble(item[WEIGHT])); } } catch (Exception e) { } } file.close(); PrintWriter pw = new PrintWriter( new BufferedWriter (new FileWriter("out.csv"))); pw.println(health); pw.close(); } }

  • javaの課題が難しくて解けません。力を貸してください

    本日、プログラムの授業で課題が出されました。 解ける方がいましたら、回答を教えてください。 課題:これまでの演習で作成した Rectangle, Triangle, Circle, Trapezoidクラスを利用するクラス (メインクラス)を修正して, コマンドライン引数から図形の種類、入力値を指定できるようにしてみよ。 ※4つのクラスをpackageで1つにまとめる必要があるようです。 お手数かけますが、宜しくお願いします。 下記が演習で作成した4つのクラスになります。参考にしてください。 (Rectangleクラス) import java.io.*; class Rectangle { private int width; private int height; public Rectangle(int w, int h) { width = w; height = h; } public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } public int getWidth() { return width; } public int getHeight() { return height; } public double calcArea() { return width * height; } public void show() { System.out.println("width=" + getWidth() + ", height= " + getHeight()); } } public class kadai1 { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("width= "); String str1 = br.readLine(); System.out.print("height= "); String str2 = br.readLine(); Rectangle r = new Rectangle(Integer.parseInt(atr1),Integer.parseInt(str2)); r.show(); System.out.println("面積 = " + r.calcArea()); } } (Triangleクラス) public double calcArea() { return width * height / 2.0; } (Circleクラス) public double calcArea() { return Math.PI * radius * radius; } public void show() { System.out.println("radius= " + getRadius()); } (Trapezoidクラス) public double calcArea() { return (upper + lower) * height / 2.0; } public void show() { System.out.println("upper= " + getUpper() + ", lower= " + getLower() + ", height= " + getHeight()); }

  • JavaのArrays.sortについて教えてください

     Javaを勉強している初心者です。 次のプログラムはある学校のある学年の梅組の生徒5人のデータ(出席番号 no、氏名 name、身長 height)を 身長の低い順に並べ替えようとするものです。クラスMainでArrays.sort(ume);を実行するとどんな処理が行われるのか、またクラスStudentのメソッドcompareTo(Object o)はどう関係するのかご教示ください。 import java.util.Arrays; class Student implements Comparable { private int no; private String name; private double height; public Student(int no, String name, double height) { this.no = no; this.name = name; this.height = height; } public int compareTo(Object o) { double h1 = this.height; double h2 = ((Student)o).height; if(h1 == h2) { return 0; } else if(h1 > h2) { return 1; } else { return -1; } } } public class Main { public static void main(String[] args) { Student[] ume = new Student[5]; ume[0] = new Student(2, "木下 薫", 141.5); ume[1] = new Student(5, "湯水 敦", 145.0); ume[2] = new Student(1, "相田 徹", 152.5); ume[3] = new Student(4, "田中 大", 136.0); ume[4] = new Student(3, "橋 航", 145.0); Arrays.sort(ume); } }

  • 継承プログラムについて

    下記で作成した継承プログラムで、間違ったプログラムの部分を直しても同じエラーが発生します。どうしたら下記のプログラムがうまく動きますか? ちなみに同じエラーが発生するのはsuper()の部分で、新保ぷが見つからないとのこと。 class Human { private double height, weight; private String name; private static int count = 0; public Human() { height = 0; weight = 0; name = "no name"; } public void inputData(String na, double ht, double wt) { height = ht; weight = wt; name = na; } public void showValue() { count++; System.out.println("名前: " + name + ", 身長: " + height + "cm, 体重:"+ weight + "kg"); } public void copyData(Human a) { height = a.height; weight = a.weight; name = a.name; } public double readData(double a) { if(a==0){ return height; }else{ return weight; } } public static int readCount() { return count; } } class Sandai extends Human { private String number; public Sandai(String nu, String na, double ht, double wt) { super(na, ht, wt); number = nu; } public void showData() { System.out.println("学籍番号:"+number+", "); showValue(); } } class Kadai12_3 { public static void main(String args[]) { Sandai student = new Sandai("09H099", "Taro", 165.0, 55.0); student.showData(); } }

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

    下記で作成した継承プログラムで、間違ったプログラムの部分を直しても同じエラーが発生します。どうしたら下記のプログラムがうまく動きますか? ちなみに同じエラーが発生するのはsuper()の部分で、シンボルが見つからないとのこと. class Human { private double height, weight; private String name; private static int count = 0; public Human() { height = 0; weight = 0; name = "no name"; } public void inputData(String na, double ht, double wt) { height = ht; weight = wt; name = na; } public void showValue() { count++; System.out.println("名前: " + name + ", 身長: " + height + "cm, 体重:"+ weight + "kg"); } public void copyData(Human a) { height = a.height; weight = a.weight; name = a.name; } public double readData(double a) { if(a==0){ return height; }else{ return weight; } } public static int readCount() { return count; } } class Sandai extends Human { private String number; public Sandai(String nu, String na, double ht, double wt) { super(na, ht, wt); number = nu; } public void showData() { System.out.println("学籍番号:"+number+", "); showValue(); } } class Kadai12_3 { public static void main(String args[]) { Sandai student = new Sandai("09H099", "Taro", 165.0, 55.0); student.showData(); } }

    • ベストアンサー
    • Java
  • 初心者Javaの件。

    いつも大変お世話になりありがとうございます。 次の2つのコードは関係があるのでしょうか? 2つを合体させないとプログラムがエラーになるのでしょうか? 私は合体させないとプログラムが成立しないと思います。 アドバイスのほど宜しくお願い申し上げます。 コード1 public class Person { private int age; private double weight; private double height; public Person(int age, double weight, double height) { this.age = age; this.weight = weight; this.height = height; } public void show() { System.out.println("年齢は" + age + "体重は" + weight + "身長は"+ height +"です。" ); } } コード2 import java.io.BufferedReader; public class Sample3 { { public static void main(String args[]) throws Exception { person p[]; System.out.println("人数を入力してください。"); BufferedReader br = new BufferedReader(new inputStreamReader(System.in)); String str = br.readLine(); int num = Integer.parseInt(str); p = new Person[num]; for(int i=0; i<num; i++) { } System.out.println("年齢を入力してください"); str = br.readLine(); int age = Integer.parseInt(str); P = new Person[num]; for(int i=0; i<num; i++){ System.out.println("年齢を入力してください"); str = br.readLine(); int age = Intger.parseInt(str); System.out.println("体重を入力してください"); str = br.readLine(); double weight = Double.parseDouble(str); System.out.println("身長を入力してください"); str = br.readLine(); double height = Double.parseDouble(str); p[i] = new person(age, weight, height); } for(int i=0; i<num; i++) { p[i].show(); } } }

    • ベストアンサー
    • Java
  • java.util.Listについてです。

    以下の配列プログラムをava.util.Listを使うとどういう感じになるのでしょうか? 何卒ご回答よろしくお願い致します。 class CustomerManager { public static void main(String[] args){ CustomerManager manager = new CustomerManager(); manager.addCustomerCard( "山田一郎"); manager.addCustomerCard( "鈴木太郎"); manager.addCustomerCard( "田中次郎"); manager.printAllInfo(); } private CustomerCard[] customers = new CustomerCard[5]; private int index = 0; public void addCustomerCard(String name){ customers[index] = new CustomerCard(1 + index,name); index++; } public void printAllInfo() { System.out.print("ID =" + customers[0].id + ","); System.out.println("名前 = " + customers[0].name); System.out.print("ID =" + customers[1].id + ","); System.out.println("名前 = " + customers[1].name); System.out.print("ID =" + customers[2].id + ","); System.out.println("名前 = " + customers[2].name); } } class CustomerCard { String name; int id; public CustomerCard(int id,String name){ this.name = name; this.id = id; } public String getName(){ return this.name; } }

    • ベストアンサー
    • Java
  • java 一意のIDを振り分けるには。。。

    一意のIDの振り分け方がわかりません。。。 以下のプログラムを改良したいのですが。 CustomerManagerが内部でです。ただCustomerManagerインスタンス毎に一意で構いません。 addCustomerメソッドの整数型引数は除去します。 何卒ご回答よろしくお願い致します。 package exercise12; class CustomerCard { String name; int id; public CustomerCard(int id,String name){ this.name = name; this.id = id; } public String getName(){ return this.name; } } package exercise12; class CustomerManager { public static void main(String[] args){ CustomerManager manager = new CustomerManager(); manager.addCustomerCard(1, "山田一郎"); manager.addCustomerCard(2, "鈴木太郎"); manager.addCustomerCard(3, "田中次郎"); manager.printAllInfo(); } private CustomerCard[] customers = new CustomerCard[4]; private int index = 0; public void addCustomerCard(int id,String name){ customers[index] = new CustomerCard(id,name); index++; } public void printAllInfo() { System.out.print("ID =" + customers[0].id + ","); System.out.println("名前 = " + customers[0].name); System.out.print("ID =" + customers[1].id + ","); System.out.println("名前 = " + customers[1].name); System.out.print("ID =" + customers[2].id + ","); System.out.println("名前 = " + customers[2].name); } }

    • ベストアンサー
    • Java

専門家に質問してみよう