Initial JavaDay14: Object and Class Exercises

Initial Java Day 14: Object and Class Exercises (1)

1.

Title:

Find the area and circumference of a circle and the distance between two circles.

Ideas for solving problems:

The most important thing here is to understand the idea of object-oriented problem solving, which should include two classes. The first class, Dome02, is the main class. Its sole purpose is to test the second class Circle. Programs that use such classes are often referred to as customers of that class. When running this program, the Java runtime system calls the main method of the main class.

Code example:

//Main Category
class Demo02{
    public static void main(String[] args){
        Circle c1=new Circle();//Create a circle class named c1
        c1.radius=10;
        Point o1=new Point();
        o1.x=0;
        o1.y=0;
        c1.o=o1;
        
        Circle c2=new Circle();
        c2.radius=10;
        Point o2=new Point();
        o2.x=10;
        o2.y=10;
        c2.o=o2;

        System.out.println(c1.getDistance(c2));
        //return;
    }
}
/**
Define a class of circles
@author HENG
@version 1.0
 */
class Circle{
    public double radius;
    public double pi=Math.PI;
    public Point o;

    /**
    Calculate the area of a circle
    @return area
     */
    public double getArea(){
        double area=pi*radius*radius;
        return area;
    }

    /**
    Calculate the circumference of a circle
    @return perimeter
     */
    public double getPerimeter(){
        double perimeter=2*pi*radius;
        return perimeter;
    }

    /**
    Calculate the distance between the current circle and other circles
    @param c Another round object
    @return distance Distance between two circles
     */
    public double getDistance(Circle c){
        double distance=this.o.getDistance(c.o);
        return distance;
    }
}
class Point{
    public double x;
    public double y;
    public double getDistance(Point p){
        return Math.sqrt(Math.pow(this.x-p.x,2)+Math.pow(this.y-p.y,2));
    }
}

2.

Title:

A simulation of gunfight

Ideas for solving problems:

The main problem is to divide the whole process into four parts - players, guns, magazines, bullets, and then write out four parts separately, and connect the four parts through the main class. The difficulty is to understand the logical relationship of each part. The following is a UML diagram:

Code details:

1. GunWar:

/**
 * main program
 * Creating Users
 *  Implementation steps
 */
class GunWar{
    public static void main(String[] args) {
    //Creating Players
        Player p1=new Player("Lao Zhang",100);
        Player p2=new Player("Lao Wang",100);
        p1.shoot(p2);

        Gun gun=new Gun();
        p1.takeGun(gun);
        p1.shoot(p2);

        Clip clip=new Clip();
        p1.loadClip(clip);
        p1.shoot(p2);
        System.out.println("=========");
        for(int i=1;i<=40;i++){
            clip.putBullet(new Bullet());
        }
        System.out.println("=========");
        for(int i=1;i<=11;i++){
            p1.shoot(p2);
        }    
    }
}

2. player code:

/**
 * Game player
 * Attributes:
 * Name (name) HP gun
 * Behavior:
 * takeGun Loading Clip Shooting Shot Injured bloodLoss
 */
class Player{
    private String  name;
    private int HP;
    private Gun gun;
/**
 Create a player
 */
    public Player(String name,int HP){
        this.name=name;
        this.HP=HP;
        this.gun=null;
        System.out.println(this.name+"Birth, his HP by"+this.HP);
    }
    /**
     * Take a gun
     */
    public void takeGun(Gun gun){
        if(gun==null){
            System.out.println("Game player"+name+"There's no gun on hand yet.");
        }else{
            System.out.println("Game player"+name+"Get a gun");
        }
    }
    /**
     * The state of the player's hand clip
     * @param clip
     */
    public void loadClip(Clip clip){
        if(this.gun==null){
            System.out.println("Game player"+name+"No gun, no magazine");
        }else{
            System.out.println("Game player"+name+"The cartridge clip has been fitted.");
            this.gun.loadClip(clip);
        }
    }
    /**
     Shooting
    */
    public void shoot(Player enemy){
        if(this.gun==null){
            System.out.println("Game player"+name+"No gun, no way"+enemy.getName()+"Shooting");
        }else{
            System.out.println("Game player"+name+"To Players"+enemy.getName()+"A shot was fired.");
            this.gun.fire(enemy);
        }
    }
    public void booldLose(int damage){
        if(this.HP>damage){
            this.HP-=damage;
            System.out.println("Game player"+name+"Receiving injuries"+damage+",Surplus"+this.HP+"Blood volume");
        }else{
            System.out.println("Game player"+name+"Dead");
        }
    }
    public String getName(){
        return this.name;
    }
}

3. gun code:

/**
 * gun
 * Attributes:
 * clip
 * Behavior:
 * fire loading
 */
class Gun{
    private Clip clip;

    public void loadClip(Clip clip){
        this.clip=clip;
        System.out.println("The gun was loaded with cartridge clips.");
    }
    public void fire(Player enemy){
        if(clip==null){
            System.out.println("Bullet clip no bullet, empty sound");
        }else{
            //Get the number of bullets in the clip first
            Bullet bullet=clip.getBullet();
            if(bullet!=null){
                System.out.println("Shooting direction"+enemy.getName()+"A bullet was fired.");
                bullet.hit(enemy);
            }else{
                System.out.println("An empty gun was fired.");
            }            
        }
    }
}

4. clip code:

* Attributes:
 * Bullet bullets Bullet clip capacity
 * behavior
 * Bullet loading
 */
class Clip{
    private Bullet[] bullets;
    private int size;

    public Clip(){
        this.bullets=new Bullet[30];//Bullet capacity
        this.size=0;
    }
    /**
     * Output bullet
     * @return
     */
    public Bullet getBullet(){
        if(size==0){
            System.out.println("The cartridge clip is empty");
            return null;
        }else{
            Bullet bullet=bullets[size-1];
            size--;
            System.out.println("Bullet clip surplus"+size+"/"+bullets.length);
        }
    }
    /**
     * Bullet loading
     * @param bullet
     */
    public void putBullet(Bullet bullet){
        if(size==bullets.length){
            System.out.println("The cartridge clip is full");
        }else{
            bullets[size]=bullet;
            size++;
            System.out.println("Bullet clip surplus"+size+"/"+bullets.length);
        }
    }
}

5. bullet code:

/**
 * Bullet
 * Attribute: Damage value damage 
 * Behavior: hit
 */
class Bullet{
    private int damage=30;
    public void hit(Player enemge){
        System.out.println("A bullet hit"+enemge);
        enemge.booldLose(damage);
    }
}

 

 

 

 

Tags: Java Attribute

Posted on Wed, 09 Oct 2019 23:09:38 -0700 by aravona