java练习题11
程序员文章站
2024-03-05 12:46:36
...
Java集合框架上机练习题
1 编写一个Book类,该类至少有name和price两个属性。该类要实现Comparable接口,在接口的compareTo()方法中规定两个Book类实例的大小关系为二者的price属性的大小关系。在主函数中,选择合适的集合类型存放Book类的若干个对象,然后创建一个新的Book类的对象,并检查该对象与集合中的哪些对象相等。查询结果如下图:
package test;
import java.util.List;
import java.util.*;
public class Book implements Comparable{
String name;
float price;
public Book(String name,float price) {
this.name=name;
this.price=price;
}
public static void main(String[] args) {
List<Book> list=new ArrayList<Book>();
Book b1=new Book("java基础教程", 29.0f);
Book b2=new Book("数据库技术", 25.0f);
Book b3=new Book("C++基础教程", 18.0f);
list.add(b1);
list.add(b2);
list.add(b3);
//创建一个新的Book类对象
Book b4=new Book("《模式识别》", 29.0f);
System.out.println("新书:"+b4.name+"与下列图书:");
for(int i=0;i<list.size();i++){
Book b=list.get(i);
//调用compareTo方法
int k=b.compareTo(b4);
if(k==1){
System.out.println(b.name);
}
}
System.out.println("价格相同,具体价格为:"+b4.price+"元");
}
public int compareTo(Object o) {
//向下转型
Book p=(Book)o;
if(p.price==this.price){
return 1;
}
else
return 0;
}
}
2 编写一个应用程序,用户分别从两个文本框输入学生的姓名和分数,程序按成绩排序将这些学生的姓名和分数显示在一个文本区中。
程序运行效果如图:
package test;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.*;
import java.util.Map.Entry;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class test extends JFrame {
JButton btn;
HashMap<String, Integer> map = new HashMap<>();
public test() {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
this.setContentPane(contentPane);
contentPane.setLayout(new GridLayout(5, 1, 5, 5));
JPanel pane1 = new JPanel();
contentPane.add(pane1);
JPanel pane2 = new JPanel();
contentPane.add(pane2);
JPanel pane3 = new JPanel();
contentPane.add(pane3);
JLabel label1 = new JLabel("姓名:");
final JTextField textField1 = new JTextField();
textField1.setColumns(10);
pane1.add(label1);
pane1.add(textField1);
JLabel label2 = new JLabel("分数:");
final JTextField textField2 = new JTextField();
textField2.setColumns(10);
pane2.add(label2);
pane2.add(textField2);
btn = new JButton("递交");
pane3.add(btn);
JScrollPane j = new JScrollPane();
final JTextArea t = new JTextArea(10, 10);
j.setViewportView(t);
contentPane.add(j);
setSize(400, 500);
setVisible(true);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = textField1.getText();
String g = textField2.getText();
map.put(name, Integer.valueOf(g));
sort(map, t);
}
});
}
public void sort(HashMap<String, Integer> map, JTextArea t) {
List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
// 然后通过比较器来实现排序
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
// 升序排序
});
String txt = "";
for (Map.Entry<String, Integer> mapping : list) {
String s = "姓名:"+mapping.getKey() + " " + "成绩:"+mapping.getValue() + "\n";
txt += s;
}
t.setText(txt);
}
public static void main(String[] args) {
new test();
}
}