Ⅰ 用java語言定義一個三角形類 Triangle , 各位幫個忙!
public class Triangle
{
int a;
int b;
int c;
public Triangle(int a, int b, int c){
this.a = a;
this.b = b;
this.c = c;
}
public int getZhouChang(){
return a + b + c;
}
public double getArea(){
double area;
double s;
s=1.0/2*(a+b+c);
area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
public boolean isTriangle(){
if(a + b > c || a + c > b || b + c > a){
return true;
}else{
return false;
}
}
}
採納吧 打字辛苦的
Ⅱ java編程題:定義Triangle類用於表示三角形,其任意兩個邊的和必須大雨第三條邊。定義
public class Triangle {
//定義三角形
private double A,B,C;
public Triangle() {
this.A = 0d;
this.B = 0d;
this.C = 0d;
}
public Triangle(double A,double B,double C)throws IllegalAccessException{
if(!check1(A,B,C) && !check2(A,B,C)){
System.out.println("不能構成三角形");
}
else{
this.A = A;
this.B =B;
this.C = C;
}
}
//任意兩邊之和大於第三邊,
public boolean check1(double A,double B,double C) {
if (A + B > C && A + C > B && B + C > A)
return true;
return false;
}
// 任意兩邊之差小於第三邊
public boolean check2(double A,double B,double C) {
if (A - B < C && A - C < B && B - C < A)
return true;
return false;
}
@Override
public String toString() {
return "三角形的三條邊為: [A=" + A + ", B=" + B + ", C=" + C + "]";
}
public double getA() {
return A;
}
public void setA(double a) {
A = a;
}
public double getB() {
return B;
}
public void setB(double b) {
B = b;
}
public double getC() {
return C;
}
public void setC(double c) {
C = c;
}
public static void main(String[] args) throws IllegalAccessException {
Triangle san = new Triangle(6,8,10);
System.out.println(san);
}
}
Ⅲ java程序題:創建三角形類Triangle
publicclassTriangle{
/*
*•創建三角形類Triangle,該類包括:三個名為side1,side2,side3的double數據域默認值1.0;
*一個無參構造方法,創建默認的三角形;一個side1,side2,side3指定值的構造方法三個數據域的訪問器和修改器;名為getArea(
*)的方法,返回三角形的面積;名為getPerimeter()的方法,返回三角形的周長;
*名為toString的方法,返回三角形的字元串,return"Triangle:side1="+side1+
*"side2="+side2+"side3="+side3;
*/
privatedoubleside1=10;
privatedoubleside2=10;
privatedoubleside3=10;
publicdoublegetArea(){
return(side1+side2+side3)/2;
}
publicdoublegetPerimeter(){
returnside1+side2+side3;
}
publicStringtoString(){
return"Triangle:side1="+side1+"side2="+side2+"side3="
+side3;
}
/**
*@Description:Triangle構造函數
*/
publicTriangle(){
}
publicdoublegetSide1(){
returnside1;
}
publicvoidsetSide1(doubleside1){
this.side1=side1;
}
publicdoublegetSide2(){
returnside2;
}
publicvoidsetSide2(doubleside2){
this.side2=side2;
}
publicdoublegetSide3(){
returnside3;
}
publicvoidsetSide3(doubleside3){
this.side3=side3;
}
}
Ⅳ java如何定義一個Triangle類
public class Triangle
{
private double size1;
private double size2;
private double size3;
private boolean isTriangle;
public Triangle(double size1, double size2, double size3) {
this.size1 = size1;
this.size2 = size2;
this.size3 = size3;
isTriangle=triangle();
}
private boolean triangle()
{
if((size1+size2>size3)&&
(size1+size3>size2)&&
(size2+size3>size1)&&
(size1-size2<size3)&&
(size1-size3<size2)&&
size2-size3<size1)
return true;
return false;
}
public double getSize1() {
return size1;
}
public void setSize1(double size1) {
this.size1 = size1;
}
public double getSize2() {
return size2;
}
public void setSize2(double size2) {
this.size2 = size2;
}
public double getSize3() {
return size3;
}
public void setSize3(double size3) {
this.size3 = size3;
}
public boolean isTriangle() {
return isTriangle;
}
public void setTriangle(boolean isTriangle) {
this.isTriangle = isTriangle;
}
public double getArea() {
if(isTriangle)
{
double c=getPermie()/2;
return Math.sqrt(c*(c-size1)*(c-size2)*(c-size3));
}
throw new IllegalArgumentException("三邊不能構成上三角行!");
}
public double getPermie() {
if(isTriangle)
return size1+size2+size3;
throw new IllegalArgumentException("三邊不能構成上三角行!");
}
public static void main(String[] args)
{
Triangle triangle=new Triangle(3, 4, 5);
System.out.println("周長:"+triangle.getPermie());
System.out.println("面積:"+triangle.getArea());
triangle.setSize1(3);
triangle.setSize2(3);
triangle.setSize3(3);
System.out.println("周長:"+triangle.getPermie());
System.out.println("面積:"+triangle.getArea());
}
}
運行結果:
周長:12.0
面積:6.0
周長:9.0
面積:3.897114317029974
Ⅳ Java:【三角形類Triangle】設計一個名為Triangle的類來擴展GeometricObject類。該類包括:
importjava.util.Scanner;
publicclassexercise_11_01{
{
privatedoubleside1=1.0;
privatedoubleside2=1.0;
privatedoubleside3=1.0;
Triangle(){
}
Triangle(doubleside1,doubleside2,doubleside3){
this.side1=side1;
this.side2=side2;
this.side3=side3;
//throwexercise_12_05.IllegalTriangleException{
//
//}
}
publicdoublegetSide1(){
returnside1;
}
publicdoublegetSide2(){
returnside2;
}
publicdoublegetSide3(){
returnside3;
}
doublegetArea(){
doublep=(side1+side2+side3)/2;
returnMath.sqrt(p*(p-side1)*(p-side2)*(p-side3));
}
doublegetPerimeter(){
returnside1+side2+side3;
}
@Override
publicStringtoString(){
return"Triangle: side1="+side1+" side2="+side2+" side3="+side3+" area:"+getArea()+" perimeter:"+getPerimeter();
}
}
publicstaticvoidmain(String[]args){
Scannerinput=newScanner(System.in);
System.out.println("請輸入三角形的三邊長度、顏色、是否填充:");
doubleside1=input.nextDouble();
doubleside2=input.nextDouble();
doubleside3=input.nextDouble();
Stringcolor=input.next();
booleanfilled=input.nextBoolean();
Trianglea=newTriangle(side1,side2,side3);
a.setColor(color);
a.setFilled(filled);
System.out.println(a.toString()+" color:"+a.getColor()+" isFilled:"+a.isFilled());
}
}
我這個是讓用戶來輸入的,如果需要「創建邊長為1、1.5和1,顏色為yellow,filled為true的Triangle對象」,直接把測試數據寫進去就好