Java basic exercises three (object-oriented collection of List, Set, Map)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_40914991/article/details/84944711

Here are some exercises java entry, a freshman to assessment questions, especially in this summarize, the main object return, collection (List, Set, Map)

First, the short answer questions

  1. HTTP service default port number and SSH service default port numbers are how much?
    HTTP服务默认端口号为80,SSH服务默认端口号为22

  2. TCP / IP network reference model which includes several levels?
    应用层、传输层、网络层、链路层

  3. Illustrate the concept in TCP / IP, URL and its composition.

    URL是统一资源定位器的英文缩写,它表示Internet上某一资源的地址。
    URL由传输协议、主机名、	端口号、文件名、引用5部分组成。
    
  4. TCP and UDP difference (whether from the connection, transmission reliability, applications, speed brief answer)

    TCP 	      UDP 
    是否连接   	面向连接      面向非连接 
    传输可靠性   可靠          不可靠 
    应用场合     传输大量数据  少量数据 
    速度         慢            快
    
    

Second, the programming problem

1. The following functions required by topic
  • 1) Define a Human class, containing the names of the members name, gender sex, age, age.
  • 2) Define a Professor class, inherited from the Human class, which is a blueprint for teachers
    a) member properties:
    i teach courses Course,.
    Ii teaching effectiveness result of the property value of 0-3. (1 indicates good effect, could accept 2, 3 shows the effect of the poor, 0 were not evaluated)
    b) constructor: Professor used to set the name, sex, age, and taught courses.
    c) member methods:
    . i getDetails the method name, sex, age, and taught courses property returns as a string.
    ii. setResult The method for setting a teaching effects, must be considered reasonable set (if it is between 0-3, then allowed to set).
    iii. getResult the method returns to teaching.
  • 3) preparation of test type of test
    a) create two classes professor1 Professor and professor2, respectively set name, sex, age, and taught courses and output.
    b) professor1 and teaching provided professor2 effects and compare for equality, equal output YES, otherwise the output NO.
/**
 * @author AlgerFan
 * @date Created in 2018/12/6 21
 * @Description 第一题
 */
public class test {
    public static void main(String[] args) {
        Professor professor1 = new Professor("测试1","男",30,"Java程序设计");
        Professor professor2 = new Professor("测试2","男",37,"体育");
        System.out.println(professor1.getDetails());
        System.out.println(professor2.getDetails());
        if(professor1.setResult(1)){
            System.out.println("professor1设置教学效果成功");
        } else {
            System.out.println("professor1设置教学效果失败");
        }
        if(professor1.setResult(2)){
            System.out.println("professor2设置教学效果成功");
        } else {
            System.out.println("professor2设置教学效果失败");
        }
        if(professor1.getResult().equals(professor2.getResult())){
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}

class Human {
    /**
     * 姓名
     */
    public String name;
    /**
     * 性别
     */
    public String sex;
    /**
     * 年龄
     */
    public int age;
}

class Professor extends Human {
    /**
     * 讲授课程
     */
    private String course;
    /**
     * 教学效果  1表示效果良好,2表示可以接受,3表示效果不佳,0表示未予评价
     */
    private int result;

    public Professor(String name, String sex, int age, String course){
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.course = course;
    }

    public String getDetails() {
        return "姓名:"+name+" 性别:"+sex+" 年龄:"+age+" 讲授课程:"+course;
    }

    public boolean setResult(int result) {
        if(result >= 0 && result <= 3){
            this.result = result;
            return true;
        } else {
            return false;
        }
    }

    public String getResult() {
        if(result == 0){
            return "未予评价";
        } else if(result == 1) {
            return "效果良好";
        } else if(result == 2){
            return "可以接受";
        } else {
            return "效果不佳";
        }
    }

}

2. Create a class Course curriculum, course members numbers id, course name courseName, course hours school, course credits credits, write constructor Course for setting the course number, course name and hours, write getter and setter methods for obtaining and set the value of each variable in class, write toString method to return all information about the course.
  • a) create a generic List collection for the Course of adding six objects, respectively (1, "the high number of university", 64,4), (2, "university sports", 36, 1), (3 " Java programming ", 48,3), (4" English reading and writing AI ", 32,2), (4," English reading and writing AI ", 32,2), (5," form and policy ", 96 , 2), which was added to the course number of the course 4 is the same object, the second small problems easy to use, requiring the use of credits for loop output is greater than 2 courses.
  • b) List the first question on the set of de-emphasis, and output the course of de-duplicated.
  • c) Using the iterator will be greater than 49 courses will be deleted when the first question school, and outputs the result after deletion.
  • d) Modify elements of the map, it requires converting the first set of questions List map is set, key value course code id, enter a program number, determining the presence or absence of course, if there is modified the course of hours, and outputs the modified course information, if there is to re-enter the course number.
import java.util.*;

/**
 * @author AlgerFan
 * @date Created in 2018/12/6 22
 * @Description 第二题
 */
class Course {
    /**
     * 课程编号
     */
    private int id;
    /**
     * 课程名称
     */
    private String courseName;
    /**
     * 课程学时
     */
    private int school;
    /**
     * 课程学分
     */
    private int credits;

    public Course(int id, String courseName, int school, int credits) {
        this.id = id;
        this.courseName = courseName;
        this.school = school;
        this.credits = credits;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public int getSchool() {
        return school;
    }

    public void setSchool(int school) {
        this.school = school;
    }

    public int getCredits() {
        return credits;
    }

    public void setCredits(int credits) {
        this.credits = credits;
    }

    @Override
    public String toString() {
        return "Course{" +
                "id=" + id +
                ", courseName='" + courseName + '\'' +
                ", school=" + school +
                ", credits=" + credits +
                '}';
    }
}

public class test2 {
    public static void main(String[] args) {
        System.out.println("第一题:");
        List<Course> courses = new ArrayList<>();
        courses.add(new Course(1,"大学高数",64,4));
        courses.add(new Course(2,"大学体育",36,1));
        courses.add(new Course(3,"Java程序设计",48,3));
        Course course1 = new Course(4, "英语读写AI", 32, 2);
        courses.add(course1);
        courses.add(course1);
        courses.add(new Course(5,"形式与政策",96,2));
        for (Course course : courses) {
            if (course.getCredits() > 2) {
                System.out.print(course+" ");
            }
        }
        System.out.println();
        System.out.println("第二题:");
        Set<Course> set = new HashSet<Course>(courses);
        for (Course course : set) {
            System.out.print(course+" ");
        }
        System.out.println();
        System.out.println("第三题:");
        Iterator<Course> it = courses.iterator();
        while (it.hasNext()){
            if(it.next().getSchool() > 49){
                it.remove();
            }
        }
        for (Course course : courses) {
            System.out.print(course+" ");
        }
        System.out.println();
        System.out.println("第四题:");
        Map<Integer,Course> courseMap = new HashMap<>();
        for (Course course : courses) {
            courseMap.put(course.getId(), course);
        }
        System.out.println("请输入需要修改的课程编号id");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            int id = scanner.nextInt();
            //根据指定的key值获取Value值
            Course course = courseMap.get(id);
            if(course==null) {
                System.out.println("您需要修改的课程信息不存在,请重新输入");
            } else {
                System.out.println("您需要修改的课程学时为"+course.getSchool());
                System.out.println("请输入新的课程学时");
                int newSchool = scanner.nextInt();
                Course newStudent = new Course(id, course.getCourseName(), newSchool, course.getCredits());
                courseMap.put(id, newStudent);
                System.out.println("修改成功");
                break;
            }
        }
        System.out.println("该课程修改后为:"+ courseMap.get(2));

    }
}

Advanced

Define two arrays, write a function to compute their intersection. Example: nums1 [] = {4,9,5}, nums2 [] = {9,4,9,8,4} intersection Output: [9,4]. (The output of each element must be unique, the order does not output.)

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @author AlgerFan
 * @date Created in 2018/12/8 08
 * @Description 附加题
 */
public class test3 {
    public static void main(String[] args) {
        int[] num1 = {1,2,2,1,5,5,6,3,7,9,8};
        int[] num2 = {1,2,2,1,5,5,16,13,10,9,8};
        Set<Integer> hashSet = new HashSet<>();
        Set<Integer> integers = new HashSet<>();
        for (int aNum1 : num1) {
            hashSet.add(aNum1);
        }
        for (int aNum2 : num2) {
            if (hashSet.contains(aNum2)) {
                integers.add(aNum2);
            }
        }
        Iterator<Integer> it = integers.iterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_40914991/article/details/84944711