Java第六次实验

1)修改实验五中的Triangle类,使其继承自GeometricObject并实现Comparable接口。覆盖Object类中的equals方法,当两个三角形的面积相等时,则两个三角形是相同的。

public class GeometricObject {
	private String color = "White";
	private boolean filled;
	private java.util.Date dateCreated;
	public GeometricObject() {
		dateCreated = new java.util.Date();
	}
	public GeometricObject(String color,boolean filled) {
		dateCreated = new java.util.Date();
		this.color = color;
		this.filled = filled;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public boolean isFilled() {
		return filled;
	}
	public void setFilled(boolean filled) {
		this.filled = filled;
	}
	public java.util.Date getDateCreated(){
		return dateCreated;
	}
	public String toString() {
		return "created on " + dateCreated + "\ncolor:" + color + " and filled:" + filled;
	}

}
public class Triangle extends GeometricObject implements Comparable<Triangle>{
	private double side1;
	private double side2;
	private double side3;
	Triangle(){
		
	}
	Triangle(double s1,double s2,double s3){
		side1 = s1;
		side2 = s2;
		side3 = s3;
	}
	public double getSide1() {
		return side1;
	}
	public void setSide1(double s1) {
		side1 = s1;
	}
	public double getSide2() {
		return side2;
	}
	public void setSide2(double s2) {
		side2 = s2;
	}
	public double getSide3() {
		return side3;
	}
	public void setSide3(double s3) {
		side3 = s3;
	}
	public double getArea() {
		double a = (side1+side2+side3)/2;
		return Math.sqrt(a*(a-side1)*(a-side2)*(a-side3));
	}
	public double gerPerimeter() {
		return side1+side2+side3;
	}
	public String toString() {
		return "Creat on "+super.getDateCreated()+"\ncolor: "+super.getColor()+"\nSide1:"+this.side1+"\nSide2:"+
	this.side2+"\nSide3:"+this.side3+"\nArea:"+this.getArea()+"\nPerimeter:" + this.gerPerimeter();
	}
	public boolean equal(Triangle A) {
		return this.getArea() == A.getArea();
	}
	@Override
	public int compareTo(Triangle A) {
		if(this.getArea()>A.getArea())
			return 1;
		else if(this.getArea()<A.getArea())
			return -1;
		else
			return 0;
	}
}
public class TriangleTest {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Triangle t1 = new Triangle(3,4,5);
		Triangle t2 = new Triangle(5,12,13);
		System.out.println(t1.toString());
		System.out.println(t2.toString());
		System.out.println(t1.equal(t2));
		System.out.println(t1.compareTo(t2));
	}

}

2)编写一个实现Sortable接口的类PointSorter,该类能够完成对坐标点的排序(按照坐标点到原点(0,0)的距离从大到小排列)。

public class Point {
	double x,y;
	double distance;
	Point(){
	
	}
	Point(double x,double y){
		this.x = x;
		this.y = y;
		this.distance = Math.sqrt(x*x+y*y);
	}
}
class Sort {
	Sortable S;
	Sort(){
		
	}
	Sort(Sortable S){
		this.S = S;
	}
	public Point[] sort(Point[] P) {
		for(int i = 0;i<P.length-1;i++)
			for(int j = 0;j<P.length-1;j++) {
				int temp = S.sort(P[i], P[j]);
				if(temp >0) {
					Point q = P[i];
					P[i] = P[j];
					P[j] = q;
				}
			}
		return P;
		
	}
	
}
class PointSorter implements Sortable{

	@Override
	public int sort(Object A, Object B) {
			Point p1 = (Point)A;
			Point p2 = (Point)B;
			if(p1.distance>p2.distance)
				return 1;
			return -1;
	}
	
}
public interface Sortable {
	public int sort(Object A,Object B);

}
public class PointTest {

	public static void main(String[] args) {
                Point p1 = new Point(4,2);
		Point p2 = new Point(1,3);
		Point p3 = new Point(1,4);
		Point p4 = new Point(5,5);
		Point p5 = new Point(1,1);
		Point[] p = {p1,p2,p3,p4,p5};
		PointSorter pointsorter = new PointSorter();
		Sort sort = new Sort(pointsorter);
		Point[] newp = sort.sort(p);
		for(int i = 0;i<5;i++) {
			System.out.println(newp[i].x +" "+newp[i].y+" "+newp[i].distance);
		}
	}

}

(3)根据以下类图,采用桥接模式编程验证面向对象的“开-闭”原则。

public class GreenTea implements TeaKind{
	private double price;
	GreenTea(){
		
	}
	GreenTea(double price){
		this.price = price;
	}
	@Override
	public double getPrice() {
		
		return price;
	}
}
public class RedTea implements TeaKind{
	private double price;
	RedTea(){
		
	}
	RedTea(double price){
		this.price = price;
	}
	@Override
	public double getPrice() {
		return price;
	}

}
public class BlackTea implements TeaKind{
	private double price;
	BlackTea(){
		
	}
	BlackTea(double price){
		this.price = price;
	}
	@Override
	public double getPrice() {
		// TODO 自动生成的方法存根
		return price;
	}

}
public class MediumCup implements TeaSize{
	public TeaKind teakind;
	MediumCup(TeaKind teakind){
		this.teakind = teakind;
	}
	@Override
	public double getprice() {
		return 1.5*teakind.getPrice();
	}

}
public class SuperCup implements TeaSize{
	public TeaKind teakind;
	SuperCup(TeaKind teakind){
		this.teakind = teakind;
	}
	@Override
	public double getprice() {
		return 2.5*teakind.getPrice();
	}

}
public class SmallCup implements TeaSize{
	public TeaKind teakind;
	SmallCup(TeaKind teakind){
		this.teakind = teakind;
	}
	@Override
	public double getprice() {
		return teakind.getPrice();
	}

}
public interface TeaSize {
	public double getprice();
}
public interface TeaKind {
	public double getPrice();
}
import java.util.Scanner;

public class TeaTest {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
			RedTea redtea = new RedTea(2);
			GreenTea greentea = new GreenTea(3);
			BlackTea blacktea = new BlackTea(4);
			System.out.println("请选择您要的饮料:");
			System.out.println("1.红茶	2.绿茶	3.黑茶");
			Scanner input = new Scanner(System.in);
			int number1 = input.nextInt();
			System.out.println("请选择您要的规格:");
			System.out.println("1.小杯	2.中杯	3.大杯");
			int number2 = input.nextInt();
			switch(number1) {
				case 1:
					switch(number2) {
						case 1:System.out.print(new SmallCup(redtea).getprice());break;
						case 2:System.out.print(new MediumCup(redtea).getprice());break;
						case 3:System.out.print(new SuperCup(redtea).getprice());break;

					};break;
				case 2:
					switch(number2) {
						case 1:System.out.print(new SmallCup(greentea).getprice());break;
						case 2:System.out.print(new MediumCup(greentea).getprice());break;
						case 3:System.out.print(new SuperCup(greentea).getprice());break;

					};break;
				case 3:
					switch(number2) {
						case 1:System.out.print(new SmallCup(blacktea).getprice());break;
						case 2:System.out.print(new MediumCup(blacktea).getprice());break;
						case 3:System.out.print(new SuperCup(blacktea).getprice());break;
					};break;
			}
			input.close();
	}
}


猜你喜欢

转载自blog.csdn.net/qq_37301850/article/details/80037468