Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java practice questions that contain classes, methods, objects, property calls, this usage, and construction methods


Jun 01, 2021 Article blog


Table of contents


This is a Java practice post, the article is arranged with exercises, explained the requirements, and test points. T his is followed by the code for the exercise. Interested students can write their own code, and then compare with the code in the text to see what is worth learning.

Java Exercise Question 1

Title: Create a hero class and monster class in LOL, requiring:

Monster class adds the life value genus, the hierarchical attribute, the life value is the level of 1000

The hero class adds "experience value", "level", "attack force" and other attribute variables, adds the "hit" method, hits the target monster, the result of the hit is the monster's life value - hero attack power

Test points: class declarations, method declarations, object declarations, and property calls

Hero class

public class Hero {


        int EXP;//经验值
        int grade;//等级
        int aggressivity;//攻击

        
        //击打的方法
        int strike(int x){
            return x - aggressivity;
        }

        
        public int getEXP() {
            return EXP;
        }






        public void setEXP(int EXP) {
            this.EXP = EXP;
        }






        public int getGrade() {
            return grade;
        }






        public void setGrade(int grade) {
            this.grade = grade;
        }






        public int getAggressivity() {
            return aggressivity;
        }






        public void setAggressivity(int aggressivity) {
            this.aggressivity = aggressivity;
        }


}

Monster class

public class Monster {


        int grade;//等级
        int healthValue ;//生命值

        
        public int getGrade() {
            return grade;
        }
        public void setGrade(int grade) {
            this.grade = grade;
        }
        public int getHealthValue() {
            healthValue = grade*1000;
            return healthValue;
        }
        public void setHealthValue(int healthValue) {
            this.healthValue = healthValue;
        }



        
}

main

public class Work1 {


    public static void main(String[] args) {
        //怪物属性
        Monster m = new Monster();
        m.setGrade(5);
        System.out.println("怪物的等级为"+m.grade+"级  生命值为:"+m.getHealthValue());

        
        //英雄 
        Hero h = new Hero();
        h.setAggressivity(160);
        int a;
        a = h.strike(m.healthValue);
        m.setHealthValue(a);
        System.out.println("英雄击打后的生命值:"+m.healthValue);
    }
}

 Java practice questions that contain classes, methods, objects, property calls, this usage, and construction methods1

The second question of the Java exercise

Title: Custom a Hero Class, requiring:

Heroes have a minimum level of 0, a maximum level of 30, an experience value of 0, and a maximum of 30 levels of experience

The class requires a constructor with parameters, parameters for the hero's experience value, initialization to ensure that the experience value within the requirements, through the experience value to calculate the hero's level, the hero's level calculation formula is as follows: N - current level, EXP (experience value) - 30 (N ,3 plus 5N) - 80

Build an unsexy construction method that sets the empirical value to 0

Build hero objects separately with hero class parameters and constructors with parameters, and output the hero's level and experience values.

Test point: the declaration of the class, the construction method, the use of this, the call of the method

Hero class

public class Hero2 {


    int EXP;//经验值
    int grade;//等级

    
    //有参的构造函数
    public Hero2(int EXP){
        this.EXP = EXP;//经验值
        }

    
    //无参的构造函数
    public Hero2(){
        EXP=0;//经验值
    }

    
}

main

public class Work2 {


    public static void main(String[] args) {
        //无参构造结果
        Hero2 e= new Hero2();
        System.out.print("无参构造的");
        System.out.println("经验值为:"+e.EXP+" 时,等级为:"+e.grade+" 级");

        
        //有参构造结果
        int a=120000;
        Hero2 h = new Hero2(a);
        System.out.print("有参构造的");
        for(int i = 1 ; i <=30 ; i++ ) {

            
            //System.out.println(i+"级经验值:"+(30*(i*i*i+5*i)-80));//输出1-30级每级的经验值所需经验
            if((30*(i*i*i+5*i)-80) <= h.EXP && (30*((i+1)*(i+1)*(i+1)+5*(i+1))-80)>h.EXP ) {
                h.grade=i;
                break;
            }
        }
        System.out.println("经验值为:"+h.EXP+" 时,等级为:"+h.grade+" 级");
    }


}

 Java practice questions that contain classes, methods, objects, property calls, this usage, and construction methods2

These are two Java exercises and code, I hope to help students. Students interested in Java can take a look at the tutorial

Source: blog.csdn.net/d694046387/java/article/details/107620181