本文目錄一覽:
一道Java編程題,拜託了各位大神
1.定義一個Student類,包括學號,姓名,成績三個字段,生成get,set和toString方法,實現Comparable接口,重寫toCompare方法,方法里就是本題的邏輯,先按成績比較,再按學好比較,使用TreeSet不實現這個接口會報錯。
package Collection;
public class Student implements ComparableStudent {
private long sno;
private String name;
private int score;
public long getSno() {
return sno;
}
public void setSno(long sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
if (this.score o.score) {
return 1;
} else if (this.score o.score) {
return -1;
} else {
if(this.snoo.sno) {
return 1;
}else {
return -1;
}
}
}
@Override
public String toString() {
return “Student [sno=” + sno + “, name=” + name + “, score=” + score + “]”;
}
}
2.然後寫測試類,生成十個學生,然後插入treeset,直接遍歷輸出就是排序好的結果。
package Collection;
import java.util.Random;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
TreeSetStudent ts=new TreeSetStudent();
for(int i=0;i10;i++) {
Student stu=new Student();
stu.setName(“student”+i);
stu.setSno(170201+i);
stu.setScore(90+new Random().nextInt(10));
ts.add(stu);
}
for(Student stu:ts) {
System.out.println(stu);
}
}
}
最後貼一個運行結果
一道Java編程題
public class Person {
private static String name;
private static String sex;
private static int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public void print() {
System.out.print(“名字:” + name + ” 性別:” + sex + ” 年齡:” + age + ” “);
}
}
public class Student extends Person {
private static String school;
private static String department;
private static String studentno;
Student(String name, int age, String school, String department,
String studentno) {
super(name, age);
this.school = school;
this.department = department;
this.studentno = studentno;
}
public void print() {
super.print();
System.out.println(“學校是:” + school + ” 系是:” + department + ” 學號是:”
+ studentno);
}
}
java的一道編程題
因為你的java文件定義了package ch3;
應該是你的ch3文件夾裏面還有其它的文件定義了名為Circle的類。
你可以再新建一個包,把它放在那個包裏面(當然package ch3也要改成對應的包名,不然會報錯),就沒有這個問題了。
java大賽的一道編程題
public class ToListT {
private ListT list = new ArrayListT();
@SuppressWarnings({ “rawtypes”, “unchecked” })
public static ToList create(Object… vs) {
ToList toList = new ToList();
toList.add(vs);
return toList;
}
@SuppressWarnings(“unchecked”)
public ToListT add(T… vs) {
if (vs != null) {
for (T v : vs) {
list.add(v);
}
}
return this;
}
public ListT toList() {
return list;
}
public static void main(String[] args) {
ToListString toList1 = new ToListString();
// 往toList1里添加元素
toList1.add(“a”, “b”);
// 可以一次添加多個元素,而且add方法能以串式方式調用
toList1.add(“c”, “d”, “e”).add(“f”).add(“g”, “h”, “i”, “j”);
// 添加完後,將元素導出成一個List
java.util.ListString list1 = toList1.toList();
//
// 這段代碼檢測list1的內容
if (list1.size() != 10) {
throw new RuntimeException();
}
java.util.ListInteger list2 = ToList.create(0, 1, 2).add(3, 4)
.add(0, 1).toList();
//
// 以下代碼檢測list2的內容
int[] ia = { 0, 1, 2, 3, 4, 0, 1 };
for (int i = 0; i 7; i++) {
if (list2.get(i) != ia[i]) {
throw new RuntimeException();
}
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/248458.html