作业2

1题.

Suansu类
作业2_第1张图片

package cdtu.ll.job4;

public class Suansu {

    private int a;
    private int b;

    public Suansu()
    {
        this.a=10;
        this.b=5;
    }

    public int addAb()
    {
        return this.a+this.b;
    }

    public int subAb()
    {
        return this.a-this.b;
    }

    public int MultiAB()
    {
        return this.a*this.b;
    }

    public int divAb()
    {
        return this.a/this.b;
    }
}

TestSusansu类
作业2_第2张图片

package cdtu.ll.job4;

public class TestSusansu {

    /**
     * @auth 龙灵
     * @param args
     */
    public static void main(String[] args) {

        Suansu suansu=new Suansu();
        System.out.println("a+b="+suansu.addAb());
        System.out.println("a-b="+suansu.subAb());
        System.out.println("a*b="+suansu.MultiAB());
        System.out.println("a/b="+suansu.divAb());

    }
}

2题、

1)Rectangle类
作业2_第3张图片

package cdtu.ll.job1;

public class Rectangle {

    private int width;
    private int height;

    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }

    /**
     * 计算面积
     * 取值范围0-50,反之返回-1重新输入值
     * @return
     */
    public int getArea()
    {
        if((this.width>0&&this.width<50)&&(this.height>0&&this.height<50))
        {
            return this.width*this.height;
        }
        return -1;
    }

    //计算周长
    public int getPerimeter()
    {
        return (this.width+this.height)*2;
    }

    //画图
    public void draw()
    {
        for(int i=0;i<this.height;i++)
        {
            for(int j=0;j<this.width;j++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
    }

}

2)TestRectangle类
作业2_第4张图片

package cdtu.ll.job1;

public class TestRectangle {

    /**
     * @auth 龙灵
     * @param args
     */
    public static void main(String[] args) {
        //长6宽3
        Rectangle rectangle=new Rectangle(6,3);
        System.out.println("面积:"+rectangle.getArea());
        System.out.println("周长:"+rectangle.getPerimeter());
        System.out.println("图像:");
        rectangle.draw();
    }
}


3题、

Student类
作业2_第5张图片

package cdtu.ll.job2;

public class Student {

    private String name;
    private int age;

    public Student()
    {
        this.age=20;
        this.name="无名氏";
    }

    public Boolean isSameAge(Student stu)
    {
        if(this.age==stu.age)
        {
            return true;
        }
        return false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

4、

Complex类
作业2_第6张图片

package cdtu.ll.job3;

public class Complex {

    private int realPart;
    private int imaginPart;

    public Complex()
    {
        this.realPart=0;
        this.imaginPart=0;
    }

    public Complex(int realPart, int imaginPart) {
        this.realPart = realPart;
        this.imaginPart = imaginPart;
    }

    Complex complexAdd(Complex complex)
    {
        return new Complex(this.realPart+complex.realPart,this.imaginPart+complex.imaginPart);
    }

    public String toString() {
        return realPart+"+"+imaginPart+"i";
    }

}

5题、

MyPoint类
作业2_第7张图片

package cdtu.ll.job5;

public class MyPoint {

    private int x;
    private int y;

    public MyPoint()
    {
        this.x=0;
        this.y=0;
    }

    public MyPoint(int x,int y)
    {
        this.x=x;
        this.y=y;
    }

    public float getD(MyPoint myPoint)
    {

        return (float) Math.sqrt(((x-myPoint.x)*(x-myPoint.x)+(y-myPoint.y)*(y-myPoint.y)));
    }

    /**
     * @auth 龙灵
     * @param args
     */
    public static void main(String[] args) {
        MyPoint myPoint=new MyPoint(2,3);
        MyPoint myPoint1=new MyPoint(4,5);
        System.out.println("距离:"+myPoint.getD(myPoint1));
    }
}

你可能感兴趣的:(java复习)