book类练习题

1、编写一个Book类,该类至少有name和price两个属性。该类要实现Comparable接口,在接口的compareTo()方法中规定两个Book类实例的大小关系为二者的price属性的大小关系。在主函数中,选择合适的集合类型存放Book类的若干个对象,然后创建一个新的Book类的对象,并检查该对象与集合中的哪些对象相等。

代码如下:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Book implements Comparable{
    public String name;
    public double price;

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List booklist = new ArrayList();
        Book b1 = new Book("java基础教程", 30.0);
        Book b2 = new Book("C++基础教程", 28.0);
        Book b3 = new Book("数据库技术", 29.0);
        Book b4 = new Book("计算机网络", 29.0);
        Book b5 = new Book("高等数学", 29.0);
        booklist.add(b1);
        booklist.add(b2);
        booklist.add(b3);
        booklist.add(b4);
        booklist.add(b5);
        Book b6 = new Book("模式识别", 29.0);

        System.out.println("新书《" + b6.name + "》与下列图书:");
        for (int i = 0; i < booklist.size(); i++) {
            Book book = (Book) booklist.get(i);
            int index = book.compareTo(b4);
            if (index == 0) {
                System.out.print("《"+book.name);
                System.out.println("》");
            }
        }
        System.out.print("相同" + "具体价格为" + b4.price + "元");
    }

    public int compareTo(Object obj) {
        Book p = (Book) obj;
        return (int) (p.price-this.price);
    }

}

运行结果:
book类练习题_第1张图片
2、编写一个应用程序,用户分别从两个文本框输入学术的姓名和分数,程序按成绩排序将这些学生的姓名和分数显示在一个文本区中。
代码如下:
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ScoreOrder extends JFrame implements ActionListener {
    JFrame jframe;
    JTextField jtextfield1;
    JTextField jtextfield2;
    JTextArea jtextarea;
    List list = new ArrayList();

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ScoreOrder myJframe = new ScoreOrder();// 显示窗口
    }

    ScoreOrder() {
        jframe = new JFrame("成绩排序");
        jframe.setBounds(200, 100, 700, 400);
        jframe.setLayout(new FlowLayout());
        jtextfield1 = new JTextField(10);
        jtextfield2 = new JTextField(10);
        JButton jbutton = new JButton("插入");
        Box box = Box.createHorizontalBox();
        box.add(new Label("姓名:"));
        box.add(jtextfield1);
        box.add(new Label("分数:"));
        box.add(jtextfield2);
        box.add(Box.createHorizontalStrut(50));
        box.add(jbutton);
        jframe.add(box);
        jtextarea = new JTextArea(40, 80);
        jframe.add(jtextarea);
        jframe.setVisible(true);
        jbutton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        jtextarea.setText("");
        String name = jtextfield1.getText();
        String grade = jtextfield2.getText();
        if (!"".equals(name) && !"".equals(grade)) {
            list.add(new Student(name, Float.parseFloat(grade)));
            jtextarea.append("当前插入的信息为:姓名:" + name + "    成绩:" + grade + "\n");
        }
        Collections.sort(list);// 对list排序
        Iterator iterator = list.iterator();
        Student student;
        while (iterator.hasNext()) {
            student = iterator.next();
            jtextarea.append("name:" + student.name + "    grade:" + student.grade + "\n");
        }
    }
}

class Student implements Comparable {

    String name;
    float grade;

    public Student(String name, float grade) {
        super();
        this.name = name;
        this.grade = grade;
    }

    @Override
    public int compareTo(Student o) {
        // TODO Auto-generated method stub
        return (int) (o.grade - this.grade);
    }

}

运行结果:
book类练习题_第2张图片


你可能感兴趣的:(book类练习题)