Ⅰ 用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对象”,直接把测试数据写进去就好