模仿circle类 /1、成员变量,针对类的属性 private int r; //2、成员方法,针对类的行为 public int getR()//get访问器,getter { return r; } public void setR(int a)//set访问器,setter { r = a; } public double getArea() { return Math.PI * r * r; }}
public class CircleTest { public static void main(String[] args) { // TODO Auto-generated method stub Circle c = new Circle(); c.setR(3); System.out.println("半径为" + c.getR() + "的圆的面积是" + c.getArea()); }}
写出矩形类、三角形类。随机生成100个长度和宽度范围在[1, 100]的矩形,然后对这些矩形排序。Rectangle[] a = new Rectangle[100];示例结果:第1大的矩形为:98 * 96 = ……第2大的矩形为:97 * 96 = ……第100大的矩形为:3 * 4 = 12
1、矩形类:
package cn.personal.demo06;
public class Rectangle {
// 定义成员变量,宽和高
private int width;
private int heigh;
// 添加有参构造方法
public Rectangle(int width, int heigh) {
super();
this.width = width;
this.heigh = heigh;
}
// 添加get/set方法
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeigh() {
return heigh;
}
public void setHeigh(int heigh) {
this.heigh = heigh;
}
//获取面积的方法
public int getArea(int wi,int he) {
return wi*he;
}
}
2、矩形测试类
package cn.personal.demo06;
import java.util.Arrays;
public class RectangleTest {
public static void main(String[] args) {
// 定义Rectangle数组,长度为100;
Rectangle[] rects = new Rectangle[100];
// 定义Rectangle中的width数组,长度为100;
int[] widths = new int[100];
// 定义Rectangle中的height数组,长度为100;
int[] heights = new int[100];
for (int i = 0; i < rects.length; i++) {
int wr=getRandon();
int hr=getRandon();
rects[i]=new Rectangle(wr, hr);
widths[i] = rects[i].getWidth();
heights[i] = rects[i].getHeigh();
}
//将宽度和高度进行升序排序
Arrays.sort(widths);
Arrays.sort(heights);
//循环输出打印
for (int i = 1; i <= rects.length; i++) {
System.out.println("第"+i+"大的矩形为:"
+widths[rects.length-i]+"*"+heights[rects.length-i]+"="
+rects[rects.length-i].getArea(widths[rects.length-i],heights[rects.length-i]));
}
}
public static int getRandon() {
return (int) (Math.random() * 99 + 1);
}
}
3、矩形测试效果
1、三角形类
package cn.personal.demo06;
public class Triangle {
//定义成员变量,三条边
private int line1;
private int line2;
private int line3;
//添加有参构造
public Triangle(int line1, int line2, int line3) {
super();
this.line1 = line1;
this.line2 = line2;
this.line3 = line3;
}
//添加get/set方法
public int getLine1() {
return line1;
}
public void setLine1(int line1) {
this.line1 = line1;
}
public int getLine2() {
return line2;
}
public void setLine2(int line2) {
this.line2 = line2;
}
public int getLine3() {
return line3;
}
public void setLine3(int line3) {
this.line3 = line3;
}
//添加求面积方法
public double getArea() {
double p = (line1 + line2 + line3) / 2d;
return Math.sqrt(p * (p - line1) * (p - line2) * (p - line3));
}
}
2、三角形测试类
package cn.personal.demo06;
public class TriangleTest {
public static void main(String[] args) {
Triangle triangle = new Triangle(3, 4, 5);
System.out.println("三边长为" + triangle.getLine1() + ","
+ triangle.getLine2() + "," + triangle.getLine3() + "的三角形面积:"
+ triangle.getArea());
triangle = new Triangle(6, 7, 8);
System.out.println("三边长为" + triangle.getLine1() + ","
+ triangle.getLine2() + "," + triangle.getLine3() + "的三角形面积:"
+ triangle.getArea());
}
}
3、三角形测试效果
有用必采纳不。
http://t.csdn.cn/LhSMV
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Rectangle[] rectangles = new Rectangle[100];
for (int i = 0; i < rectangles.length; i++) {
Rectangle rectangle = new Rectangle(random.nextInt(100) + 1,random.nextInt(100) + 1);
rectangles[i] = rectangle;
}
Arrays.sort(rectangles, new Comparator<Rectangle>() {
@Override
public int compare(Rectangle o1, Rectangle o2) {
int area1 = o1.getArea();
int area2 = o2.getArea();
return area1 -area2;
}
});
for (int i = 0; i < rectangles.length; i++) {
Rectangle rectangle = rectangles[i];
System.out.println("第" + (i + 1) + "大的矩形为:" + rectangle.getHeight() + "*" + rectangle.getWidth() + "=" + rectangle.getArea());
}
}
}
class Rectangle {
private int width;
private int height;
public Rectangle(){}
public Rectangle(int width, int height) {
super();
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
//获取面积
public int getArea() {
return width*height;
}
}
package com.example.demo.csdn.test01;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
import java.util.stream.IntStream;
public class Rectangle {
private int width;
private int height;
public int getArea() {
return width * height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public static void main(String[] args) {
Random random = new Random(70);
Rectangle[] rectangles = new Rectangle[100];
for (int i = 0; i < rectangles.length; i++) {
Rectangle rectangle = new Rectangle();
rectangle.setWidth(random.nextInt(100) + 1);
rectangle.setHeight(random.nextInt(100) + 1);
rectangles[i] = rectangle;
}
Arrays.sort(rectangles, new Comparator<Rectangle>() {
@Override
public int compare(Rectangle o1, Rectangle o2) {
int area1 = o1.getArea();
int area2 = o2.getArea();
return area1 == area2 ? 0 : (area1 > area2 ? -1 : 1);
}
});
for (int i = 0; i < rectangles.length; i++) {
Rectangle rectangle = rectangles[i];
System.out.println("第" + (i + 1) + "大的矩形为:" + rectangle.getHeight() + "*" + rectangle.getWidth() + "=" + rectangle.getArea());
}
}
}
package com.example.demo.csdn.test01;
public class Triangle {
private int line1;
private int line2;
private int line3;
public double getArea() {
double p = (line1 + line2 + line3) / 2d;
return Math.sqrt(p * (p - line1) * (p - line2) * (p - line3));
}
public int getLine1() {
return line1;
}
public void setLine1(int line1) {
this.line1 = line1;
}
public int getLine2() {
return line2;
}
public void setLine2(int line2) {
this.line2 = line2;
}
public int getLine3() {
return line3;
}
public void setLine3(int line3) {
this.line3 = line3;
}
public static void main(String[] args) {
Triangle triangle = new Triangle();
triangle.setLine1(3);
triangle.setLine2(4);
triangle.setLine3(5);
System.out.println("三边长为" + triangle.getLine1() + "," + triangle.getLine2() + "," + triangle.getLine3() + "的三角形面积:" + triangle.getArea());
triangle.setLine1(5);
triangle.setLine2(12);
triangle.setLine3(13);
System.out.println("三边长为" + triangle.getLine1() + "," + triangle.getLine2() + "," + triangle.getLine3() + "的三角形面积:" + triangle.getArea());
}
}
矩形类:
/**
* 矩形
*/
public class Rectangle implements Comparable<Rectangle>{
//类属性h,也就是矩形的高
private int h;
//类属性w,也就是矩形的宽
private int w;
//类行为方法,get访问器,获取属性h的值
public int getH() {
return h;
}
//类行为方法,set访问器,给属性h赋值
public void setH(int h) {
this.h = h;
}
//类行为方法,get访问器,获取属性w的值
public int getW() {
return w;
}
//类行为方法,set访问器,给属性w赋值
public void setW(int w) {
this.w = w;
}
//计算矩形面积
public double getArea() {
return h*w;
}
//重写排序方法,根据面积大小排序
@Override
public int compareTo(Rectangle r) {
return (int) (r.getArea()-this.getArea());
}
}
矩形测试:
import java.util.Arrays;
public class RectangleTest {
public static void main(String args[]) {
//容量为100的数组,存放生成的10的矩形
Rectangle[] rectangleArr = new Rectangle[100];
//循环100次,生成100个矩形
for(int i=0; i<100; i++) {
//创建Circle类对象
Rectangle r = new Rectangle();
//给矩形属性h赋值3,也就是设置该矩形的高为3,Math.random()获取随机数
r.setH((int)(Math.random()*100+1));
//给矩形属性w赋值4,也就是设置该矩形的宽为4,Math.random()获取随机数
r.setW((int)(Math.random()*100+1));
//放入数组
rectangleArr[i] = r;
}
//对数组内的矩形面积进行排序
Arrays.sort(rectangleArr);
//排序后输出
for(int i=0; i<100; i++) {
System.out.println("第" +( i+1) + "大矩形为:" + rectangleArr[i].getW() + "*" + rectangleArr[i].getH() + "=" + rectangleArr[i].getArea());
}
}
}
结果:
三角形类:
/**
* 三角形
*/
public class Triangle {
//类属性a,三角西的一条边
private int a;
//类属性b,三角西的一条边
private int b;
//类属性c,三角西的一条边
private int c;
//类行为方法,get访问器,获取属性a的值
public int getA() {
return a;
}
//类行为方法,set访问器,给属性a赋值
public void setA(int a) {
this.a = a;
}
//类行为方法,get访问器,获取属性b的值
public int getB() {
return b;
}
//类行为方法,set访问器,给属性b赋值
public void setB(int b) {
this.b = b;
}
//类行为方法,get访问器,获取属性c的值
public int getC() {
return c;
}
//类行为方法,set访问器,给属性c赋值
public void setC(int c) {
this.c = c;
}
//计算三角形面积
public double getArea() {
//海伦公式求三角形面积
double p = (a+b+c)/2;
return Math.sqrt((p*(p-a)*(p-b)*(p-c)));
}
}
package com.ly.csdn.test;
import lombok.Builder;
import lombok.Data;
import java.awt.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
@Data
@Builder
public class TestRectangleDemo {
//类属性长
private int length;
//类属性宽
private int width;
//类属性高
private int height;
/**
* 计算矩形面积
*
* @return
*/
public int getArea() {
return width * height;
}
/**
* 计算三角形面积
*/
public double getDeltaArea() {
//求三角形面积根据海伦公式
double p = (length + width + height) / 2;
return Math.sqrt((p * (p - length) * (p - width) * (p - height)));
}
public static void main(String[] args) {
Random random = new Random(70);
TestRectangleDemo[] r = new TestRectangleDemo[100];
for (int i = 0; i < r.length; i++) {
TestRectangleDemo testRectangleDemo =TestRectangleDemo.builder().width(random.nextInt(100) + 1)
.height(random.nextInt(100) + 1).build();;
r[i] = testRectangleDemo;
}
Arrays.sort(r, new Comparator<TestRectangleDemo>() {
@Override
public int compare(TestRectangleDemo o1, TestRectangleDemo o2) {
int area1 = o1.getArea();
int area2 = o2.getArea();
return area1 == area2 ? 0 : (area1 > area2 ? -1 : 1);
}
});
//计算矩形输出结果
for (int i = 0; i < r.length; i++) {
TestRectangleDemo rectangle = r[i];
System.out.println("第" + (i + 1) + "大的矩形为:" + rectangle.getHeight() + "*" + rectangle.getWidth() + "=" + rectangle.getArea());
}
//计算三角形输出结果
TestRectangleDemo testRectangleDemo = TestRectangleDemo.builder().length(1).width(2).height(3).build();
System.out.println("三边长为:" + testRectangleDemo.getLength() + "宽为:" + testRectangleDemo.getWidth()
+ "高为:" + testRectangleDemo.getHeight() + "的三角形面积:" + testRectangleDemo.getDeltaArea());
}
}
海伦公式
运行结果截图,望采纳===