JavaSE 学习附加作业——IO 操作相关

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ajianyingxiaoqinghan/article/details/80552184

IO 操作相关作业

题目:

根据 coreJava.txt 文件:

  • 提炼出 Question 类
  • 解析文件(一个函数),要求:
    • 把同个等级的题目,放入同一个 ArrayList 集合;
    • 把等级作为 key,把 ArrayList 集合作为 value,放入 Map 集合;
  • 出一套考题,每个等级一道题目(随机选出 Random)写成一个函数

部分 coreJava.txt 内容如下:

@answer=2/3,score=5,level=5
指出下面语句没有编译错误的是:
long n = 999999999999;
int n = 999999999999L;
long n = 999999999999L; 
double n = 999999999999;
@answer=1/2,score=5,level=4
下列关于数组的声明错误的是:
int[] arry = new int[100];
int[3] arry = {1,2,3};
int[] arry = new int[]{1,2,3};
int[][] arry = new int[3][];
@answer=0/1/2,score=5,level=3
语句System.out.println(1+2+"java"+3+4)输出的结果是:
3java34
12java34
3java7
12java7
@answer=1,score=5,level=6
Java语言中int类型标示整数的最大范围是:
-2147483647~ 2147483647 
<T>-2147483648 ~2147483647 
-32767~32767
-32768~32767
@answer=1,score=5,level=7
Java语言中字符串“学Java”所占的内存空间是:
6个字节
7个字节
10个字节
11个字节
@answer=1,score=5,level=8
对JDK描述错误的是:
运行Java程序只要有JRE环境就可以,不一定需要全部JDK的功能
JDK中包括JVM、核心类库、Java开发工具(java,javac)等
JDK本身也是平台无关的,不同的操作系统安装的JDK是一样的
JDK的全称是 Java Development Kit

答案:

1. Question.java

作用:提炼出 Question 类

package homework4_27;

import java.util.ArrayList;

public class Question {
    private int score;
    private int level;
    private String answer;
    private String description;
    private ArrayList<String> options;

    public Question() {
        this.options = new ArrayList<String>();
    }
    public Question(int score, int level, String answer, String description) {
        super();
        this.score = score;
        this.level = level;
        this.answer = answer;
        this.description = description;
        this.options = new ArrayList<String>();
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public int getLevel() {
        return level;
    }
    public void setLevel(int level) {
        this.level = level;
    }
    public String getAnswer() {
        return answer;
    }
    public void setAnswer(String answer) {
        this.answer = answer;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public ArrayList<String> getoptions() {
        return options;
    }

    public void addOption(String op) {
        options.add(op);
    }
    @Override
    public String toString() {
        return "Question [score=" + score + ", level=" + level + ", answer=" + answer + ", description=" + description
                + ", options=" + options + "]";
    }

    public void showQuestion() {
        System.out.println("Question: " + description);
        System.out.println("Options: ");
        for(String op : options) {
            System.out.println(op);
        }
        System.out.println("Answer: " + answer);
        System.out.println("Score: " + score);
    }
}

2. homework2.java

作用

  • 解析文件:
    • 把同个等级的题目,放入同一个 ArrayList 集合;
    • 把等级作为 key,把 ArrayList 集合作为 value,放入 Map 集合;
package homework4_27;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

// 解析 v2.0,将所有解析后的问题放入单一一个 ArrayList 中
public class homework2 {
    public static void main(String[] args) {
        // 按 level 存入的问题集
        HashMap<Integer, ArrayList<Question> > questWithLevel = new HashMap<Integer, ArrayList<Question> >();

        try {
            BufferedReader br = 
                    new BufferedReader(
                            new InputStreamReader(
                                    new FileInputStream("/Users/upcautolang/Documents/corejava.txt")));
            String str = null;

            Question question = null;
            int extraLineNum = 0;
            while((str = br.readLine()) != null){
                // 包含 @,则可以分析问题的学分、答案、等级
                if(str.contains("@")) {
                    // 将当前非空的 question 传入 questWithLevel 的 Map 集合
                    if(question != null) {
                        int key = question.getLevel();
                        ArrayList<Question> tmpList = new ArrayList<Question>();
                        if(questWithLevel.containsKey(key)) {
                            tmpList = questWithLevel.get(key);
                        }
                        else {
                            tmpList = new ArrayList<Question>();
                        }
                        tmpList.add(question);
                        questWithLevel.put(key, tmpList);
                    }
                    question = new Question();
                    // 切分信息
                    // 例:@answer=2/3,score=5,level=5
                    String[] infos = str.split("[@,][a-z]+=");
                    // 设置答案,成绩,等级
                    question.setAnswer(infos[1]);
                    question.setScore(Integer.parseInt(infos[2]));
                    question.setLevel(Integer.parseInt(infos[3]));

                    extraLineNum = 0; 
                }
                else {
                    if(extraLineNum == 0) {
                        question.setDescription(str);
                        extraLineNum = 1;
                    }
                    else {
                        question.addOption(str);
                        extraLineNum++;
                    }
                }
            }
            // 测试打印
            for(int level : questWithLevel.keySet()) {
                System.out.println("================================");
                System.out.println("Level " + level + " Question Set:");
                System.out.println("---------");
                ArrayList<Question> questionList = questWithLevel.get(level);
                for(Question quest : questionList) {
                    System.out.println(quest);
                }
            }

        } catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}

3. homework3.java

作用:出一套考题,每个等级一道题目(随机选出 Random)写成一个函数

package homework4_27;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

// 解析 v2.0,将所有解析后的问题放入单一一个 ArrayList 中
public class homework3 {
    public static void main(String[] args) {
        // 按 level 存入的问题集
        HashMap<Integer, ArrayList<Question> > questWithLevel = new HashMap<Integer, ArrayList<Question> >();

        try {
            BufferedReader br = 
                    new BufferedReader(
                            new InputStreamReader(
                                    new FileInputStream("/Users/upcautolang/Documents/corejava.txt")));
            String str = null;

            Question question = null;
            int extraLineNum = 0;
            while((str = br.readLine())!=null){
                // 包含 @,则可以分析问题的学分、答案、等级
                if(str.contains("@")) {
                    // 将当前非空的 question 传入 questWithLevel 的 Map 集合
                    if(question != null) {
                        int key = question.getLevel();
                        ArrayList<Question> tmpList = new ArrayList<Question>();
                        if(questWithLevel.containsKey(key)) {
                            tmpList = questWithLevel.get(key);
                        }
                        else {
                            tmpList = new ArrayList<Question>();
                        }
                        tmpList.add(question);
                        questWithLevel.put(key, tmpList);
                    }
                    question = new Question();
                    // 切分信息
                    // 例:@answer=2/3,score=5,level=5
                    String[] infos = str.split("[@,][a-z]+=");
                    // 设置答案,成绩,等级
                    question.setAnswer(infos[1]);
                    question.setScore(Integer.parseInt(infos[2]));
                    question.setLevel(Integer.parseInt(infos[3]));

                    extraLineNum = 0; 
                }
                else {
                    if(extraLineNum == 0) {
                        question.setDescription(str);
                        extraLineNum = 1;
                    }
                    else {
                        question.addOption(str);
                        extraLineNum++;
                    }
                }
            }

            // 随机出题
            System.out.println("=========================");
            System.out.println("     琦少的随机考试");
            System.out.println("=========================");

            for(int level : questWithLevel.keySet()) {
                System.out.println("------- Level " + level + " --------");
                ArrayList<Question> questionList = questWithLevel.get(level);
//              int randomNumber = (int) Math.round(Math.random() * (questionList.size()-1));
                int randomNumber = new Random().nextInt(questionList.size());
                questionList.get(randomNumber).showQuestion();
            }
        } catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券