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

死了好多脑细胞

package com.bj.array;
/**the steps of creating a new "class Exam"
 * 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
 * 	   Noted:1and 3 use the method of fortified "for(){}"+"@override";2 uses the simple "for(){}"method
 * }
 * 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;
            }
        }


    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值