ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【Java课堂作业】利用数组方法,使用三种初始化方式,编写考试分数和科目

2021-05-03 18:29:35  阅读:181  来源: 互联网

标签:考试分数 Java Exam 课堂作业 Step score new public subject


死了好多脑细胞

package com.bj.array;
/**the steps of creating a new "Class"
 * Step 1: to creat a new Class
 * Using the "private" to encapsulate the "Object"
 *Step 2:All+Insert调用Getter and Setter
 *Step 3:All+Insert调用Constructor
 *Step 4:to set a null of this "Class"
 *Step 5:to set a entrance of method,  then print it out
 *There are 4 subjects in this exercise:Java, Python, Bigdata, AI
 * entrance: public static void main(String[] args){
 *     to use 3 initialization method to print it
 *     1.Default initialization
 *     2.Static initialization
 *     3.Dynamic initialization
 * }
 * to use a for(){}
 * Step 6:to add a override:keyboard "toString" between the "Step 3:Constructor"and "Step 2:Getter and setter"
 */
public class exercise {
    // There are 4 subjects in this exercise:Java, Python, Bigdata, AI
    public static void main(String[] args) {
        /* 1.Static initialization */
        
        int[] i = {60, 70, 80, 90};//静态初始化基本类型数组
        Exam[] exams = {//给subject数组分配空间和赋值
                new Exam(60, "java"),
                new Exam(70, "python"),
                new Exam(80, "bigData"),
                new Exam(90, "AI")
        };
        //做一个强化for循环的打印,需要在步骤3和步骤2之间加上一个步骤6:Step 6
        for (Exam exam : exams) {
            System.out.println(exam);
        }
        //2.Dynamic initialization
        int[] e1 = new int[4];//动态初始化数组,先分配空间,并说明一共有4门科目
        Exam[] exams1 = {//给subject数组分配空间和赋值
                new Exam(60, "java"),
                new Exam(70, "python"),
                new Exam(80, "bigData"),
                new Exam(90, "AI")};

        for (int j = 0; j < 4; j++) {
            System.out.println(exams1[j]);//尝试了getScore() and getSubject()反而只能打印出一半,所以就直接写
            //exams1[j]全部打印,就出分数和科目名称了
        }
        //3.Default initialization
        int score[] = new int[4];//默认值0,0
        String[] subject = new String[4];//默认值null,null
        Exam[] exams2 = {//给subject数组分配空间和赋值
                new Exam(60, "java"),
                new Exam(70, "python"),
                new Exam(80, "bigData"),
                new Exam(90, "AI")};
        for(Exam exam3:exams2){
            System.out.println(exam3);
        }

        }
        //Step 1:to creat a class and to private package
        static class Exam {
            private int score;//score
            private String subject;//my subject

            //Step 4: to set a null of this "Class"
            public Exam() {
            }

            //Step 3: to use the keyboard :all+insert to add "Constructor"
            public Exam(int score, String subject) {
                this.score = score;
                this.subject = subject;
            }

            //step 6:to override:toString()
            @Override
            public String toString() {
                return "Exam{" +
                        "score=" + score +
                        ", subject='" + subject + '\'' +
                        '}';
            }

            //Step 2:to use the keyboard:all+insert to add "Getter and Setter"
            public int getScore() {
                return score;
            }

            public void setScore(int score) {
                this.score = score;
            }

            public String getSubject() {
                return subject;
            }

            public void setSubject(String subject) {
                this.subject = subject;
            }
        }


    }

标签:考试分数,Java,Exam,课堂作业,Step,score,new,public,subject
来源: https://blog.csdn.net/weixin_43963253/article/details/116379187

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有