Computer 2019-1 / 2 / 3 / 4 - Experiment 6

1. Performance management system

Construct a performance management system CourseManagement system, which includes the following methods:

  • void add(int no, int grade) adds the grade of the student number. If the system already has the student grade, it outputs "the student already exists";
  • void delete(int no) deletes the grade of a student number. If the student does not exist, "no such student" will be output;
  • int query (int no) queries and returns the grade of the student number;
  • Count the number of students in [0-59], [60-69], [70-79], [80-89] and [90-100] grades and print them.

Please select the appropriate container to realize the above functions. (assuming that the student grades of the same student number will not be added repeatedly) read the operation type and related parameters in the main function, and call the static function to output the statistical information of student grades.

Enter Description:

Number of operations operation name operation parameters

Output Description:

Query the number of students in each grade segment

Example of referee test procedure:

import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        CourseManagementSystem cms = new CourseManagementSystem();
        int ops = sc.nextInt();
        for (int i=0;i<ops;i++) {
            String op = sc.next();
            if (op.equals("add")) 
                cms.add(sc.nextInt(), sc.nextInt());
            else if  (op.equals("delete"))
                cms.delete(sc.nextInt());
            else if  (op.equals("query")) {
                int no = sc.nextInt();
                int s = cms.query(no);
                System.out.println("the score for "+no+" is : "+s);
            }
        }
        cms.statistic();
    }
}

/* Your code is embedded here*/

Input example:

A set of inputs is given here. For example:

8
add 1 63
add 2 78
add 3 74
delete 3
add 2 20
delete 5
query 1
add 4 90

Output example:

The corresponding output is given here. For example:

the student already exists
no such student
the score for 1 is : 63
[0-59] : 0
[60-69] : 1
[70-79] : 1
[80-89] : 0
[90-100] : 1

Own code:

class CourseManagementSystem{
	int[][] arr=new int[100][2];
	int q=0;
	int u=0;
	int e=0;
	int r=0;
	int y=0;
	CourseManagementSystem(){}
	void add(int n,int v) {
		if(arr[n][0]!=0)
			System.out.println("the student already exists");
		else
		{
			arr[n][0]=v;
			
			if(v>=0 && v<=59)q++;
			else if(v>=60 && v<=69)u++;
			else if(v>=70 && v<=79)e++;
			else if(v>=80 && v<=89)r++;
			else if(v>=90 && v<=100)y++;
			
		}
	}
	void delete(int n) {
		if(arr[n][0]==0)System.out.println("no such student");
		else 
		{
			
			int v=arr[n][0];
			
			if(v>=0 && v<=59)q--;
			else if(v>=60 && v<=69)u--;
			else if(v>=70 && v<=79)e--;
			else if(v>=80 && v<=89)r--;
			else if(v>=90 && v<=100)y--;
			
			arr[n][0]=0;
		}
	}
	public int query(int no) {
		
		return arr[no][0];
	}
	public void statistic() {
		System.out.println("[0-59] : "+q+"\n"
				+"[60-69] : "+u+"\n"
				+"[70-79] : "+e+"\n"
				+"[80-89] : "+r+"\n"
				+"[90-100] : "+y
				);
		
	}
	
	
}






2. Student list 2

Write a student class, including student number no, name, grade score, and provide the necessary constructor, toString function and equals/hashcode function. The format of toString function is "no:xxx name:xxx score:xxx", and no participates in the calculation of equals and hashcode

Construct a container in the main function to store student objects, input multiple student objects from the command line, store them in the container, and read the operations on the container from the command line. The specific operations include:
Add add a student (including student number and student name)
Delete delete a student (including student number)
set modify a student's information (only modify the scores of students of a student number)

After completing the operation, output the students in the container in the order of student number from small to large

Enter Description:

Student number student object data operand operation content

Output Description:

List the students in the output set in order

Input example:

A set of inputs is given here. For example:

4
1 wong 90
2 liu 80
3 chen 70
4 fang 60
3
add 5 duan 80
delete 3
set 4 70

Output example:

The corresponding output is given here. For example:

no:1 name:wong score:90
no:2 name:liu score:80
no:4 name:fang score:70
no:5 name:duan score:80

Own code:

import java.util.*;
class Student implements Comparable<Student>{
	int no;
	String name;
	int score;
	Student(int no,String name,int score){
		this.no=no;
		this.name=name;
		this.score=score;
	}
	public Student(int t) {
		this.no=t;
	}
	void setScore(int n) {score=n;}
	
	public String toString() {
		return "no:"+no+" name:"+name+" score:"+score;
	}
	@Override
	public int compareTo(Student o) {
		if(no>o.no)return 1;
		else if(no==o.no)return 0;
		else
			return -1;
	}
	@Override
	public boolean equals(Object o)
	{
		if(o==null)
		return false;
		else
		{
			Student stu=(Student)o;
			if(stu.no==this.no)return true;
		}
		return false;
	}
	@Override 
	public int hashCode() {
		int aa=19;
		return 31*aa+no;
	}
}
public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        TreeSet<Student> test=new TreeSet<>();
        int addnum=in.nextInt();
        for(int i=0;i<addnum;i++) {
        	int no=in.nextInt();
        	String name=in.next();
        	int score=in.nextInt();
        	test.add(new Student(no,name,score));
        }
        

        int ops = in.nextInt();
        for (int i=0;i<ops;i++)
        {
            String op = in.next();
            if (op.equals("add")) 
            {
            	int no=in.nextInt();
            	String name=in.next();
            	int score=in.nextInt();
            	test.add(new Student(no,name,score));
            } 
            else if  (op.equals("delete"))
            {
            	int t=in.nextInt();
            	 test.remove(new Student(t));
            }
            else if  (op.equals("set"))
            {
                int no = in.nextInt();
                int score=in.nextInt();
               Iterator it=test.iterator();
               while(it.hasNext())
               {
            	  Student stu=(Student)it.next();
            	   if(stu.no==no)
            		  stu.setScore(score);
               }
            }
        }
        
        
        for(Student s:test)
        {
        	System.out.println(s);
        }
     
    }
}






3. Page printing of office documents

In office software (word, excel), sometimes only a part of the whole document needs to be printed, so the user needs to select the page range to be printed. At present, the format of the entered page range is defined as: separated by commas, and - can be used to represent continuous page numbers.
For example: 1,3,5-9,20. It means that the page number to be printed is 1, 3, 5, 6, 7, 8, 9, 20.

This topic requires reading a line of string as the page number range to be printed. Note the following:

  • 1. Page number range can be entered out of order. For example: 5,3,7,9-10,1-2;
  • 2. Consecutive page definitions may not be entered in descending order. For example: 1,9,5,20-15,10;
  • 3. The page number range entered may be duplicate. For example: 1,9,15,5-10,12-20

Enter Description:

First line: format string representing page number range

Output Description:

Output the page numbers to be printed in the order from small to large, and divide them with spaces

Input example 1:

A set of inputs is given here. For example:

1,3,5-9,20

Output example 1:

The corresponding output is given here. For example:

1 3 5 6 7 8 9 20

Input example 2:

A set of inputs is given here. For example:

12-20,1,15,9,5-10

Output example 2:

The corresponding output is given here. For example:

1 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20

Own code:

import java.util.*;
class is_int{
	boolean judge(String s)
	{
		for(int i=0;i<s.length();i++)
		{
			if(!(s.charAt(i)>='0'&& s.charAt(i)<='9'))return false;
		}
		return true;
	}
}
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        is_int is=new is_int();
        
        String str_all=in.nextLine();
        String[] str=str_all.split(",");
        HashSet<Integer> set=new HashSet<>();
        
        for(int i=0;i<str.length;i++)
        {
        	if(is.judge(str[i]))//yes
        		set.add(Integer.parseInt(str[i]));
        	else//Not an integer
        	{
        		String[] another=str[i].split("-");
        		int num1=Integer.parseInt(another[0]);
        		int num2=Integer.parseInt(another[1]);
        		if(num1>num2) {
        			int t=num1;
        			num1=num2;
        			num2=t;
        		}
        		for(int ii=num1;ii<=num2;ii++)
        			set.add(ii);
        	}
        }
        
        int[] arr=new int[set.size()];
        int len=0;
        for(Integer temp:set)
        	arr[len++]=temp.intValue();
        
        Arrays.sort(arr);
        
        for(int i=0;i<arr.length;i++)
        {
        	if(i==0)System.out.print(arr[i]);
        	else
        		System.out.print(" "+arr[i]);
        }
        
        
    }
}






4. SDUST Java string set union set

Receive N English strings from the keyboard (the number of different strings is greater than 10), take 5 different strings from the beginning and put them into one set S1, then take 5 different strings and put them into another set S2, and output each string in the combination of S1 and S2 in alphabetical order (strings are case sensitive)

Enter Description:

A line of English strings separated by spaces (the number of different strings is greater than 10).

Output Description:

The string of the union of S1 and S2 output in alphabetical order (compare the first letter of the string first, compare the second letter of the string with the same first letter, and so on).

Input example:

A set of inputs is given here. For example:

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

Output example:

The corresponding output is given here. For example:

algorithm
android
database
java
javaee
javase
jsp
python
servlet

Own code:

import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        String s=in.nextLine();
        String[] str=s.split(" ");
        
        TreeSet<String> set1=new TreeSet<>();
        TreeSet<String> set2=new TreeSet<>();
        
       for(String s1:str)
       {
    	   if(set1.size()<5)
    	   {
    		   set1.add(s1);
    		   continue;
    	   }
    	   if(set2.size()<5)
    	   {
    		   set2.add(s1);
    		   continue;
    	   }
       }
       
       set1.addAll(set2);
       for(String aa:set1)
    	   System.out.println(aa);
        
    }
}






Tags: Java

Posted on Sat, 02 Oct 2021 21:01:51 -0400 by famous58