Using Java implementation to find perimeters of different graphics

Exercise 2: Finding Perimeters of Different Graphics

A project description

Training purposes: Use interfaces to achieve polymorphism.
Training requirements: Design a graphical interface, which contains an abstract method for calculating the perimeter, and implement the methods in the interface by defining different graphics such as circles, rectangles, triangles, trapezoids, etc.The perimeters of different graphics are calculated by referencing subclass objects with interface class objects.

Class II Design


The length of each edge length is passed in the construction method of each class, and the perimeter() method calculates the perimeter of each graphic, that is, the sum of each edge length.Attributes in a class are the perimeters of each perimeter.

Three Project Implementation Design

Define a Graph interface, write an abstract method for perimeter calculation in the interface, then define classes to implement the Graph interface, implement abstract methods in the interface in the class, and create a construction method to receive the edge lengths of graphics, then create instantiated objects of these classes in the test class Test01, pass in parameters, call their construction methods, and reuse pairs ofThe image name.The method name calls the perimeter() method of these classes and outputs the value of the perimeter of the result.
First, the scanner import java.util.Scanner is introduced before the test class is declared; then a scanner Scanner input=new Scanner(System.in) is created in the main method; the function switch(value) that the program needs to execute is determined by the data the scanner enters from the keyboard.
If it is 1, the perimeter of the circle is calculated and the radius of the circle is entered.The output is then invoked through the object.
If 2, ask for the perimeter of the rectangle, and enter the length and width of the rectangle.The output is then invoked through the object.
If it is 3, the perimeter of the trapezoid is calculated, and the lengths of each side of the trapezoid are entered.The output is then invoked through the object.
If 4, the perimeter of the triangle is calculated, and the bottom and sides of the triangle are entered.The output is then invoked through the object.

Four Runs and Tests





Five Analysis and Summary

Seeking the perimeter of graphics we have written in Java training before, it seems nothing to say, simply define an interface, then write the implementation class of the interface, implement abstract methods in the implementation class, and then call the method of the perimeter of the graphics you need in the test class, pass in the specific parameter values.It is important to note that most edge lengths are floating point numbers, so when setting the data type, set the data type of edge lengths to floating point type!

package experiment.two;
//Finding the Perimeter of a Circle
public class Circular implements Graph{
	public double r;
	public Circular(double r) {
		this.r=r;
	}
	@Override
	public void perimeter() {
		// TODO Auto-generated method stub
		System.out.println("The circumference of a circle is:"+Math.PI*r*2);
	}
	
}
package experiment.two;
public interface Graph {
	void perimeter();
}
package experiment.two;
//Find the perimeter of a rectangle
public class Rectangle implements Graph{
	public double length;
	public double wide;
	public Rectangle(double length,double wide) {
		this.length=length;
		this.wide=wide;
	}
	@Override
	public void perimeter() {
		// TODO Auto-generated method stub
		System.out.println("The perimeter of the rectangle is:"+(length+wide)*2);
	}
}
package experiment.two;
//Finding the Perimeter of Trapezoid
public class Trapezoid implements Graph{
	public double rightLength;
	public double leftLength;
	public double bottom;
	public double lowbottom;
	public Trapezoid(double rightLength,double bottom,double lowbottom, double leftLength) {
		this.bottom=bottom;
		this.rightLength=rightLength;
		this.leftLength=leftLength;
		this.lowbottom=lowbottom;
		
	}
	@Override
	public void perimeter() {
		// TODO Auto-generated method stub
		System.out.println("The perimeter of the trapezoid is:"+(bottom+rightLength+leftLength+lowbottom));
	}
}
package experiment.two;
//Finding the perimeter of a triangle
public class Triangle implements Graph{
	public double rightLength;
	public double leftLength;
	public double bottom;
	public Triangle(double rightLength,double leftLength,double bottom) {
		this.bottom=bottom;
		this.rightLength=rightLength;
		this.leftLength=leftLength;
	}
	@Override
	public void perimeter() {
		// TODO Auto-generated method stub
		System.out.println("The perimeter of a triangle is:"+(bottom+rightLength+leftLength));
	}
}
package experiment.two;
import java.util.Scanner;
public class Test01 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		@SuppressWarnings("resource")
		Scanner input=new Scanner(System.in);
		int key;
		System.out.println("Please enter the graphic of the perimeter you want:\n1.Finding the Perimeter of a Circle\n2.Find the perimeter of a rectangle\n3.Finding the Perimeter of Trapezoid\n4.Finding the perimeter of a triangle");
		key=input.nextInt();
		switch(key) {
		   case 1:
			 System.out.print("Please enter the radius of the circle:");
			 double r=input.nextDouble();
			 Graph circular=new Circular(r);
			 circular.perimeter();//Finding the Perimeter of a Circle
			 break;
		   case 2:
			   System.out.print("Please enter a rectangular rectangle:");
			   double length=input.nextDouble();
			   System.out.print("Please enter the width of the rectangle:");
			   double wide=input.nextDouble();
			   Graph rectangle=new Rectangle(length,wide);
			   rectangle.perimeter();//Find the perimeter of a rectangle
			   break;
		   case 3:  
			   System.out.print("Please enter the top and bottom of the trapezoid:");
			   double upBottom=input.nextDouble();
			   System.out.print("Please enter the bottom of the trapezoid:");
			   double downBottom=input.nextDouble();
			   System.out.print("Please enter the left side of the trapezoid:");
			   double leftSide=input.nextDouble();
			   System.out.print("Please enter the right side of the trapezoid:");
			   double rightSide=input.nextDouble();
			   Graph trapezoid=new Trapezoid(upBottom,downBottom,leftSide,rightSide);
			   trapezoid.perimeter();//Finding the Perimeter of Trapezoid
			   break;
		   case 4:
			   System.out.print("Please enter the low of the triangle:");
			   double bottom=input.nextDouble();
			   System.out.print("Please enter the left side of the triangle:");
			   double leftSideOne=input.nextDouble();
			   System.out.print("Please enter the right side of the triangle:");
			   double rightSideOne=input.nextDouble();
			   Graph triangle=new Triangle(bottom,leftSideOne,rightSideOne);
			   triangle.perimeter();//Finding the perimeter of a triangle
			   break;
		}
	}
}
77 original articles published, 3 praised, 1500 visits
Private letter follow

Tags: Java

Posted on Thu, 13 Feb 2020 18:37:30 -0800 by kkessler