2023面向对象程序设计《练习题3》-面向对象

本篇练习题是在LTXR同学强烈的要求下更新的o v o!!!

T1.

裁判测试程序样例中展示的是一段定义基类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 String getId(){
	    return id;
	}
public String setId(String id){
	    return this.id=id;
	}

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

	public String getName(){
	    return name;
	}

	public People(){
		
	}
	public People(String id,String name) {
		this.id=id;
		this.name=name;
	}
	public void say(){
	    System.out.print("I'm a person! My name is " + this.name + ".");
	    }

T2.

在类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 obj) {
            if(this ==obj) {
                return true;
            }
           if(obj instanceof Student)
	     		Student p = (Student)obj;
				return this.id == p.id;
			}
				else return false;
            }

 T3.

定义一个形状类Shape,提供计算周长getPerimeter()和面积getArea()的函数
定义一个子类正方形类Square继承自Shape类,拥有边长属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
定义一个子类长方形类Rectangle继承自Square类,拥有长、宽属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()
定义一个子类圆形类Circle继承自Shape,拥有半径属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()

在main函数中,分别构造三个子类的对象,并输出他们的周长、面积.
提示:用System.out.printf("%.2f",d)进行格式化输出

输入描述:

正方形类的边长
长方形类的长宽
圆类的半径

输出描述:

正方形的周长、面积
长方形的周长、面积

裁判测试程序样例:

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
         Scanner scan = new Scanner(System.in);      
         double length = scan.nextDouble();
         Square s = new Square(length);
         System.out.printf("%.2f ",s.getPerimeter());
         System.out.printf("%.2f\n",s.getArea());

         length = scan.nextDouble();
         double wide = scan.nextDouble();
         Rectangle r = new Rectangle(length,wide);
         System.out.printf("%.2f ",r.getPerimeter());
         System.out.printf("%.2f\n",r.getArea());

         double radius = scan.nextDouble();
         Circle c = new Circle(radius);
         System.out.printf("%.2f ",c.getPerimeter());
         System.out.printf("%.2f\n",c.getArea());

         scan.close(); 
    }
}

/* 你的代码被嵌在这里 */

输入样例:

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

1
1 2
2

输出样例:

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

4.00 1.00
6.00 2.00
12.57 12.57

abstract class Shape{
	abstract double getPerimeter();
	abstract double getArea();
}

class Square extends Shape{
	
	double len;
	public Square(double len) {
		this.len=len;
	}
	
	public double getPerimeter() {
		return 4*this.len;
	}
	
	public double getArea() {
		return this.len*this.len;
	}
}

class Rectangle extends Square{
	double broad;
	public Rectangle(double len,double broad) {
		super(len);
		this.broad=broad;
	}
	
	@Override
	public double getPerimeter() {
		return 2*this.len+2*this.broad;
	}
	
	@Override
	public double getArea() {
		return this.len*this.broad;
	} 
	
}

class Circle extends Shape{
	double r;
	
	public Circle(double r) {
		this.r=r;
	}
	
	@Override
	public double getPerimeter() {
		return 2*Math.PI*r;
	}
	
	@Override
	public double getArea() {
		return Math.PI*this.r*this.r;
	} 
}

T4.

使用继承设计:教师类。
使程序运行结果为:

Li 40 信工院
教师的工作是教学。

函数接口定义:

定义类Teacher, 继承Person 

裁判测试程序样例:


class Person{
    String name;
    int age;
    Person(String name,int age){
        this.name = name;
        this.age = age;
    }
    void work(){
        
    }
    void show() {
        System.out.print(name+" "+age+" ");
    }
}

/* 请在这里填写答案 */

public class Main {

    public static void main(String[] args) {
    
         Teacher t = new Teacher("Li",40,"信工院");
         t.show();
         t.work();
    }
}

输入样例:

没有输入

输出样例:

Li 40 信工院
教师的工作是教学。

class Teacher extends Person{
    String place;
    public Teacher(String name,int age,String place){
        super(name,age);
        this.place=place;
    }void work(){
        System.out.println("教师的工作是教学。");
    }
    void show() {
        super.show();
        System.out.println(place);
    }
}

T5.

请从下列的抽象类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 {
	private double radius;
	public Circle(double r) {
		radius=r;
	}
	public double getArea() {
		return Math.PI*radius*radius;
	}
	public double getPerimeter() {
		return Math.PI*2*radius;
	}
}

T6.

创建一个直角三角形类(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{
	private double a,b;
	
	public RTriangle(double a, double b) {
		this.a = a;
		this.b = b;
	}
	
	public double getArea() {
		return 0.5*a*b;
	}
	
	public double getPerimeter() {
		return a+b+Math.sqrt(a*a+b*b);
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值