6-1 手机类
分数 15
全屏浏览题目
切换布局
作者 温彦
单位 山东科技大学
构造手机类,包含其配置信息:型号(字符串)、内存大小(整数)、存储空间(整数,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]
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
class CellPhone { private String no; private int store; private int space; private int price; CellPhone(String no,int store,int space,int price) { this.no = no; this.store = store; this.space = space; this.price = price; } public boolean equals(Object obj) { if(obj==null) return false; if(obj instanceof CellPhone) { CellPhone other = (CellPhone)obj; if(this.no.equals(other.no)&&this.store == other.store&&this.space == other.space) { return true; } else return false; } else return false; } public String toString() { return "CellPhone [model:"+no+", memory:"+store+", storage:"+space+", price:"+price+"]"; } }
7-1 jmu-Java-04面向对象进阶--02-接口-Comparator
分数 15
全屏浏览题目
切换布局
作者 郑如滨
单位 集美大学
Arrays.sort可以对所有实现Comparable的对象进行排序。但如果有多种排序需求,如有时候需对name进行降序排序,有时候只需要对年龄进行排序。使用Comparable无法满足这样的需求。可以编写不同的
Comparator
来满足多样的排序需求。1.编写PersonSortable2类
属性:
private name(String)
、private age(int)
有参构造函数:参数为name,age
toString方法:返回格式name-age
2 编写Comparator类
- 编写
NameComparator
类,实现对name进行升序排序- 编写
AgeComparator
类,对age进行升序排序3.main方法中
- 输入n
- 输入n行name age,并创建n个对象放入数组
- 对数组按照name进行升序排序后输出。
- 在3的基础上对数组按照age进行升序排序后输出。
- 最后最后两行使用如下代码输出NameComparator与AgeComparator所实现的所有接口。
System.out.println(Arrays.toString(NameComparator.class.getInterfaces())); System.out.println(Arrays.toString(AgeComparator.class.getInterfaces()));
输入样例:
5 zhang 15 zhang 12 wang 14 Wang 17 li 17
输出样例:
NameComparator:sort Wang-17 li-17 wang-14 zhang-15 zhang-12 AgeComparator:sort zhang-12 wang-14 zhang-15 Wang-17 li-17 //最后两行是标识信息
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
import java.util.Scanner; import java.util.Arrays; import java.util.Comparator; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); PersonSortable2[] person = new PersonSortable2[n]; for(int i=0;i<n;i++) { person[i] = new PersonSortable2(in.next(),in.nextInt()); } System.out.println("NameComparator:sort"); Arrays.sort(person,new NameComparator()); for(int i=0;i<person.length;i++) { System.out.println(person[i].toString()); } System.out.println("AgeComparator:sort"); Arrays.sort(person,new AgeComparator()); for(int i=0;i<person.length;i++) { System.out.println(person[i].toString()); } } } class PersonSortable2 { private String name; private int age; PersonSortable2(String name,int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public String toString() { return name+"-"+age; } } class NameComparator implements Comparator<PersonSortable2> { public int compare(PersonSortable2 a,PersonSortable2 other) { String name1 = a.getName(); String name2 = other.getName(); if(name1.compareTo(name2)>0) return 1; else if(name1.compareTo(name2)<0) return -1; else return 0; } } class AgeComparator implements Comparator<PersonSortable2> { public int compare(PersonSortable2 a,PersonSortable2 other) { if(a.getAge()>other.getAge()) return 1; else if(a.getAge()<other.getAge()) return -1; else return 0; } }
7-2 定义接口(Biology、Animal)、类(Person)、子类(Pupil)
分数 15
全屏浏览题目
作者 吴光生
单位 新余学院
(1)定义
Biology
(生物)、Animal
(动物)2个接口,其中Biology声明了抽象方法breathe( )
,Animal声明了抽象方法eat( )
和sleep( )
。
(2)定义一个类Person
(人)实现上述2个接口,实现了所有的抽象方法,同时自己还有一个方法think( )
。breathe()、eat()、sleep()、think()四个方法分别输出:我喜欢呼吸新鲜空气
我会按时吃饭
早睡早起身体好
我喜欢思考
(3)定义Person类的子类Pupil
(小学生),有私有的成员变量school
(学校),公有的成员方法setSchool( )
、getSchool( )
分别用于设置、获取学校信息。
(4)在测试类Main
中,用Pupil类创建一个对象zhangsan。尝试从键盘输入学校信息给zhangsan,获取到该信息后输出该学校信息,格式为“我的学校是XXX”;依次调用zhangsan的breathe()、eat()、sleep()、think()方法。输入格式:
从键盘输入一个学校名称(字符串格式)
输出格式:
第一行输出:我的学校是XXX(XXX为输入的学校名称)
第二行是breathe()方法的输出
第三行是eat()方法的输出
第四行是sleep()方法的输出
第五行是think()方法的输出输入样例:
在这里给出一组输入。例如:
新余市逸夫小学
输出样例:
在这里给出相应的输出。例如:
我的学校是新余市逸夫小学 我喜欢呼吸新鲜空气 我会按时吃饭 早睡早起身体好 我喜欢思考
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
import java.util.Scanner; import java.util.Arrays; import java.util.Comparator; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); Pupil zhangsan = new Pupil(s); System.out.println("我的学校是"+zhangsan.getSchool()); zhangsan.breathe(); zhangsan.eat(); zhangsan.sleep(); zhangsan.think(); } } interface Biology { void breathe( ); } interface Animal { void eat(); void sleep(); } class Person implements Biology,Animal { public void breathe() { System.out.println("我喜欢呼吸新鲜空气"); } public void eat() { System.out.println("我会按时吃饭"); } public void sleep() { System.out.println("早睡早起身体好"); } public void think() { System.out.println("我喜欢思考"); } } class Pupil extends Person { private String school; Pupil(String s) { school = s; } public void setSchool(String s) { school = s; } public String getSchool() { return school; } }
7-3 矩阵转置运算
分数 15
全屏浏览题目
作者 Ma
单位 山东科技大学
在数学中,矩阵(Matrix)是一个按照长方阵列排列的复数或实数集合,最早来自于方程组的系数及常数所构成的方阵。
矩阵的转置是矩阵的一种运算,设A为m×n阶矩阵(即m行n列),第i 行j 列的元素是a(i,j),把m×n矩阵A的行换成同序数的列得到一个n×m矩阵,此矩阵叫做A的转置矩阵。
输入格式:
N;//矩阵的行数
....
....//矩阵中的元素输入样例:
4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
输出样例:
1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
import java.util.Scanner; import java.util.Arrays; import java.util.Comparator; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[][] matrix = new int[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { matrix[i][j] = in.nextInt(); } } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(j==0) System.out.print(matrix[j][i]); System.out.print(" "+matrix[j][i]); } System.out.print("\n"); } in.close(); } }
7-4 有重复的数据
分数 20
全屏浏览题目
作者 翁恺
单位 浙江大学
在一大堆数据中找出重复的是一件经常要做的事情。现在,我们要处理许多整数,在这些整数中,可能存在重复的数据。
你要写一个程序来做这件事情,读入数据,检查是否有重复的数据。如果有,输出“
YES
”这三个字母;如果没有,则输出“NO
”。输入格式:
你的程序首先会读到一个正整数n,n∈[1,100000],然后是n个整数。
输出格式:
如果这些整数中存在重复的,就输出:
YES
否则,就输出:
NO
输入样例:
5 1 2 3 1 4
输出样例:
YES
代码长度限制
16 KB
时间限制
800 ms
内存限制
64 MB
import java.util.Scanner; import java.util.Arrays; import java.util.Comparator; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++) { a[i] = in.nextInt(); } Arrays.sort(a); int flag = 0; for(int i=0;i<n-1;i++) { if(a[i]==a[i+1]) { flag = 1; break; } } if(flag==1) System.out.println("YES"); else System.out.println("NO"); in.close(); } }