《Java程序设计》实验5

6-1 sdut-oop-list-1 学生集合(类、集合)

分数 10

全屏浏览题目

切换布局

作者 周雪芹

单位 山东理工大学

以下程序不完整,请你根据已经给出的程序代码中表达的题意,以及程序的输入、输出信息,完成Student类的设计,补全代码。

函数接口定义:

class Student{

}

裁判测试程序样例:

import java.util.ArrayList;

public class Main{
private ArrayList slist = new ArrayList();

    public void addStudent(Student s){
        this.slist.add(s);
    }

    public void showStudent(){
        for(Student s:slist){
            System.out.println(s);
        }
    }

    public static void main(String[] args) {
        Main t = new Main();
        t.addStudent(new Student("2016001","rose",18));
        t.addStudent(new Student("2016002","hunifu",19));
        t.addStudent(new Student("2016003","britsh",20));
        t.addStudent(new Student("2016004","sunni",17));
        t.showStudent();
    }
}

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

输入样例:

无输入。

 
 

输出样例:

2016001 rose 18
2016002 hunifu 19
2016003 britsh 20
2016004 sunni 17

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

class Student
{
	private String no;
	private String name;
	private int age;
	Student(String no,String name,int age)
	{
		this.no = no;
		this.name = name;
		this.age = age;
	}
	public String toString()
	{
		return no+" "+name+" "+age;
    }
    
}

6-2 图书列表

分数 25

全屏浏览题目

作者 温彦

单位 山东科技大学

构建一个书类Book,包括名称(字符串),价格(整型),作者(字符串,多个作者当做一个字符串处理),版本号(整型),提供带参数的构造函数Book(String name, int price, String author, int edition),提供该类的toString()和equals()方法,toString方法返回所有成员属性的值的字符串形式,形如“name: xxx, price: xxx, author: xxx, edition: xxx”,当两个Book对象的名称(不关心大小写,无空格)、作者(不关心大小写,无空格)、版本号相同时,认为两者表示同一本书。 Main函数中,读入两本书,输出他们是否相等,打印两本书的信息。

构建一个书单类BookList,该类中用一个列表类对象存放书单,提供添加图书(addBook)、查找图书(searchBook)的函数
main函数从键盘输入多个Book添加到书单中,(添加时,提供书的名称、价格、作者、版本号),而后从键盘读入一本书,查找该列表对象中是否包含该书,若包含,输出”found: 该书在列表中的序号”,若不包含,输出“not found”,查找时,提供书的名称、作者、版本号。

输入描述:

添加书的个数
添加的书
查找的书

输出描述:

查找结果

裁判测试程序样例:

import java.util.Scanner;

/* 你的答案被嵌在这里 */

public class Main{
    
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        BookList bl = new BookList();
        int n = s.nextInt();
        for (int i=0; i<n;i++) {
            bl.addBook(new Book(s.next(),
                    s.nextInt(),
                    s.next(),
                    s.nextInt()));
        }
        bl.searchBook(new Book(s.next(),
                    0,
                    s.next(),s.nextInt()));
    }
}

输入样例:

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

2
ThinkingInJava
86
BruceEckel
4
CoreJava
95
CayS.Horstmann
10
CoreJava
CayS.Horstmann
10

输出样例:

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

found: 1 

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

import java.util.ArrayList;

class Book
{
	private String name;
	private int price;
	private String author;
	private int edition;
	Book(String name, int price, String author, int edition)
	{
		this.name = name;
		this.price = price;
		this.author = author;
		this.edition = edition;
	}
	public String toString()
	{
		return "name: "+name+", price: "+price+", author: "+author+", edition: "+edition;
	}
	public boolean equals(Object obj)
	{
		if(obj==null)
			return false;
		if(obj instanceof Book)
		{
			Book other = (Book)obj;
			this.name = this.name.replaceAll("\\s", "");
			other.name = other.name.replaceAll("\\s","");
			this.author = this.author.replaceAll("\\s", "");
			other.author = other.author.replaceAll("\\s", "");
			if(name.equalsIgnoreCase(other.name)&&this.author.equalsIgnoreCase(other.author)&&this.edition == other.edition)
			{
				return true;
			}
			else
				return false;
		}
		else
			return false;
	}
}

class BookList
{
	private ArrayList<Book> list = new ArrayList<>();
	public void addBook(Book a)
	{
		list.add(a);
	}
	public void searchBook(Book a)
	{
		int flag = 0;
		for(int i=0;i<list.size();i++)
		{
			if(list.get(i).equals(a))
			{
				System.out.println("found: "+i);
				flag = 1;
				break;
			}
		}
		if(flag == 0)
			System.out.println("no found");
	}
}

6-3 根据要求,使用泛型和LinkedList编写StringList类,实现QQ号码查找的功能。

分数 30

全屏浏览题目

作者 tr

单位 成都信息工程大学

已知数组存放一批QQ号码,QQ号码最长为11位,最短为5位:
String[] strs = {"12345","67891","12347809933","98765432102","67891","12347809933"}。
将该数组里面的所有QQ号都存放在LinkedList中,然后遍历链表,将list中第一个指定长度的QQ号查找出来;如果不存在指定长度的QQ号,则输出“not exist”。

Main类:在main方法中,调用constructList方法将strs中的字符串存入一个String的链表中,然后调用search方法查找第一个指定长度的QQ号码,并打印到屏幕。

编写StringList类,编程要求如下:

  1. 根据程序需求,定义成员变量、编写构造方法。
  2. LinkedList constructList(String[] strs) 方法:将String数组strs中的元素添加到链表中,构建一个String对象链表,最后返回链表。
  3. String search(LinkedList list)方法:使用scanner的nextInt()方法从键盘读入一个int,表示指定长度,然后遍历链表,查找出链表中第一个指定长度的QQ号码并返回;如果不存在指定长度的QQ号,则返回字符串"not exist"。

函数接口定义:

 
 

LinkedList<String> constructList(String[] strs); String search(LinkedList<String> list);

裁判测试程序样例:

 
 

import java.util.*; public class Main { public static void main(String[] args) { String[] strs = {"12345","67891","12347809931","98765432102","67891","12347809933"}; StringList sl=new StringList(); LinkedList<String> qqList=sl.constructList(strs); System.out.println(sl.search(qqList)); } } /* 请在这里填写答案:StringList类 */

输入样例:

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

5

输出样例:

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

12345

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

class StringList
{
	public LinkedList constructList(String[] strs)
	{
		LinkedList<String> list = new LinkedList<>();
		for(int i=0;i<strs.length;i++)
		{
			list.add(strs[i]);
		}
		return list;
	}
	public String search(LinkedList list)
	{
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		for(int i=0;i<list.size();i++)
		{
			if(list.get(i) instanceof String)
			{
				String s = (String)list.get(i);
				if(s.length()==n)
				{
					return s;
				}
			}
		}
			return "no exist";
	}
}

7-1 sdust-Java-字符串集合求并集

分数 15

全屏浏览题目

切换布局

作者 张峰

单位 山东科技大学

从键盘接收N个英文字符串(其中不同的字符串数量大于10),从头开始取5个不同的字符串放入一个集合S1,然后接着取5个不同的字符串放入另一个集合S2,按照字母顺序输出S1和S2的并集中的每个字符串(字符串区分大小写)

输入格式:

一行以空格分开的英文字符串(不同的字符串数量大于10)。

输出格式:

按照字母顺序(先比较字符串首字母,首字母相同的比较字符串第二个字母,以此类推)输出的S1和S2并集的字符串。

输入样例:

android python java javaee javase database java jsp servlet java algorithm junit

输出样例:

algorithm
android
database
java
javaee
javase
jsp
python
servlet

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

import java.util.Scanner;
import java.util.Set;
import java.util.Arrays;
import java.util.TreeSet;

public class Main {    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        String[] str = s.split(" ");
        Set<String> set1 = new TreeSet<>();
        Set<String> set2 = new TreeSet<>();
        int flag=0;
        for(int i=0;i<str.length;i++)
        {
        	if(set1.size()<6) 
        	{
        	    set1.add(str[i]);
        	    flag = i;
        	}
        }
        for(int i=flag+1;i<str.length;i++)
        {
        	if(set2.size()<6)
        	{
        	    set2.add(str[i]);
        	}
        }
        TreeSet<String> set = new TreeSet<>();
        set.addAll(set1);
        set.addAll(set2);
        for(String strs:set)
        {
        	System.out.println(strs);
        }
    }
}

7-2 jmu-Java&Python-统计文字中的单词数量并按出现次数排序

分数 20

全屏浏览题目

作者 郑如滨

单位 集美大学

现在需要统计若干段文字(英文)中的单词数量,并且还需统计每个单词出现的次数

注1:单词之间以空格(1个或多个空格)为间隔。
注2:忽略空行或者空格行。

基本版:
统计时,区分字母大小写,且不删除指定标点符号。

进阶版:

  1. 统计前,需要从文字中删除指定标点符号!.,:*?。 注意:所谓的删除,就是用1个空格替换掉相应字符。
  2. 统计单词时需要忽略单词的大小写。

输入说明

若干行英文,最后以!!!!!为结束。

输出说明

单词数量
出现次数排名前10的单词(次数按照降序排序,如果次数相同,则按照键值的字母升序排序)及出现次数。

输入样例1

failure is probably the fortification in your pole

it is like a peek your wallet as the thief when you
are thinking how to spend several hard-won lepta
          
when you are wondering whether new money it has laid
background because of you then at the heart of the
     
most lax alert and most low awareness and left it

godsend failed
!!!!!

输出样例1

46
the=4
it=3
you=3
and=2
are=2
is=2
most=2
of=2
when=2
your=2

输入样例2

Failure is probably The fortification in your pole!

It is like a peek your wallet as the thief when You
are thinking how to. spend several hard-won lepta.

when yoU are? wondering whether new money it has laid
background Because of: yOu?, then at the heart of the
Tom say: Who is the best? No one dare to say yes.
most lax alert and! most low awareness and* left it

godsend failed
!!!!!

输出样例2

54
the=5
is=3
it=3
you=3
and=2
are=2
most=2
of=2
say=2
to=2

代码长度限制

16 KB

时间限制

1600 ms

内存限制

180 MB

  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值