Java 编写一个矩形类实现shape接口。 编写一个圆类实现shape接口。 编写一个主类用来测 50
Java编写一个矩形类实现shape接口。编写一个圆类实现shape接口。编写一个主类用来测试上述各接口。...
Java
编写一个矩形类实现shape接口。
编写一个圆类实现shape接口。
编写一个主类用来测试上述各接口。 展开
编写一个矩形类实现shape接口。
编写一个圆类实现shape接口。
编写一个主类用来测试上述各接口。 展开
2个回答
2014-12-04
展开全部
/**
*
* @author Administrator
* 图形接口
*
*/
public interface shape {
// 得到面积
public double getArea (int radiusOrLength, int width);
// 得到周长
public double getPerimeter(int radiusOrLength, int width);
}
/**
*
* @author Administrator
* 圆形
*/
public class Circle implements shape{
// π定义
private final static double PI = 3.14;
@Override
public double getArea(int radius, int width) {
double area = Math.sqrt(radius)* PI;
return area;
}
@Override
public double getPerimeter(int radius, int width) {
double perimeter = 2*radius* PI;
return perimeter;
}
}
/**
* 长方形
* @author Administrator
*
*/
public class Rectangle implements shape{
@Override
public double getArea(int length, int width) {
double area = length*width;
return area;
}
@Override
public double getPerimeter(int length, int width) {
double perimeter = 2*(length + width);
return perimeter;
}
}
/**
* 正方形
* @author Administrator
*
*/
public class Square extends Rectangle {
public double getArea(int length, int width) {
return super.getArea(length, width);
}
}
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class Main {
private static int radius= 0;
private static int length = 0;
private static int width = 0;
private static int shape = 0;
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
InputStream in = System.in;
Scanner scan = new Scanner(in);
System.out.println("********************************************");
System.out.println("请输入图形及它们的半径或者长宽");
System.out.println("1表示圆");
System.out.println("2表示长方形");
System.out.println("3表示正方形");
System.out.println("********************************************");
System.out.print("请输入图形:");
shape = (int)scan.nextInt();
getShape(shape);
switch (shape) {
case 1 :
Circle circle = new Circle();
System.out.println("圆的面积:"+ circle.getArea(radius, 0) );
System.out.println("圆的周长:"+ circle.getPerimeter(radius, 0));
break;
case 2:
Rectangle rectangle = new Rectangle();
System.out.println("长方形的面积:"+ rectangle.getArea(length, width));
System.out.println("长方形的周长:" + rectangle.getPerimeter(length, width));
break;
case 3:
Square square = new Square();
System.out.println("正方形的面积:"+ square.getArea(length, length));
System.out.println("正方形的周长:" + square.getPerimeter(length, length) );
break;
default:
System.out.println("输入有误!" );
}
}
public static void getShape(int shape) {
InputStream in = System.in;
Scanner scan = new Scanner(in);
switch (shape) {
case 1 :
System.out.println("请输入的半径" );
radius = (int)scan.nextInt();
break;
case 2:
getLenWid();
break;
case 3:
System.out.println("请输入正方形的长" );
length = (int)scan.nextInt();
break;
default:
System.out.println("输入有误,请重新选择!" );
getShape(shape);
}
}
public static void getLenWid() {
InputStream in = System.in;
Scanner scan = new Scanner(in);
System.out.println("请输入长方形的长" );
length = (int)scan.nextInt();
System.out.println("请输入长方形的宽" );
width = (int)scan.nextInt();
if (length <= width) {
System.out.println("长方形的长度大于宽度!" );
getLenWid();
}
}
}
*
* @author Administrator
* 图形接口
*
*/
public interface shape {
// 得到面积
public double getArea (int radiusOrLength, int width);
// 得到周长
public double getPerimeter(int radiusOrLength, int width);
}
/**
*
* @author Administrator
* 圆形
*/
public class Circle implements shape{
// π定义
private final static double PI = 3.14;
@Override
public double getArea(int radius, int width) {
double area = Math.sqrt(radius)* PI;
return area;
}
@Override
public double getPerimeter(int radius, int width) {
double perimeter = 2*radius* PI;
return perimeter;
}
}
/**
* 长方形
* @author Administrator
*
*/
public class Rectangle implements shape{
@Override
public double getArea(int length, int width) {
double area = length*width;
return area;
}
@Override
public double getPerimeter(int length, int width) {
double perimeter = 2*(length + width);
return perimeter;
}
}
/**
* 正方形
* @author Administrator
*
*/
public class Square extends Rectangle {
public double getArea(int length, int width) {
return super.getArea(length, width);
}
}
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class Main {
private static int radius= 0;
private static int length = 0;
private static int width = 0;
private static int shape = 0;
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
InputStream in = System.in;
Scanner scan = new Scanner(in);
System.out.println("********************************************");
System.out.println("请输入图形及它们的半径或者长宽");
System.out.println("1表示圆");
System.out.println("2表示长方形");
System.out.println("3表示正方形");
System.out.println("********************************************");
System.out.print("请输入图形:");
shape = (int)scan.nextInt();
getShape(shape);
switch (shape) {
case 1 :
Circle circle = new Circle();
System.out.println("圆的面积:"+ circle.getArea(radius, 0) );
System.out.println("圆的周长:"+ circle.getPerimeter(radius, 0));
break;
case 2:
Rectangle rectangle = new Rectangle();
System.out.println("长方形的面积:"+ rectangle.getArea(length, width));
System.out.println("长方形的周长:" + rectangle.getPerimeter(length, width));
break;
case 3:
Square square = new Square();
System.out.println("正方形的面积:"+ square.getArea(length, length));
System.out.println("正方形的周长:" + square.getPerimeter(length, length) );
break;
default:
System.out.println("输入有误!" );
}
}
public static void getShape(int shape) {
InputStream in = System.in;
Scanner scan = new Scanner(in);
switch (shape) {
case 1 :
System.out.println("请输入的半径" );
radius = (int)scan.nextInt();
break;
case 2:
getLenWid();
break;
case 3:
System.out.println("请输入正方形的长" );
length = (int)scan.nextInt();
break;
default:
System.out.println("输入有误,请重新选择!" );
getShape(shape);
}
}
public static void getLenWid() {
InputStream in = System.in;
Scanner scan = new Scanner(in);
System.out.println("请输入长方形的长" );
length = (int)scan.nextInt();
System.out.println("请输入长方形的宽" );
width = (int)scan.nextInt();
if (length <= width) {
System.out.println("长方形的长度大于宽度!" );
getLenWid();
}
}
}
展开全部
鄙人在这里提供一个不同的解法,接口里方法不传参数
1.接口定义如下:
public interface Shape {
public double area();
public double length();
}
2.圆定义如下:
public class Circle implements Shape{
private final double Pi=3.141596;
public double r;
@Override
public double area() {
// TODO Auto-generated method stub
return r*r*Pi;
}
@Override
public double length() {
// TODO Auto-generated method stub
return 2*r*Pi;
}
}
3.矩形定义如下:
public class Juxing implements Shape{
public int len1;
public int len2;
@Override
public double area() {
// TODO Auto-generated method stub
return len1*len2;
}
@Override
public double length() {
// TODO Auto-generated method stub
return 2*(len1+len2);
}
}
4.测试类如下:
public class Test {
public static void main(String[] args){
Circle c= new Circle();
c.r=3;
System.out.println("\nThis is Circle(r=3) area="+c.area());
System.out.println("This is Circle(r=3) length="+c.length());
Juxing ju= new Juxing();
ju.len1=4;
ju.len2=5;
System.out.println("\nThis is juxing's area= "+ju.area());
System.out.println("This is Juxing's length= "+ju.length());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询