Java按照下列要求完成接口的设计 (1)定义一个public接口Shapes,其包名为oop.base;(2)定义返回值为double类型并且无输入参数的public抽象方法,其中方法名为get

本文设计了一个形状接口Shapes及其实现类Square,Square类通过构造函数初始化宽度和高度,并实现了计算面积和周长的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 按照下列要求完成接口的设计。

(1)定义一个public接口Shapes,其包名为oop.base;

(2)定义返回值为double类型并且无输入参数的public抽象方法,其中方法名为getArea;

(3)定义返回值为double类型并且无输入参数的public抽象方法,其中方法名为getPerimeter。

2. 按照下列要求完成类实现接口的设计。

(1)定义一个public类Square,其包名为oop.base,且该类实现了第1题中的接口Shapes;

(2)在类Square中定义两个double类型的public成员变量,其中成员变量名为:width,height;

(3)用带有两个形式参数的public构造方法对Square类中的成员变量进行初始化,其中形式参数名为:width,height;

(4)在Square类中实现getArea方法,返回面积,width* height;

(5)在Square类中实现getPerimeter方法,返回周长,2*(width+height)。

3. 在第1题和第2题的基础之上,写出下列程序的执行结果,并用程序验证。

package oop.base.test;

import oop.base.Square;

public class SquareTest {

  public static void main(String[] args) {

   Square squ = new Square(5, 15);

   double area = squ.getArea();

   double peri = squ. getPerimeter();

   System.out.print("area:" + area + "  peri:" + peri);

   }

}

源代码:

package haut.oop.exp2.exten;
import haut.oop.exp2.abstr.Square;
import java.util.*;
interface Shape{            //接口
	double getArea();
	double getCircumference();
}
 abstract class Rectangle implements Shape{
	public double width,height;
   
	public Rectangle(double width,double height){
		this.width = width;
        this.height = height;
	}
	
		
	public double getArea(){
		double area = width * height;
		return area;
	}
	public double getPerimeter(){
		return 2*(width + height);
	}
	
	
    public static void main(String[] args) {

    Square squ = new Square(5, 15);

     double area = squ.getArea();

     double peri = squ. getPerimeter();

     System.out.print("area:" + area + "  peri:" + peri);

  }}
    
	
	

截屏效果:


    
    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值