HUAWEI华为Java软件工程师机试题——集合类

1、有如下Student对象,属性如图所示:

软通动力java机考内容 软通动力华为java机考题库_软通动力java机考内容


其中classNum表示学生的班级编号,比如:class05

有如下List:

List list = new ArrayList(); 
 list.add(new Student(“Tom”, 18, 100, “class05”)); 
 list.add(new Student(“Jerry”, 22, 70, “class04”)); 
 list.add(new Student(“Owen”, 25, 90, “class05”)); 
 list.add(new Student(“Jim”, 30,80 , “class05”)); 
 list.add(new Student(“Steve”, 28, 66, “class06”)); 
 list.add(new Student(“Kevin”, 24, 100, “class04”));

在这个list 的基础上,完成下列要求:

1) 计算所有学生的平均年龄

2) 计算各个班级的平均分

2、已知有16支足球队参加2008北京奥运会。写一个程序,随机把16支球队分成四组

3、设计Account对象如下:

软通动力java机考内容 软通动力华为java机考题库_java_02


要求完善设计,使得该Account 对象能够自动分配id。

给定一个List 如下:

List list = new ArrayList(); 
 list.add(new Account(10.00, “1234”)); 
 list.add(new Account(15.00, “5678”)); 
 list.add(new Account(0, “1010”));

要求把List 中的内容放到一个Map 中,该Map 的键为id,值为相应的Account 对象。

最后遍历这个Map,打印所有Account 对象的id 和余额。4、已知某学校课程安排如下:

软通动力java机考内容 软通动力华为java机考题库_System_03


完成下列要求:

1) 使用一个Map,以老师的名字作为键,以老师教授的课程名作为值,表示上述

课程安排。

2) 增加了一位新老师Allen 教JDBC

3) Lucy 改为教CoreJava

4) 遍历Map,输出所有的老师及老师教授的课程

5) *利用Map,输出所有教JSP 的老师。5、利用Map,完成下面的功能:

从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该

年没有举办世界杯,则输出:没有举办世界杯。

软通动力java机考内容 软通动力华为java机考题库_System_04

6、在上题的基础上继续完成:输入球队的名字,输出得冠的年份
如:输入巴西,输出1958 1962 1970 1994 2002
7、完成下面的要求
1) 创建一个List,在List 中增加三个工人,基本信息如下:
姓名 年龄 工资
zhang3 18 3000
li4 25 3500
wang5 22 3200
2) 在li4 之前插入一个工人,信息为:姓名:zhao6,年龄:24,工资3300
3) 删除wang5 的信息
4) 利用for 循环遍历,打印List 中所有工人的信息

我的答案(HCQ,2018/8/7)

1.

package JulyTwelfthHUAWEI;

public class Student {
    private String name;
    private int age;
    private double score;
    private String classNum;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    public String getClassNum() {
        return classNum;
    }
    public void setClassNum(String classNum) {
        this.classNum = classNum;
    }

    public Student(String name, int age, double score, String classNum) {
        super();
        this.name = name;
        this.age = age;
        this.score = score;
        this.classNum = classNum;
    }
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
}

**xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx**

package JulyTwelfthHUAWEI;

import java.util.List;
import java.util.ArrayList;

public class StudentTest {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<Student>();
        list.add(new Student("Tom", 18, 100, "class05"));
        list.add(new Student("Jerry", 22, 70, "class04"));
        list.add(new Student("Owen", 25, 90, "class05"));
        list.add(new Student("Jim", 30, 80, "class05"));
        list.add(new Student("Steve", 28, 66, "class06"));
        list.add(new Student("Kevin", 24, 100, "class04"));

        double sumAge = 0;
        double sumScore4 = 0;
        double sumScore5 = 0;
        double sumScore6 = 0;
        double num4 = 0;
        double num5 = 0;
        double num6 = 0;
        // 1)
        for(Student stu : list) {
            sumAge+=stu.getAge();
        }
        System.out.print("所有学生的平均年龄:");
        System.out.println(sumAge/6.0);
        // 2) 
        for(int i=0; i<list.size(); i++) {
            if(list.get(i).getClassNum().equals("class04")) {
                sumScore4+=list.get(i).getScore();
                num4++;
            }
            if(list.get(i).getClassNum().equals("class05")) {
                sumScore5+=list.get(i).getScore();
                num5++;
            }
            if(list.get(i).getClassNum().equals("class06")) {
                sumScore6+=list.get(i).getScore();
                num6++;
            }
        }
        System.out.print("class04的平均分数:");
        System.out.println(sumScore4/num4);
        System.out.print("class05的平均分数:");
        System.out.println(sumScore5/num5);
        System.out.print("class06的平均分数:");
        System.out.println(sumScore6/num6);
    }
}

2.

package JulyTwelfthHUAWEI;

import java.util.List;

public class Group {
    private int id;
    private String name;
    private List<String> team;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<String> getTeam() {
        return team;
    }
    public void setTeam(List<String> team) {
        this.team = team;
    }
    public Group(int id, String name, List<String> team) {
        super();
        this.id = id;
        this.name = name;
        this.team = team;
    }
    public Group() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "Group [id=" + id + ", name=" + name + ", team=" + team + "]";
    }   
}
**xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx**
package JulyTwelfthHUAWEI;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Football {
    public static void main(String[] args) {
        List<String> teams = new ArrayList<String>();
        Collections.addAll(teams,"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P");
        Collections.shuffle(teams);
        System.out.println(teams);

        Group[] groups = new Group[4];
        for(int i = 0; i<groups.length; i++) {
            groups[i] = new Group(i, "G"+i, new ArrayList<>());
        }

        int n = 0;
        for(int i = 0;i < teams.size();i++){
            groups[n++ % 4].getTeam().add(teams.get(i));
        }
        for(int i = 0; i<groups.length; i++) {
            System.out.println(groups[i].getName()+groups[i].getTeam());
        }

    }
}

3.

package JulyTwelfthHUAWEI;

import java.util.Random;

public class Account {
    private long id;
    private double balance;
    private String password;
    Random rd = new Random();
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public double getBalance() {
        return balance;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Account(double balance, String password) {
        super();
        this.id = Math.abs(rd.nextLong());
        this.balance = balance;
        this.password = password;
    }
    public Account() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "Account [id=" + id + ", balance=" + balance + "]";
    }
}
**xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx**
package JulyTwelfthHUAWEI;

import java.util.ArrayList;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AccountTest {
        public static void main(String[] args) {
            List<Account> list = new ArrayList<Account>();
            list.add(new Account(10.00, "1234"));
            list.add(new Account(15.00, "5678"));
            list.add(new Account(0, "1010"));
            Map<Long, Account> map = new HashMap<>();
            for(Account account : list) {
                map.put(account.getId(), account);
            }

            Collection<Account> c = map.values();
            for (Account a : c) {
                System.out.println(a.toString());
            }
        }
}

4.

package JulyTwelfthHUAWEI;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Teacher {
    public static void main(String[] args) {
        //   1)
        Map<String, String> map = new HashMap<>();
        map.put("Tom", "CoreJava");//1001=jack
        map.put("John", "Oracle");
        map.put("Susan", "Oracle");
        map.put("Jerry", "JDBC");
        map.put("Kevin", "JSP");
        map.put("Lucy", "JSP");
        //   2)
        map.put("Allen", "JDBC");
        //   3)
        map.replace("Lucy", "CoreJava");
        //   4)
        System.out.println(map);
        //   5)
        Set<Entry<String, String>> set = map.entrySet();
        for(Entry<String, String> entry : set) {
            if(entry.getValue().equals("JSP")) {
                System.out.println(entry.getKey());
            }
        }   
    }
}

5. and 6.

package JulyTwelfthHUAWEI;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;

public class WorldCup {
    private static Scanner sc;

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("2006", "意大利");
        map.put("2002", "巴西");
        map.put("1998", "法国");
        map.put("1994", "巴西");
        map.put("1990", "德国");
        map.put("1986", "阿根廷");
        map.put("1982", "意大利");
        map.put("1978", "阿根廷");
        map.put("1974", "德国");
        map.put("1970", "巴西");
        map.put("1966", "英格兰");
        map.put("1962", "巴西");
        map.put("1958", "巴西");
        map.put("1954", "德国");
        map.put("1950", "乌拉圭");
        map.put("1938", "意大利");
        map.put("1934", "意大利");
        map.put("1930", "乌拉圭");

        sc = new Scanner(System.in);
        //   5.
        System.out.print("请输入年份:");
        String year = sc.next();
        if(map.containsKey(year)==true) {
            System.out.print(year+"年的世界杯冠军是:");
            System.out.println(map.get(year));
        } else {
            System.out.println(year+"年没有举办世界杯。");
        }

        //   6.
        System.out.print("请输入冠军球队的国家:");
        String team = sc.next();
        Set<Entry<String, String>> set = map.entrySet();
        System.out.print(team+"队获得世界杯冠军的年份是:");
        for(Entry<String, String> entry : set) {
            if(entry.getValue().equals(team)) { 
                System.out.print(entry.getKey()+" ");
            }
        }       
    }
}

7.

package JulyTwelfthHUAWEI;

public class Worker {
    private String name;
    private int age;
    private int salary;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public Worker(String name, int age, int salary) {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    public Worker() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "Worker [name=" + name + ", age=" + age + ", salary=" + salary + "]";
    }
}
**xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx**
package JulyTwelfthHUAWEI;

import java.util.ArrayList;
import java.util.List;

public class WorkerTest {
    public static void main(String[] args) {
        //   1)
        List<Worker> list = new ArrayList<Worker>();
        list.add(new Worker("zhang3", 18, 3000));
        list.add(new Worker("li4", 25, 3500));
        list.add(new Worker("wang5", 22, 3200));
        //   2)

        list.add(1, new Worker("zhao6", 24, 3300));
        //   3)
        for(int i=0; i<list.size(); i++) {
            if(list.get(i).getName()=="wang5") {
                list.remove(i);
            }   
        }
        //   4)
        for(int i=0; i<list.size(); i++) {
            System.out.println(list.get(i).toString());
        }       
    }
}