java 三角形類 Triangle的用法詳解
三角形類Triangle
設(shè)計一個名為 Triangle 的類來擴展 GeometricObject 類。該類包括:
1、三個名為 side1,side2,side3 的double 數(shù)據(jù)域表示這個三角形的三條邊,它們的默認(rèn)值是1.0.
2、一個無參構(gòu)造方法創(chuàng)建的默認(rèn)的三角形
3、一個能創(chuàng)建帶指定side1,side2,side3的三角形的構(gòu)造方法
4、所有三個數(shù)據(jù)域的訪問器方法
5、一個名為getArea()的方法返回這個三角形的面積
6、一個名為getPerimeter()的方法返回這個三角形的周長
7、一個名為toString()的方法返回這個三角形的字符串描述
問題分析:
按部就班,根據(jù)要求,從數(shù)據(jù)域,到構(gòu)造方法(無參,有參),到方法,到測試類
學(xué)會使用繼承以及繼承的方法就好
代碼演示(已驗證)
1、創(chuàng)建的類:
// 創(chuàng)建的類 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、測試類,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()); } }
泡泡:
創(chuàng)建類的話認(rèn)真點應(yīng)該沒什么,在測試類里面有一個細節(jié)需要注意:在輸入 double 類型 的邊長時,要先使用字符串類型輸入,然后在轉(zhuǎn)化為double類型數(shù)據(jù),轉(zhuǎn)化方式如下:
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);
嗯,ok,收工。
補充:利用java編寫三角形的判定程序
我就廢話不多說了,大家還是直接看代碼吧~
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+”能構(gòu)成三角形!”); else System.out.println(a+”,”+b+”,”+c+”不能構(gòu)成三角形!”); }
運行結(jié)果如下所示
程序小總結(jié):
有的時候,我們需要在滿足某種條件時進行一些操作,而不滿足條件時就不進行任何操作,這個時候我們可以只使用 if 語句。也就是說,if else 不必同時出現(xiàn)。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
mybatis連接MySQL8出現(xiàn)的問題解決方法
這篇文章主要介紹了mybatis連接MySQL8出現(xiàn)的問題解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10Spring?Security過濾器鏈加載執(zhí)行流程源碼解析
Spring?Boot?對于?Spring?Security?提供了自動化配置方案,可以使用更少的配置來使用?Spring?Security。那么這個過濾器鏈?zhǔn)窃趺醇虞d和實現(xiàn)攔截的呢,對Spring?Security過濾器鏈加載執(zhí)行流程感興趣的朋友一起看看吧2021-12-12Elasticsearch term 查詢之精確值搜索功能實現(xiàn)
term查詢是Elasticsearch中用于精確值搜索的一種基本方式,通過了解 term 查詢的工作原理和使用方法,你可以更好地利用 Elasticsearch 進行結(jié)構(gòu)化數(shù)據(jù)的搜索和分析,本文將詳細介紹 term 查詢的工作原理、使用場景以及如何在 Elasticsearch 中應(yīng)用它,感興趣的朋友一起看看吧2024-06-06Spring Boot 中的 Spring Cloud Feign的原
Spring Cloud Feign 是 Spring Cloud 中的一個組件,它可以幫助我們實現(xiàn)聲明式的 REST 客戶,這篇文章主要介紹了Spring Boot 中的 Spring Cloud Feign,需要的朋友可以參考下2023-07-07解決springcloud 配置gateway 出現(xiàn)錯誤的問題
今天給大家分享springcloud 配置gateway 出現(xiàn)錯誤的問題,其實解決方法很簡單,只需要降低springcloud版本,改成Hoxton.SR5就好了,再次改成Hoxton.SR12,也不報錯了,下面給大家展示下,感興趣的朋友一起看看吧2021-11-11SpringBoot后端接收參數(shù)優(yōu)化代碼示例(統(tǒng)一處理前端參數(shù))
使用Spring Boot開發(fā)API的時候,讀取請求參數(shù)是服務(wù)端編碼中最基本的一項操作,下面這篇文章主要給大家介紹了關(guān)于SpringBoot后端接收參數(shù)優(yōu)化(統(tǒng)一處理前端參數(shù))的相關(guān)資料,需要的朋友可以參考下2024-07-07