How to use the Java Triangle class that Huawei's tens of thousands of people want to know

This article mainly introduces the detailed usage of the java triangle class Triangle, which has a good reference value, and I hope it will be helpful to everyone. Let's follow the editor to take a look

Triangle class Triangle design a class
named Triangle to extend the GeometricObject class. This category includes:

1. Three double data fields named side1, side2, and side3 represent the three sides of this triangle, and their default value is 1.0.

2. A default triangle created by a parameterless construction method

3. A construction method that can create a triangle with specified side1, side2, and side3

4. Accessor methods for all three data fields

5. A method called getArea() returns the area of ​​this triangle

6. A method called getPerimeter() returns the perimeter of this triangle

7. A method named toString() returns the string description of this triangle

Problem analysis:
step by step, according to the requirements, from the data field, to the construction method (no parameters, with parameters), to the method, to the test class

Just learn to use inheritance and inheritance methods

Code demonstration (verified)
1. Class created:

// 创建的类
package java_testquestions;
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 filled() {
    
    
 return filled;
 }
 public void setFilled(boolean filled) {
    
    
 this.filled = filled;
 }
 public java.util.Date getDateCreated(){
    
    
 return dateCreated;
 }
 public String toString() {
    
    
 return "created on "+dateCreated +"\n color: "+color + " and filled: "+filled;
 }
}
public class Triangle extends GeometricObject
{
    
    
 private double side1;
 private double side2;
 private double side3;
 String color;
  
 Triangle(){
    
    
 side1 = 1.0;
 side2 = 1.0;
 side3 = 1.0;
 }
 public Triangle(double side1,double side2,double side3) {
    
    
 this.side1 = side1;
 this.side2 = side2;
 this.side3 = side3;
 }
 public double getSide1() {
    
    
 return side1;
 }
 public double getSide2() {
    
    
 return side2;
 }
 public double getSide3() {
    
    
 return side3;
 }
// 父类中已有该方法
 public void setColor(String Color) {
    
    
 color = Color;
 }
 public String getColor() {
    
    
 return color;
 }
 public double getArea() {
    
    
 double s = (side1+side2+side3)*1.0/2;
 double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
 return area;
 }
  
 public double getPerimeter() {
    
    
 return side1+side2+side3;
 }
 public String toString() {
    
    
 return "Triangle: side1 = "+side1 +"side2 = "+side2 +" side3 = "+side3;
 }
}

2. Test class, Triangle_Test

// 测试类
package java_testquestions;
import java.util.Scanner;
public class Triangle_Test 
{
    
    
 public static void main(String[] args)
 {
    
    
 Scanner input = new Scanner(System.in);
  
 System.out.println("请输入三角形的三条边:"); // 需要用字符串类型输入,这样在下面 输入颜色时就不会报错
 String side11 = input.nextLine();
 double side1 = Double.parseDouble(side11);
 String side22 = input.nextLine();
 double side2 = Double.parseDouble(side22);
 String side33 = input.nextLine();
 double side3 = Double.parseDouble(side33);
// double side1 = input.nextDouble();
// double side2 = input.nextDouble();
// double side3 = input.nextDouble();
 Triangle triangle = new Triangle(side1,side2,side3);
  
 System.out.println("请输入三角形的颜色: ");
 String color = input.nextLine();
 triangle.setColor(color);
 System.out.println("\n");
  
 System.out.println("请输入是否填充 true or false:");
 boolean filled = input.nextBoolean();
 triangle.setFilled(filled);
  
 System.out.println("三角形的面积为 :"+triangle.getArea());
 System.out.println("三角形的周长为 :"+triangle.getPerimeter());
 System.out.println("三角形的颜色为 :"+triangle.getColor());
 System.out.println("三角形是否填充 :"+triangle.filled());
 }
}

Bubble:

It should be fine to be careful when creating the class. There is a detail in the test class that needs attention: when inputting the side length of the double type, first use the string type input, and then convert it to the double type data, the conversion method is as follows:

String side11 = input.nextLine();
double side1 = Double.parseDouble(side11);
String side22 = input.nextLine();
double side2 = Double.parseDouble(side22);
String side33 = input.nextLine();
double side3 = Double.parseDouble(side33);

Well, ok, call it a day.

Supplement: Use java to write triangle judgment program

I won't talk too much nonsense, everyone should look at the code directly~

public static void main(String[] args) {
    
    
// TODO 自动生成的方法存根
int a,b,c;
System.out.println(“请输入三角形的三边”);
@SuppressWarnings(“resource”)
Scanner sc=new Scanner(System.in);
System.out.println(“a=);
a=sc.nextInt();
System.out.println(“b=);
b=sc.nextInt();
System.out.println(“c=);
c=sc.nextInt();
if((a+b)>c&&(b+c)>a&&(c+a)>b)
System.out.println(a+,+b+,+c+”能构成三角形!”);
else
System.out.println(a+,+b+,+c+”不能构成三角形!”);
}

The results of the operation are as follows
Insert picture description here

Summary of the program:
Sometimes, we need to perform some operations when certain conditions are met, and do not perform any operations when the conditions are not met. At this time, we can only use the if statement. In other words, if else does not have to appear at the same time.
The above is personal experience, I hope to give you a reference, and I hope you can support the editor. If there are mistakes or not fully considered, please feel free to enlighten me.

Guess you like

Origin blog.csdn.net/dcj19980805/article/details/115304641