Java 实验五

5-1手机类

构造手机类,包含其配置信息:型号(字符串)、内存大小(整数)、存储空间(整数,GB为单位)、价格(整数)。提供带参数的构造函数,重写其equals方法,使得两个相同配置(型号、内存、存储相同即可,价格可不同)的手机为相等的手机。重写其toString函数,打印手机的配置信息,形式为CellPhone [model:xxx, memory:xxx, storage:xxx, price:xxx]
main函数中从键盘读入两个手机对象,比较他们是否相等,输出他们的配置信息。

输入描述:

两个计算机对象,包含型号、内存、存储空间、价格

输出描述:

两个对象是否相等,两个对象的配置信息

裁判测试程序样例:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        CellPhone c1 = new CellPhone(sc.next(),sc.nextInt(),sc.nextInt(),sc.nextInt());
        CellPhone c2 = new CellPhone(sc.next(),sc.nextInt(),sc.nextInt(),sc.nextInt());

        System.out.println(c1.equals(c2));
        System.out.println(c1);
        System.out.println(c2);
    }
}

/* 你的代码将被嵌在这里 */

输入样例:

在这里给出一组输入。例如:

P20 8 64 4999
P20 8 64 4999

输出样例:

在这里给出相应的输出。例如:

true
CellPhone [model:P20, memory:8, storage:64, price:4999]
CellPhone [model:P20, memory:8, storage:64, price:4999]

答案:

class CellPhone {
	private String model;//型号
	private int memory;//内存大小
	private int storage;//存储空间
	private int price;
	public CellPhone(String model, int memory, int storage, int price) {
		this.model = model;
		this.memory = memory;
		this.storage = storage;
		this.price = price;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CellPhone other = (CellPhone) obj;
		if (memory != other.memory)
			return false;
		if (model == null) {
			if (other.model != null)
				return false;
		} else if (!model.equals(other.model))
			return false;
		if (storage != other.storage)
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "CellPhone [model:" + model + ", memory:" + memory + ", storage:" + storage + ", price:" + price + "]";
	}
}

5-2sdust-Java-可实现多种排序的Book类

设计Book类,要求:1)Book类的成员属性包括:书名name(String类型)、出版日期publishDate(Date类型)、定价price(double型);2)为Book对象提供按出版日期、价格排序的两种方式,且能够满足测试程序代码中的输入输出要求(注意代码注释中的说明)。其中,类BookComparatorByPrice的排序功能是:将一组Book对象按照价格升序排序,如果价格相同,则按照书名字母顺序排列;类BookComparatorByPubDate的排序功能是:将一组Book对象按照出版日期降序排序。

裁判测试程序样例:

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Book[] books = new Book[4];
        //1. 从键盘接收用户输入的4本书的名称(仅有英文字符构成)、出版日期(格式:1998-10-09)、价格,生成Book对象,构造包含4本书的数组
        for(int i=0;i<4;i++){
            String name = scan.next();
            String date_str = scan.next();
            Date date = null;
            //将键盘录入的日期字符串转换为Date
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                date = sdf.parse(date_str);
            } catch (ParseException e) {
                System.out.println("日期格式有误");;
            }
            
            double price = Double.parseDouble(scan.next());
            
            Book book = new Book(name, date, price);
            books[i] = book;
        }
        
        //2.将books按照出版日期降序排序;然后输出books
        Arrays.sort(books, new BookComparatorByPubDate());
        for(Book book:books){
            System.out.println(book);
        }
        
        //3.将books按照价格升序排序,如果价格相同,则按照书名字母顺序排列。然后输出books
        Arrays.sort(books, new BookComparatorByPrice());
        for(Book book:books){
            System.out.println(book);
        }
        
        scan.close();
    }

}

/* 请在这里填写答案 */

输入样例:

Java
2011-08-01
29
Python
2014-01-01
48
C
2004-09-08
17.5
DataBase
2012-09-17
17.5

输出样例:

书名:Python,定价:48.0
书名:DataBase,定价:17.5
书名:Java,定价:29.0
书名:C,定价:17.5
书名:C,定价:17.5
书名:DataBase,定价:17.5
书名:Java,定价:29.0
书名:Python,定价:48.0

答案:

class Book{
	private String name;
	private Date publishDate;
	private double price;
	public Book(String name, Date publishDate, double price) {
		this.name = name;
		this.publishDate = publishDate;
		this.price = price;
	}
	public String getName() {
		return name;
	}
	public Date getPublishDate() {
		return publishDate;
	}
	public double getPrice() {
		return price;
	}
	@Override
	public String toString() {
		return "书名:" + name + ",定价:" + price;
	}
	
}
 
class BookComparatorByPubDate implements Comparator{
	@Override
	public int compare(Object o1, Object o2) {
		Book b1 = (Book) o1;
		Book b2 = (Book) o2;
		return b2.getPublishDate().compareTo(b1.getPublishDate());
	}
	
}
 
class BookComparatorByPrice implements Comparator{
 
	@Override
	public int compare(Object o1, Object o2) {
		Book b1 = (Book) o1;
		Book b2 = (Book) o2;
		if(b1.getPrice() == b2.getPrice()) {
			return b1.getName().compareTo(b2.getName());
		}
		else {
			return b1.getPrice() < b2.getPrice() ? -1 : 1;
		}
	}
	
}

5-3学生、大学生、研究生类-2

修改题目125(学生类-本科生类-研究生类)
为学生类添加属性成绩,添加相应的get和set函数,添加函数getGrade()表示获得等级,该函数应当为抽象函数。
本科生和研究生的等级计算方式不同,如下所示

本科生标准 研究生标准
[80--100) A [90--100) A
[70--80) B [80--90) B
[60--70) C [70--80) C
[50--60) D [60--70) D
50 以下 E 60 以下 E

main函数中构造两个学生Student变量,分别指向本科生和研究生对象,调用getGrade()方法输出等级

输入格式:

本科生类信息,学号、姓名、性别、专业、成绩
研究生类信息,学号、姓名、性别、专业、导师、成绩

输出格式:

本科生等级
研究生等级

输入样例:

在这里给出一组输入。例如:

2 chen female cs 90
3 li male sc wang 80

输出样例:

在这里给出相应的输出。例如:

A
B

答案:

import java.util.*;
abstract class Student{
    int number;
    String name;
    String sex;
    int score;
    Student(int n,String nam,String s,int sco){
        number=n;
        name=nam;
        sex=s;
        score=sco;
    }
    abstract public String getGrade();
}

class CollegeStudent extends Student{
    String major;
    CollegeStudent(int number,String name,String sex,String major,int score){
        super(number,name,sex,score);
        this.major=major;
    }
    public String getGrade(){
        if(score>=80 && score <100)
            return "A";
        else if(score>=70 && score <80)
            return "B";
        else if(score>=60 && score <70)
            return "C";
        else if(score>=50 && score <60)
            return "D";
        else
            return "E";
    }
}

class GraduateStudent extends CollegeStudent{
    String teacher;
    GraduateStudent(int n,String name,String sex,String major,String teacher,int score){
        super(n,name,sex,major,score);
        this.teacher=teacher;
    }
    public String getGrade(){
        if(score>=90 && score <100)
            return "A";
        else if(score>=80 && score <90)
            return "B";
        else if(score>=70 && score <80)
            return "C";
        else if(score>=60 && score <70)
            return "D";
        else
            return "E";
    }
}
class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        Student s1=new CollegeStudent(sc.nextInt(),sc.next(),sc.next(),sc.next(),sc.nextInt());
        Student s2=new GraduateStudent(sc.nextInt(),sc.next(),sc.next(),sc.next(),sc.next(),sc.nextInt());
        System.out.println(s1.getGrade());
        System.out.println(s2.getGrade());
    }
}

5-4USB接口的定义

定义一个USB接口,并通过Mouse和U盘类实现它,具体要求是:

1.接口名字为USB,里面包括两个抽象方法:

void work();描述可以工作

void stop(); 描述停止工作

2.完成类Mouse,实现接口USB,实现两个方法:

work方法输出“我点点点”;

stop方法输出 “我不能点了”;

3.完成类UPan,实现接口USB,实现两个方法:

work方法输出“我存存存”;

stop方法输出 “我走了”;

4测试类Main中,main方法中

定义接口变量usb1 ,存放鼠标对象,然后调用work和stop方法

定义接口数组usbs,包含两个元素,第0个元素存放一个Upan对象,第1个元素存放Mouse对象,循环数组,对每一个元素都调用work和stop方法。

输入格式:

输出格式:

输出方法调用的结果

输入样例:

在这里给出一组输入。例如:

 

输出样例:

在这里给出相应的输出。例如:

我点点点
我不能点了
我存存存
我走了
我点点点
我不能点了

答案:

interface USB{
    void work();
    void stop();
}
class Mouse implements USB{
   public void work(){
        System.out.println("我点点点");
    }
    public void stop(){
        System.out.println("我不能点了");
    }
}
class UPan implements USB{
    public void work(){
        System.out.println("我存存存");
    }
    public void stop(){
        System.out.println("我走了");
    }
}
class Main{
    public static void main(String[] args){
        USB usb1=new Mouse();
        usb1.work();
        usb1.stop();
        USB[] usbs=new USB[2];
        usbs[0]=new UPan();
        usbs[1]=new Mouse();
        for(int i=0;i<2;i++){
            usbs[i].work();
            usbs[i].stop();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值