实验内容:
(1)定义一个泛型类Instrument,其中包括一个泛型方法void play(E x)。定义两种乐器类:Cello、Violin可以进行演奏。定义一个测试类进行测试。
程序设计思路:
定义两种乐器类重写tostring方法。
程序代码:
package java_experiment_six;
class Violin {
public String toString()
{
return "violin.........";
}
}
class Cello {
public String toString()
{
return "cello.........";
}
}
class instrument<E> {//泛型类
String play(E x)
{
return x.toString();
}
}
public class Instrutest {
public static void main(String[] args) {
instrument<Cello>model = new instrument<Cello>();
instrument<Violin>model1 = new instrument<Violin>();
Cello cello = new Cello();
Violin violin = new Violin();
System.out.println(model.play(cello));
System.out.println(model1.play(violin));
}
}
运行截图:
实验内容:
(2)输入10个数字保存到List中,并按倒序显示出来。
程序设计思路:
判断是否为数字,如果是则用collections的sort实现排序,在用其reverse逆置,最后遍历输出。
程序代码:
package java_experiment_six;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Listtest {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Integer> integers = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 10; i++) {
try {
System.out.println("输入第" + i + "个数吧");
int input = Integer.parseInt(sc.next());
integers.add(input);
}
catch (Throwable e) {
System.err.println("这不是个数字,重新输入吧");
i--;
continue;
}
}
Collections.sort(integers);// 自然排
Collections.reverse(integers);// 倒排
for (Integer integer:integers) {
System.out.println(integer);
}
}
}
运行截图:
实验内容:
(3)编写一个程序,把学生名和考试分数录入到Map中,并按分数显示前三名学生的名字。要求定义Student类,封装学生名和考试分数2个属性及方法。
程序设计思路:
学生的名字为Map中的key, 分数为Map中的value,获取成绩先存入set,后存入list,用collections的sort实现排序,在用其reverse逆置,名字存入set,取前三成绩遍历名字,并输出。
程序代码:
package java_experiment_six;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
class Student{
String name;
int score;
public Student() {
}
public Student(String name,int score) {
this.name=name;
this.score=score;
}
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;
}
}
public class Maptest {
public static void main(String[] args) {
// // TODO Auto-generated method stub
Map<String,Integer> map = new HashMap<String,Integer>();
Scanner input=new Scanner(System.in);
System.out.println("请输入学生人数");
int number=input.nextInt();
System.out.println("请输入学生姓名和分数:");
for(int i=0;i<number;i++){
Student stu=new Student(input.next(),input.nextInt());
map.put(stu.getName(),stu.getScore());
}
//Collection<Integer> score=map.values();
Set<Integer> set=new HashSet<Integer>(map.values());
// for(Integer s:score)
// set.add(s);
List<Integer> list=new ArrayList(set);
Collections.sort(list);
Collections.reverse(list);
Set<String> name=map.keySet();
System.out.println("前三名学生的姓名是:");
int m=0;
for(Integer i: list){
m++;
if(m<=3){
for(String s: name)
{
if(i==map.get(s))
System.out.println("第"+m+"名"+s);
}
}
}
}
}
运行截图: