JAVA简单代码
本文最后更新于 3808 天前,其中的信息可能已经有所发展或是发生改变。

第二单元任务五实训:通过程序设计几何图形接口(Shape),实现类矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型,能够利用接口和多态性计算几何图形的面积和周长并显示。

//接口

    

public interface Shape {
	public double perimeter();//周长
	public double area();//面积
}
	//矩形
	class Rectangle implements Shape{
	double a ;	 //长
	double b ;	 //宽
	public Rectangle(double a,double b){
		this.a=a;
		this.b=b;
	}
	public double perimeter(){
		return 2*(a+b);
	}
	public double area(){
		return a*b;
	}
	}
	//圆形
		class Circle implements Shape {
		public static final double PI=3.14;
		double r;	 //半径
		public Circle(double r){
			this.r=r;
		}
		public double perimeter(){
			return 2*PI*r;
		}
		public double area(){
			return PI*r*r;
		}

	}
	//正方形
		class Square implements Shape {
		double a ;	//边长
		public Square(double a){
			this.a=a;
		}
		public double perimeter(){
			return 4*a;
		}
		public double area(){
			return a*a;
		}

		}

//测试

    

import java.util.*;
public class ShapeTest {

	/**
	 * @param args
	 */
	/*
	 * 获得类型的信息

	在获得类型类之后,你就可以调用其中的一些方法获得类型的信息了,主要的方法有:

	getName():String:获得该类型的全称名称。

	getSuperClass():Class:获得该类型的直接父类,如果该类型没有直接父类,那么返回null。

	getInterfaces():Class[]:获得该类型实现的所有接口。

	isArray():boolean:判断该类型是否是数组。

	isEnum():boolean:判断该类型是否是枚举类型。

	isInterface():boolean:判断该类型是否是接口。

	isPrimitive():boolean:判断该类型是否是基本类型,即是否是int,boolean,double等等。

	isAssignableFrom(Class cls):boolean:判断这个类型是否是类型cls的父(祖先)类或父(祖先)接口。

	getComponentType():Class:如果该类型是一个数组,那么返回该数组的组件类型。

	*/
	  public static void print(Shape shape){	//在书本p86那里有说到。
          System.out.println(shape.getClass()+"面积:"+shape.area());		//利用getClass()的方法能
          System.out.println(shape.getClass()+"周长:"+shape.perimeter());
   }
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("***********查水表**************");
		System.out.println("请输入你需要计算有哪些集合图形面积。");
		System.out.println("回复1为矩形,2为圆形,3为正方形");
		int a=input.nextInt();
		if(a==1){
			System.out.println("请输入矩形的长:");
			int chang=input.nextInt();	//输入长
			System.out.println("请输入矩形的宽:");
			int kuan=input.nextInt();	//输入宽
			Shape shape=new Rectangle(chang,kuan);	//已经在接口中定义了a为长,a==chang,b==kuan
			print(shape);	//输出表达
		}else if(a==2){
			System.out.println("请输入圆形的半径:");
			int r=input.nextInt();	//输入圆的半径
			Shape shape=new Circle(r);	//同上,已经在接口中定义了
	        print(shape);
		}else if(a==3){
			System.out.println("请输入正方形的边长:");
			int chang1=input.nextInt();	//输入正方形边长
			Shape shape=new Square(chang1);	//同上,已经在接口中定义了
	        print(shape);
		}else{
			System.out.println("您输入错误!请返回重新输入");
		}
		 System.out.println("查水表结束");
        System.out.println("***********查水表**************");
	}

}

下面是三角形和长方形的周长和面积的计算代码。

public interface Shape {
  public double area();//面积
  public double longer();//周长
}
//三角形
public class Triangle implements Shape {
  double s1;
  double s2;
  double s3;
  // 初始化一个三角形对象,并赋予该三角形三边长
  public Triangle(double s1, double s2, double s3) {
    if (isTri(s1, s2, s3)) {
      this.s1 = s1;
      this.s2 = s2;
      this.s3 = s3;
    } else {
      System.out.println("输入的三边长" + s1 + "、" + s2 + "、" + s3
      + "不能组成一个三角形,请重新输入三边长!");
    }
  }
  // 判断是否是个三角形
  public boolean isTri(double s1, double s2, double s3) {
    if (s1 + s2 < s3) {
      return false;
    }
    if (s1 + s3 < s2) {
      return false;
    }
    if (s2 + s3 < s1) {
      return false;
    }
    return true;
  }
 //面积
  public double area() {
    double p = (s1 + s2 + s3) / 2;
    return Math.sqrt(p * (p - s1) * (p - s2) * (p - s3));
  }
//周长
  public double longer() {
    return s1 + s2 + s3;
  }
}
//长方形
public class Director implements Shape {
  double s1;
  double s2;
  // 初始化一个长方形,并赋予该长方形两边长
  public Director(double s1, double s2) {
    this.s1 = s1;
    this.s2 = s2;
  }
//面积
  public double area() {
    return s1 * s2;
  }
//周长
  public double longer() {
    // TODO Auto-generated method stub
    return 2 * (s1 + s2);
  }
}
//测试类
public class Test {
  /**
   * @param args
   */
  public static void main(String[] args) {
    Shape triangle = new Triangle(3, 4, 8);
    // 新建一个三边长为3,4,5的三角形
    Shape tri = new Triangle(3, 4, 5);
    Shape director = new Director(10, 20);
    // 新建一个两边长为10,20的长方形
    System.out.println("三角形triangle的周长为:" + triangle.longer());
    System.out.println("三角形triangle的面积为:" + triangle.area());
    System.out.println("三角形tri的周长为:" + tri.longer());
    System.out.println("三角形tri的面积为:" + tri.area());
    System.out.println("该长方形的周长为:" + director.longer());
    System.out.println("该长方形的面积为:" + director.area());
  }
}

评论

  1. Image
    2028 年前
    -0001-11-30 0:00:00

    沙发!

  2. Image
    Windows Firefox 57.0
    9 年前
    2018-1-10 18:14:38

    666666厉害厉害

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇