2. 编写一个Java应用程序,该程序中有3个类:Triangle、Lader和Circle,分别用来刻画“三角形”、“梯形”和“圆形”。
具体要求如下:Triangle类具有类型为double的三个边以及周长和面积属性,Triangle类具有返回周长、面积和修改三个边的方法。另外,Triangle类还具有一个boolean类型的属性,该属性用来判断三个数能否构成一个三角形。Lader类具有类型为double的上底、下底、高和面积属性,具有返回面积的方法。Circle类具有类型为double的半径、周长和面积属性,具有返回周长和面积的方法。
1. 答
import java.util.*;
class RandomSort {
public static void main(String args[]) {
int num1[]=new int[10];
Random random=new Random();
for (int i=0; i<num1.length; i++) {
num1[i]=Math.abs(random.nextInt())%100+1;
}
Arrays.sort(num1);
int num2[]=new int[num1.length];
for (int i=0, j=0; i<num1.length; i++, j++) {
num2[j]=num1[i];
System.out.println(num2[i]);
}
}
}
2. 答
public class Shape {
public static void main(String args[])throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double a = 0, b = 0, c = 0;
System.out.println("依次输入三角形三条边:");
do {
try {
a = Double.parseDouble(br.readLine());
b = Double.parseDouble(br.readLine());
c = Double.parseDouble(br.readLine());
} catch (NumberFormatException e) {
System.out.println("请输入合法数字");
continue;
}
}while (!Triangle.isTriangle(a, b, c));
Triangle triangle1 = new Triangle(a, b, c);
System.out.println("周长:" + triangle1.getGirth());
System.out.println("面积:" + triangle1.getArea());
System.out.println("每边扩大2倍后(演示修改方法)");
triangle1.setBorder(a*2,b*2,c*2);
System.out.println("周长:" + triangle1.getGirth());
System.out.println("面积:" + triangle1.getArea());
}
}
class Triangle {
private double a, b, c;
public static boolean isTriangle(double a, double b, double c) {
if ((a + b > c) && (b + c > a) && (c + a > b)) {
return true;
} else {
System.out.println("不能构成三角形,重新输入:");
return false;
}
}
public double getArea() {
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
public double getGirth() {
return (a + b + c);
}
public void setBorder(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
}
梯形和圆形的对象构造方法
6 个解决方案
#1
#2
请大侠们帮我看看这个程序有哪些还要补充和修改的.请给出修改意见,本人一定高分相送..急需,谢谢各位大哥大姐了!!
#3
基本上达到要求了啊 .高手来看看
#4
/**
*@param a top edge
*@param b bottom edge
*@param h the distance between top and bottom edges
*@param angle, the angle of left top corner.
*/
public Trapezia(doulble a, double b, dough h, double angle){
//todo initial these parameters
}
/**
*@param r radii of the cycle
*/
public Circle(double r){
//if u want to "draw" it, then need the point of the centre of a circle
}
#5
给你说说第一题的吧``
import java.util.*;
class RandomSort {
public static void main(String args[]) {
int num1[]=new int[10];
Random random=new Random();
for (int i=0; i <num1.length; i++) {
num1[i]=Math.abs(random.nextInt())%100+1;
}
Arrays.sort(num1);
int num2[]=new int[num1.length];//这里为什么要new一个新的数组?
//要打印的话,直接打印num1不就行了?
//如果非要重新定义个数组的话,那下面为什么要定义两个变量?(i和j)一个不就行了?
for (int i=0, j=0; i <num1.length; i++, j++) {
num2[j]=num1[i];//num2[i] = num1[i];不就行了
System.out.println(num2[i]);
}
}
}
#6
谢谢楼上的大哥!!
#1
#2
请大侠们帮我看看这个程序有哪些还要补充和修改的.请给出修改意见,本人一定高分相送..急需,谢谢各位大哥大姐了!!
#3
基本上达到要求了啊 .高手来看看
#4
/**
*@param a top edge
*@param b bottom edge
*@param h the distance between top and bottom edges
*@param angle, the angle of left top corner.
*/
public Trapezia(doulble a, double b, dough h, double angle){
//todo initial these parameters
}
/**
*@param r radii of the cycle
*/
public Circle(double r){
//if u want to "draw" it, then need the point of the centre of a circle
}
#5
给你说说第一题的吧``
import java.util.*;
class RandomSort {
public static void main(String args[]) {
int num1[]=new int[10];
Random random=new Random();
for (int i=0; i <num1.length; i++) {
num1[i]=Math.abs(random.nextInt())%100+1;
}
Arrays.sort(num1);
int num2[]=new int[num1.length];//这里为什么要new一个新的数组?
//要打印的话,直接打印num1不就行了?
//如果非要重新定义个数组的话,那下面为什么要定义两个变量?(i和j)一个不就行了?
for (int i=0, j=0; i <num1.length; i++, j++) {
num2[j]=num1[i];//num2[i] = num1[i];不就行了
System.out.println(num2[i]);
}
}
}
#6
谢谢楼上的大哥!!