The sixth experiment in Java

( 1) Modify the Triangle class in Experiment 5 so that it inherits from GeometricObject and implements the Comparable interface. Override the equals method in the Object class. When the areas of the two triangles are equal, the two triangles are the same.

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 auto-generated method stub
		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) Write a class PointSorter that implements the Sortable interface, which can complete the sorting of the coordinate points (arranged according to the distance from the coordinate point to the origin (0, 0) from large to small).

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) According to the following class diagram, the bridge mode programming is used to verify the object-oriented "open-closed" principle.

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 auto-generated method stub
		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 auto-generated method stub
			RedTea redtea = new RedTea(2);
			GreenTea greentea = new GreenTea(3);
			BlackTea blacktea = new BlackTea(4);
			System.out.println("Please choose the drink you want:");
			System.out.println("1. Black tea 2. Green tea 3. Dark tea");
			Scanner input = new Scanner(System.in);
			int number1 = input.nextInt();
			System.out.println("Please select the specification you want:");
			System.out.println("1. Small cup 2. Medium cup 3. Large cup");
			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();
	}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324770921&siteId=291194637