面向对象技术书面作业3

题目:定义一个Person类,它包括的属性有“姓名”和“性别”。为Person类派生出一个子类Student类,为Student子类添加两个属性年龄和成绩,在子类中打印出学生的姓名、性别、年龄及成绩。定义测试类,生成Person和Student类的对象,并调用相关方法输出学生的姓名、性别、年龄和成绩等。

//为Student子类添加两个属性年龄和成绩,
// 在子类中打印出学生的姓名、性别、年龄及成绩。
public class Student extends Person{
    private int age;
    private double score;

    //构造方法
    public Student(){

    }
    public Student(String name,char sex,int age,double score){
        super(name, sex);
        this.age =age;
        this.score =score;
    }

    public void print(){
        System.out.println("学生名字是 " + this.getName() + " 是" + this.getSex()
                +"生 , 年龄是 " + this.age + " 岁,成绩是 " + this.score);
    }

    //setter and getter

    public int getAge() {
        return age;
    }

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

    public double getScore() {
        return score;
    }

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

public class Student extends Person{
    private int age;
    private double score;

    //构造方法
    public Student(){

    }
    public Student(String name,char sex,int age,double score){
        super(name, sex);
        this.age =age;
        this.score =score;
    }

    public void print(){
        System.out.println("学生名字是 " + this.getName() + " 是" + this.getSex()
                +"生 , 年龄是 " + this.age + " 岁,成绩是 " + this.score);
    }

    //setter and getter

    public int getAge() {
        return age;
    }

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

    public double getScore() {
        return score;
    }

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

// 定义测试类,生成Person和Student类的对象,
// 并调用相关方法输出学生的姓名、性别、年龄和成绩等。

public class Test {
    public static void main(String[] args) {
        //无参构造
        Person person1 =new Person();
        //有参构造
        Person person2 = new Person("小明",'男');
        //无参构造
        Student student1 = new Student();
        //有参构造
        Student student2 =new Student("小红",'女',18,80.0);

        //输出学生的信息
        student1.print();

        student2.print();
    }
}

标题题目一:1.设计一个Personal类:

(1)该类有实例变量(属性)id、name、age、sex;
(2)一个无参构造函数和一个有参构造函数;
(3)一组用于访问实例变量的set/get方法。
再设计一个测试类TestPersonal,测试该类。

public class Personal {
	//成员变量
    private int id;
    private String name;
    private int age;
    private char sex;

    //无参
    public Personal(){

    }
    //有参
    public Personal(int id,String name,int age,char sex){
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    //setter and getter

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }
}

public class TestPersonalTestPersonal {
    public static void main(String[] args) {
        Personal personal1 = new Personal();
        Personal personal2 = new Personal();
        Personal personal3 = new Personal(123,"小明",18,'男');

        personal2.setId(122);
        personal2.setName("小红");
        personal2.setAge(19);
        personal2.setSex('女');

        System.out.println(personal1.getId() + " 号" + personal1.getName() + " 是一个"+ personal1.getSex() + "生,今年" + personal1.getAge());

        System.out.println(personal2.getId() + " 号" + personal2.getName() + " 是一个"+ personal2.getSex() + "生,今年" + personal2.getAge());

        System.out.println(personal3.getId() + " 号" + personal3.getName() + " 是一个"+ personal3.getSex() + "生,今年" + personal3.getAge());
    }
}

题目二:2.设计一个类Student类,该类从Personal派生,该类有学生学号studentID、所在的系部department、年级grade。新的Student类中有相应的构造函数、查询及输出学生个人信息的getInfo()与print()方法。再设计一个测试类TestStudent,测试该类。

public class Student extends Personal {
    private int studentID;
    private String department;
    private int grade;

    public Student() {

    }

    public Student(int studentID, String department, int grade) {
        super();
        this.studentID = studentID;
        this.department = department;
        this.grade = grade;
    }

    public Student(int id, String name, int age, char sex, int studentID, String department, int grade) {
        super(id, name, age, sex);
        this.studentID = studentID;
        this.department = department;
        this.grade = grade;
    }

    public int getStudentID() {
        return studentID;
    }

    public void setStudentID(int studentID) {
        this.studentID = studentID;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public int getGrade() {
        return grade;
    }

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

    //print()方法
    //int id, String name, int age, char sex, int studentID, String department, int grade
    public void print(){
        System.out.println("ID: " +this.getId() + " name:" + this.getName()+" age: " +this.getAge() +
                " sex: " + this.getAge() + " studentID: " + this.getStudentID() +" department: "+ this.getDepartment() +
                " grade: " +this.getGrade());
    }
}

public class TestStudent {
    public static void main(String[] args) {

        //无参构造
        Student student1 =new Student();
        //有参构造
        Student student2 = new Student(111,"电子院",2);
        Student student3 =new Student(1111,"小明",18,'男',112,"电子院",3);

        //打印信息
        student1.print();
        student2.print();
        student3.print();

    }
}

作业四: 4、设Shape是圆、矩形、三角形共享的类,含有求圆的周长和面积的方法getCircumference()与getArea(),以及属性DESCRIPTION以描述几何体的特征,试设计相关类圆、矩形、三角形类,并求圆、矩形、三角形的周长与面积。设计相应的测试类,求实际对象的面积与方法。(要求分别将Shape类用抽象、接口实现)。

抽象类实现:

//Shape抽象类
public abstract class Shape {
    //属性
    String DESCRIPTION;
    //计算周长和面积的方法
    public abstract double getCircumference();
    public abstract double getArea();

    //抽象类一定要有构造方法
    public Shape() {
    }

    public Shape(String DESCRIPTION) {
        this.DESCRIPTION = DESCRIPTION;
    }
}

圆类继承shape抽象类

public class Round extends Shape{

    double radius;

    //构造方法

    public Round() {
    }

    public Round(double radius) {
        this.radius = radius;
    }

    public Round(String DESCRIPTION, double radius) {
        super(DESCRIPTION);
        this.radius = radius;
    }

    //重写抽象类的方法
    @Override
    //返回圆的周长
    public double getCircumference() {
        return 2*Math.PI*this.radius;
    }

    @Override
    //返回圆半径
    public double getArea() {
        return Math.PI*Math.pow(this.radius,2);
    }
}

矩形继承Shape抽象类

public class Rectangular extends Shape{
    int length;
    int width;

    //构造方法
    public Rectangular() {
    }

    public Rectangular(int length, int width) {
        this.length = length;
        this.width = width;
    }

    public Rectangular(String DESCRIPTION, int length, int width) {
        super(DESCRIPTION);
        this.length = length;
        this.width = width;
    }

    @Override
    public double getCircumference() {
        return (this.length+this.width)*2;
    }

    @Override
    public double getArea() {
        return this.length*this.width;
    }
}

三角形实现shape抽象类

public class Triangle extends Shape{
    int line1;
    int line2;
    int line3;
    //构造方法
    public Triangle(){

    }

    public Triangle(String DESCRIPTION,int line1,int line2 ,int line3){
        super(DESCRIPTION);
        this.line1 =line1;
        this.line2 =line2;
        this.line3 =line3;
    }

    @Override
    public double getCircumference() {
        return line1+line2+line3;
    }

    @Override
    public double getArea() {
        //海伦公式
        double s = (line1 + line2 + line3)/2;
        return Math.pow(s*(s - line1)*(s - line2)*(s - line3),0.5);
    }
}

测试一下看实现的怎么样

public class Test {
    public static void main(String[] args) {
        //创建半径为4的圆
        Round round =new Round("这是一个半径为4的圆",4);
        System.out.println("圆的周长是" + round.getCircumference());
        System.out.println("圆的面积是" + round.getArea());

        //创建一个长为4,宽为5的矩形
        Rectangular rectangular =new Rectangular("这是一个一个长为4,宽为5的矩形",4,5);
        System.out.println("矩形的周长是" + rectangular.getCircumference());
        System.out.println("矩形的面积是" + rectangular.getArea());

        //创建一个边长为3,4,5的三角形
        Triangle triangle = new Triangle("这是一个一个边长为3,4,5的三角形",3,4,5);
        System.out.println("三角形的周长是" +triangle.getCircumference());
        System.out.println("三角形的面积是" + triangle.getArea());
    }
}

**

接口实现

**
shape接口

public interface Shape {
    //描述几何体特征的
    String DESCRIPTION = null;

    //计算周长
    public abstract double getCircumference();
    //计算面积
    public abstract double getArea();
}

圆实现接口

public class Round implements Shape {

    double radius;


    //无参构造
    public Round() {
    }

    //有参构造
    public Round(double radius) {
        this.radius = radius;
    }

    @Override
    //返回圆的周长
    public double getCircumference() {
        return 2*Math.PI*this.radius;
    }

    @Override
    //返回圆的面积
    public double getArea() {
        return Math.PI*Math.pow(this.radius,2);
    }
}

矩形实现接口

public class Rectangular implements Shape{
    int length;
    int width;

    //无参构造
    public Rectangular() {

    }

    //有参构造
    public Rectangular(int length, int width) {
        this.length = length;
        this.width = width;
    }

    @Override
    //获取矩形的周长
    public double getCircumference() {
        return (this.length+this.width)*2;
    }

    @Override
    //获取矩形的面积
    public double getArea() {
        return this.length*this.width;
    }
}

三角形实现接口

public class Triangle implements Shape {
    int line1;
    int line2;
    int line3;

    //无参构造
    public Triangle() {
    }

    //有参构造
    public Triangle(int line1, int line2, int line3) {
        this.line1 = line1;
        this.line2 = line2;
        this.line3 = line3;
    }

    @Override
//    获取三角形的周长
    public double getCircumference() {
        return line1+line2+line3;
    }

    @Override
//    获取三角形的面积
    public double getArea() {
        //海伦公式
        double s = (line1 + line2 + line3)/2;
        return Math.pow(s*(s - line1)*(s - line2)*(s - line3),0.5);
    }
}

来测试一下

public class Test {
    public static void main(String[] args) {
        //创建一个半径为4的圆
        Round round = new Round(4);
        System.out.println("圆的周长是" + round.getCircumference());
        System.out.println("圆的面积是" + round.getArea());

        //创建一个长为4,宽为5的矩形
        Rectangular rectangular =new Rectangular(4,5);
        System.out.println("矩形的周长是" + rectangular.getCircumference());
        System.out.println("矩形的面积是" + rectangular.getArea());

        //创建一个边长为3,4,5的三角形
        Triangle triangle = new Triangle(3,4,5);
        System.out.println("三角形的周长是" +triangle.getCircumference());
        System.out.println("三角形的面积是" + triangle.getArea());
    }
}


版权声明:本文为Destiny_159原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。