PTA 2021级-JAVA06 继承和多态、抽象类和接口

一、函数题

6-1 创建一个直角三角形类实现IShape接口

创建一个直角三角形类(regular triangle)RTriangle类,实现下列接口IShape。两条直角边长作为RTriangle类的私有成员,类中包含参数为直角边的构造方法。

interface IShape {// 接口

public abstract double getArea(); // 抽象方法 求面积

public abstract double getPerimeter(); // 抽象方法 求周长

}

###直角三角形类的定义:

直角三角形类的构造函数原型如下:
RTriangle(double a, double b);

其中 a 和 b 都是直角三角形的两条直角边。

裁判测试程序样例:


import java.util.Scanner;
import java.text.DecimalFormat;
 
interface IShape {
    public abstract double getArea();
 
    public abstract double getPerimeter();
}
 
/*你写的代码将嵌入到这里*/


public class Main {
    public static void main(String[] args) {
        DecimalFormat d = new DecimalFormat("#.####");
        Scanner input = new Scanner(System.in);
        double a = input.nextDouble();
        double b = input.nextDouble();
        IShape r = new RTriangle(a, b);
        System.out.println(d.format(r.getArea()));
        System.out.println(d.format(r.getPerimeter()));
        input.close();
    }
}

输入样例:

3.1 4.2

输出样例:

6.51
12.5202
class RTriangle implements IShape//implements 是实现接口,可以实现多个接口,用逗号分开就行了
//比如:class A extends B implements C,D,E
//extends与implements两种实现的具体使用,是要看项目的实际情况
//需要实现,不可以修改implements
//只定义接口需要具体实现,或者可以被修改扩展性好,用extends。
{
    public RTriangle (double a, double b)
    {
        this.a = a;
        this.b = b;
    }
    double a,b;
    public double getArea() {
        return 0.5*a*b;
    }
    public double getPerimeter() {
        return a + b + Math.sqrt(a*a+b*b);
    }
}

6-2 从抽象类shape类扩展出一个圆形类Circle

请从下列的抽象类shape类扩展出一个圆形类Circle,这个类圆形的半径radius作为私有成员,类中应包含初始化半径的构造方法。

public abstract class shape {// 抽象类

public abstract double getArea();// 求面积

public abstract double getPerimeter(); // 求周长

}

主类从键盘输入圆形的半径值,创建一个圆形对象,然后输出圆形的面积和周长。保留4位小数。

圆形类名Circle

裁判测试程序样例:

import java.util.Scanner;
import java.text.DecimalFormat;
 
abstract class shape {// 抽象类
     /* 抽象方法 求面积 */
    public abstract double getArea( );
 
    /* 抽象方法 求周长 */
    public abstract double getPerimeter( );
}

/* 你提交的代码将被嵌入到这里 */

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        DecimalFormat d = new DecimalFormat("#.####");// 保留4位小数
         double r = input.nextDouble( ); 
        shape c = new  Circle(r);
 
        System.out.println(d.format(c.getArea()));
        System.out.println(d.format(c.getPerimeter()));
        input.close();
    } 
}

输入样例:

3.1415926

输出样例:

31.0063
19.7392
class Circle extends shape
{//extends 拓展
    private double radius;//私有成员,本类中可用的,表面上是说只有本类中可以使用(更改)该变量或者方法。
    public Circle(double radius) 
    {
        this.radius = radius;
    }
    public double getArea() 
    {
        return Math.PI*radius*radius;
    }
    public double getPerimeter() 
    {
        return 2*Math.PI*radius;
    }
}

6-3 模拟题: 重写父类方法equals

在类Point中重写Object类的equals方法。使Point对象x和y坐标相同时判定为同一对象。

裁判测试程序样例:

 

import java.util.Scanner; class Point { private int xPos, yPos; public Point(int x, int y) { xPos = x; yPos = y; } @Override /* 请在这里填写答案 */ } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Object p1 = new Point(sc.nextInt(),sc.nextInt()); Object p2 = new Point(sc.nextInt(),sc.nextInt()); System.out.println(p1.equals(p2)); sc.close(); } }

输入样例:

10 20
10 20

输出样例:

true
    public boolean equals(Object o)//Object类是所有类的父类,任何一个类如果没有明确的继承一个父类的话,那么它就是Object的子类;
    {
        //boolean 数据类型 boolean 变量存储为 8 位(1 个字节)的数值形式,但只能是 True 或是 False
        //equals 方法是String类从它的超类Object中继承的, 被用来检测两个对象是否相等,即两个对象的内容是否相等,区分大小写
        Point p = (Point)o;
        if(this.xPos==p.xPos&&this.yPos==p.yPos)
        {
            return true;
        }
        return false;
    }

6-4 重写父类方法equals

在类Student中重写Object类的equals方法。使Student对象学号(id)相同时判定为同一对象。

函数接口定义:

在类Student中重写Object类的equals方法。使Student对象学号(id)相同时判定为同一对象。

裁判测试程序样例:

import java.util.Scanner; class Student { int id; String name; int age; public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } /* 请在这里填写答案 */ } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Student s1 = new Student(sc.nextInt(),sc.next(),sc.nextInt()); Student s2 = new Student(sc.nextInt(),sc.next(),sc.nextInt()); System.out.println(s1.equals(s2)); sc.close(); } }

输入样例:

1001 Peter 20
1001 Jack 18

输出样例:

true
public boolean equals(Object o)
{
    Student s = (Student)o;
    if(this.id==s.id)
    {
        return true;
    }
    return false;
}

6-5 根据派生类写出基类(Java)

裁判测试程序样例中展示的是一段定义基类People、派生类Student以及测试两个类的相关Java代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。

函数接口定义:

 

提示: 观察派生类代码和main方法中的测试代码,补全缺失的代码。

裁判测试程序样例:

注意:真正的测试程序中使用的数据可能与样例测试程序中不同,但仅按照样例中的格式调用相关方法(函数)。

 

class People{ protected String id; protected String name; /** 你提交的代码将被嵌在这里(替换此行) **/ } class Student extends People{ protected String sid; protected int score; public Student() { name = "Pintia Student"; } public Student(String id, String name, String sid, int score) { super(id, name); this.sid = sid; this.score = score; } public void say() { System.out.println("I'm a student. My name is " + this.name + "."); } } public class Main { public static void main(String[] args) { Student zs = new Student(); zs.setId("370211X"); zs.setName("Zhang San"); zs.say(); System.out.println(zs.getId() + " , " + zs.getName()); Student ls = new Student("330106","Li Si","20183001007",98); ls.say(); System.out.println(ls.getId() + " : " + ls.getName()); People ww = new Student(); ww.setName("Wang Wu"); ww.say(); People zl = new People("370202", "Zhao Liu"); zl.say(); } }

输入样例:

在这里给出一组输入。例如:

(无)

输出样例:

在这里给出相应的输出。例如:

I'm a student. My name is Zhang San.
370211X , Zhang San
I'm a student. My name is Li Si.
330106 : Li Si
I'm a student. My name is Wang Wu.
I'm a person! My name is Zhao Liu.
public People(String id, String name) {//首先根据Main中的zs.setID\Name say可以得出Student类中包含这几个方法
    this.id = id;
    this.name = name;
}
public void setId(String id) {
    this.id = id;
}
public void setName(String name) {
    this.name = name;
}
public void say()
{
    System.out.println("I'm a person! My name is " + this.name + ".");//从最后一行输出可知
}
//又由zs.getId()与zs.getName()可知
public String getId(){
    return id;
}
public String getName()
{
    return name;
}
public People(){
    name = "Pintia Student";
}

6-6 写出派生类构造方法(Java)

裁判测试程序样例中展示的是一段定义基类People、派生类Student以及测试两个类的相关Java代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。

函数接口定义:

 

提示: 观察类的定义和main方法中的测试代码,补全缺失的代码。

裁判测试程序样例:

注意:真正的测试程序中使用的数据可能与样例测试程序中不同,但仅按照样例中的格式调用相关方法(函数)。

 

class People{ private String id; private String name; public People(String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public String getName() { return name; } } class Student extends People{ private String sid; private int score; public Student(String id, String name, String sid, int score) { /** 你提交的代码将被嵌在这里(替换此行) **/ } public String toString(){ return ("(Name:" + this.getName() + "; id:" + this.getId() + "; sid:" + this.sid + "; score:" + this.score + ")"); } } public class Main { public static void main(String[] args) { Student zs = new Student("370202X", "Zhang San", "1052102", 96); System.out.println(zs); } }

输入样例:

在这里给出一组输入。例如:

(无)

输出样例:

(Name:Zhang San; id:370202X; sid:1052102; score:96)
super(id, name);//super调用父类构造方法
this.sid = sid;
this.score = score;

6-7 设计一个Duck类及其子类

设计一个Duck类和它的两个子类RedheadDuck和MallardDuck。裁判测试程序中的Main类会自动提交。

类的定义:

//Duck类的定义
class Duck {    }

//RedheadDuck类的定义
class RedheadDuck extends Duck {  }

//MallardDuck类的定义
class MallardDuck extends Duck {   }

裁判测试程序样例:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scanner=new Scanner(System.in);
        Duck rduck = new RedheadDuck();
        rduck.display();
        rduck.quack();
        rduck.swim();
        rduck.fly();    
        Duck gduck = new MallardDuck();
        gduck.display();
        gduck.quack();
        gduck.swim();
        gduck.fly();         
    }
}
/* 请在这里填写答案 */

输入样例:

在这里给出一组输入。例如:

输出样例:

在这里给出相应的输出。例如:

我是一只红头鸭
我会呱呱呱
我会游泳
我会飞
我是一只绿头鸭
我会呱呱呱
我会游泳
我会飞
abstract class Duck//定义一个duck抽像类
{
    public void quack()
    {
        System.out.println("我会呱呱呱");
    }
    public void swim() {
        System.out.println("我会游泳");
    }
    public void fly() {
        System.out.println("我会飞");
    }
    abstract public void display();
}
class RedheadDuck extends Duck
{
    public void display()
    {
        System.out.println("我是一只红头鸭");
    }
}
class MallardDuck extends Duck 
{
    public void display() 
    {
        System.out.println("我是一只绿头鸭");
    }
}

6-8 八边形Octagan类(接口)

编写一个名为Octagon的类,表示八边形。假设八边形八条边的边长都相等。它的面积可以使用下面的公式计算:

     面积 = (2 + 4 / sqrt(2)) * 边长 * 边长

请实现Octago

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CRAEN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值