同步课 / 2014级JavaSE

创建圆形、三角形、方形 三个形状类,具有高宽等属性和能够计算周长、面积的成员方法
10
王伟
开始于 2016-03-16 13:23
0 28 162
已截止

任务尚未发布或者你没有权限查看任务内容。

任务讨论
package com.edu.www;
/**
 * 作业说明:创建圆形、三角形、方形 三个形状类,具有高宽等属性和能够计算周长、面积的成员方法
 * 作者:闫中宇
 * 班级:一班
 * 学号:2014011546
 */
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
			Circle c = new Circle(5);
			Rectangle r=new Rectangle(1,2);
			Triangle t = new Triangle(2,2,3);
			
			c.display();
			r.display();
			t.display();
		
	}

}
方类
package com.edu.www;

public class Rectangle {

	private double Length;//长
	private double Width;//宽
	private double area;//面积
	private double perimeter;//周长
	
	public Rectangle(double Length, double Width) {
		if(Width <= 0 && Length <= 0){
			System.out.println("方形的边不能为负数!");
			
		}else{
			this.Length = Length;
			this.Width = Width;
			this.area = Length * Width;
			this.perimeter = 2*(Width + Length);
		}
	}
	public Rectangle(int length2, int width2) {
		// TODO Auto-generated constructor stub
	}
	public double getLength() {
		return Length;
	}
	
	public double getWidth() {
		return Width;
	}
	
	public double getArea() {
		return area;
	}
	
	public double getPerimeter() {
		return perimeter;
	}
	
	public void display(){
		System.out.println("方形的面积:"+getArea());
		
		System.out.println("方形的周长:"+getPerimeter());
	}

}
三角类
package com.edu.www;

import java.text.DecimalFormat;

public class Triangle {

	private double a;
	private double b;
	private double c;
	private double area;
	private double perimeter;
	private DecimalFormat df = new DecimalFormat("0.00");
	public Triangle(double a, double b, double c) {
		double p;
		if(judgeSetup(a,b,c)){
			this.a = a;
			this.b = b;
			this.c = c;
			p = (a+b+c)/2;
			
			this.area = Math.sqrt( (p*(p-a)*(p-b)*(p-c)));
			this.perimeter = a+b+c;
			
		}
		else if(a<0 || b<0 || c<0){
			System.out.println("三角形任意一边不能为负数!");
		}
		else{
			System.out.println("三角形三边无法构成三角形!");
		}		
	}
	
	public boolean judgeSetup(double a,double b,double c){
		double max,min;
		max = (a>b)?a:b;
		max = (max>c)?max:c;
		min = (a<b)?a:b;
		min = (min<c)?min:c;
		if( (max-min) < (a+b+c-max-min)&&(a+b+c-max) > max  ){
			return true;
		}
		else{
			return false;
		}
	}
	public double getA() {
		return a;
	}

	public double getB() {
		return b;
	}

	public double getC() {
		return c;
	}
	
	public double getArea() {
		return area;
	}
	public double getPerimeter() {
		return perimeter;
	}
	public void display(){
		System.out.println("三角形的面积:"+df.format( getArea() ));
		System.out.println("三角形的周长:"+getPerimeter());
	}
}
圆类
package com.edu.www;

import java.text.DecimalFormat;

public class Circle {
	private double radius;
	private double area;
	private double perimeter;
	private DecimalFormat df = new DecimalFormat("0.00");
	public Circle(double radius) {
		if(radius<=0){
			System.out.println("圆的半径不可为负数");
		}else{
			this.radius = radius;
			this.area = this.radius * this.radius * 3.14;
			this.perimeter = 2 * 3.14 *radius;
		}
	}
	
	public double getRadius() {
		return radius;
	}
	
	public void setRadius(double a) {
		this.radius = a;
	}
	
	public double getArea() {
		return area;
	}
	
	public double getPerimeter() {
		return perimeter;
	}
	
	public void display(){
		System.out.println("圆的面积是:"+getArea());
		System.out.println("园的周长是:"+df.format( getPerimeter() ));
	}
}
/**
 * 作业说明: 创建圆形、三角形、方形 三个形状类,具有高宽等属性和能够计算周长、面积的成员方法
 * 姓名:王晓旺
 * 学号:2014011739
 * 班级:5班
 */
//Circle.java
package com.works1.www;

public class Circle {
	private double r;
	final double pi = 3.14;

	public Circle(double r) {
		// TODO Auto-generated constructor stub
		if (r < 0) {
			System.out.println("圆的r不能为负数");
		} else {
			this.r = r;
		}
	}

	public double getPer() {
		double L;
		L = 2 * pi * r;
		return L;
	}

	public double getArea() {
		double S;
		S = pi * r * r;
		return S;
	}

}
// Triangle.java
package com.works1.www;

public class Triangle {
	private double a, b, c;

	public Triangle(double a, double b, double c) {
		// TODO Auto-generated constructor stub
		if (a < 0 || b < 0 || c < 0 || a + b <= c || a + c <= b || b + c <= a) {
			System.out.println("输入的数值不满足构成三角形的要求!");
		} else {
			this.a = a;
			this.b = b;
			this.c = c;
		}
	}

	public double getPer() {
		double L;
		L = a + b + c;
		return L;
	}

	public double getArea() {
		double p, s;
		p = (a + b + c) / 2;
		s = Math.sqrt(p * (p - a) * (p - b) * (p - c));
		return s;
	}
}
//Rectangle.java
package com.works1.www;

public class Rectangle {
	private double a, b;

	public Rectangle(double a, double b) {
		// TODO Auto-generated constructor stub
		if (a < 0 || b < 0) {
			System.out.println("输入的条件不符合题意!");
		} else {
			this.a = a;
			this.b = b;
		}
	}

	public double getPer() {
		double L;
		L = (a + b) * 2;
		return L;
	}

	public double getArea() {
		double S;
		S = (a * b);
		return S;
	}
}
//Test.java
package com.works1.www;

public class Test {
	public static void main(String[] args) {
		// TODO Auto-generated constructor stub
		Circle X = new Circle(8);
		Triangle Y = new Triangle(3, 4, 5);
		Rectangle Z = new Rectangle(6, 7);
		System.out.println("圆的周长:" + X.getPer());
		System.out.println("圆的面积:" + X.getArea());
		System.out.println("三角形的周长:" + Y.getPer());
		System.out.println("三角形的面积:" + Y.getArea());
		System.out.println("方形的面积:" + Z.getPer());
		System.out.println("方形的面积:" + Z.getArea());
	}
}
/**
 * 作业说明: 创建圆形、三角形、方形 三个形状类,具有高宽等属性和能够计算周长、面积的成员方法
 * 姓名:王晓旺
 * 学号:2014011739
 * 班级:5班
 */
//Circle.java
package com.works1.www;

public class Circle {
	private double r;
	final double pi = 3.14;

	public Circle(double r) {
		// TODO Auto-generated constructor stub
		if (r < 0) {
			System.out.println("圆的r不能为负数");
		} else {
			this.r = r;
		}
	}

	public double getPer() {
		double L;
		L = 2 * pi * r;
		return L;
	}

	public double getArea() {
		double S;
		S = pi * r * r;
		return S;
	}

}
// Triangle.java
package com.works1.www;

public class Triangle {
	private double a, b, c;

	public Triangle(double a, double b, double c) {
		// TODO Auto-generated constructor stub
		if (a < 0 || b < 0 || c < 0 || a + b <= c || a + c <= b || b + c <= a) {
			System.out.println("输入的数值不满足构成三角形的要求!");
		} else {
			this.a = a;
			this.b = b;
			this.c = c;
		}
	}

	public double getPer() {
		double L;
		L = a + b + c;
		return L;
	}

	public double getArea() {
		double p, s;
		p = (a + b + c) / 2;
		s = Math.sqrt(p * (p - a) * (p - b) * (p - c));
		return s;
	}
}
//Rectangle.java
package com.works1.www;

public class Rectangle {
	private double a, b;

	public Rectangle(double a, double b) {
		// TODO Auto-generated constructor stub
		if (a < 0 || b < 0) {
			System.out.println("输入的条件不符合题意!");
		} else {
			this.a = a;
			this.b = b;
		}
	}

	public double getPer() {
		double L;
		L = (a + b) * 2;
		return L;
	}

	public double getArea() {
		double S;
		S = (a * b);
		return S;
	}
}
//Test.java
package com.works1.www;

public class Test {
	public static void main(String[] args) {
		// TODO Auto-generated constructor stub
		Circle X = new Circle(8);
		Triangle Y = new Triangle(3, 4, 5);
		Rectangle Z = new Rectangle(6, 7);
		System.out.println("圆的周长:" + X.getPer());
		System.out.println("圆的面积:" + X.getArea());
		System.out.println("三角形的周长:" + Y.getPer());
		System.out.println("三角形的面积:" + Y.getArea());
		System.out.println("方形的面积:" + Z.getPer());
		System.out.println("方形的面积:" + Z.getArea());
	}
}