收获
学习到arrays.sort()??????https://blog.csdn.net/zg97zb/article/details/103764165?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163474346816780265428943%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163474346816780265428943&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-103764165.pc_search_ecpm_flag&utm_term=+Arrays.sort%28goodRings%2C+%28o1%2C+o2%29+-%3E+Double.compare%28o2.getArea%28%29%2C+o1.getArea%28%29%29%29%3B&spm=??????
Main类
package test5;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
//从键盘输入要创建的GoodRing对象的个数n;
System.out.print("请输入要创建的GoodRing对象的个数:");
int num=reader.nextInt();
//创建n个GoodRing对象的数组
GoodRing[] goodRings=GoodRingUtil.createGoodRings(num);
System.out.println();
//输出n个GoodRing对象的面积之和
System.out.print(num+"个GoodRing对象的面积之和为:"+GoodRingUtil.totalArea(goodRings));
System.out.println();
//输出所有GoodRing对象
System.out.println("所有GoodRing对象分别为:");
GoodRingUtil.outputGoodRings(goodRings);
System.out.println();
//对数组进行排序
GoodRingUtil.sort(goodRings);
//输出所有GoodRing对象
System.out.println("排序后所有GoodRing对象分别为:");
GoodRingUtil.outputGoodRings(goodRings);
reader.close();//关掉reader
}
}
GoodRing类
package test5;
public class GoodRing {
//初始化数据域变量,改为private
private double innerRadius;//内半径
private double outerRadius;//外半径
private String fillColor;//填充颜色
// private double Area;
//数据域对应的访问器和修改器的修饰符均为public
public double getinnerRadius() {
return innerRadius;
}
public void setinnerRadius(double innerRadius) {
this.innerRadius=innerRadius;
}
public double getouterRadius() {
return outerRadius;
}
public void setouterRadius(double outerRadius) {
this.outerRadius=outerRadius;
}
public String getfillColor() {
return fillColor;
}
public void setfillColor(String fillColor) {
this.fillColor=fillColor;
}
//构造方法名改为GoodRing,修饰符均用public
//构造有参数方法,用于创建环的对象
public GoodRing(double iRadius,double oRadius,String color){
innerRadius=iRadius;//初始化内半径
outerRadius=oRadius;//初始化外半径
fillColor=color;//初始化填充颜色
}
//无参数构造方法
public GoodRing(){
innerRadius=1;
outerRadius=2;
fillColor="WHITE";
}
//计算方法均用修饰符public修饰
//计算并返回圆面积的方法
public double getArea() {
return (outerRadius*outerRadius-innerRadius*innerRadius)*Math.PI;
}
// public void setArea(double Area){
// this.Area=Area;
// }
//计算并返回内圆面积的方法
public double getInnerThength() {
return 2*innerRadius*Math.PI;
}
//计算并返回外圆面积的方法
public double getOutThength() {
return 2*outerRadius*Math.PI;
}
}
TestGoodRing类
package test5;
public class TestGoodRing {
public static void main(String[] args) {
GoodRing goodring=new GoodRing();
//第一次输出
System.out.println("内圆半径:"+goodring.getinnerRadius());
System.out.println("外圆半径:"+goodring.getouterRadius());
System.out.println("填充颜色:"+goodring.getfillColor());
System.out.println("环的面积:"+goodring.getArea());
System.out.println("内圆的周长为:"+goodring.getInnerThength());
System.out.println("外圆的周长为:"+goodring.getOutThength());
System.out.println();
//修改数据
goodring.setinnerRadius(2);
goodring.setouterRadius(5);
goodring.setfillColor("BLACK");
//第二次输出
System.out.println("内圆半径:"+goodring.getinnerRadius());
System.out.println("外圆半径:"+goodring.getouterRadius());
System.out.println("填充颜色:"+goodring.getfillColor());
System.out.println("环的面积:"+goodring.getArea());
System.out.println("内圆的周长为:"+goodring.getInnerThength());
System.out.println("外圆的周长为:"+goodring.getOutThength());
}
}
GoodRingUtil类
package test5;
import java.util.Random;
import java.util.Arrays;
public class GoodRingUtil {
//随机生成各个GoodRing对象的内外半径。
public static GoodRing[] createGoodRings(int num) {
GoodRing[] goodRings=new GoodRing[num];
Random random=new Random();
for(int i=0;i<num;i++) {
goodRings[i]=new GoodRing(random.nextInt(5)+1,random.nextInt(5)+6,"PINK");
}
return goodRings;
}
//表示数组中所有GoodRing对象的面积之和。
public static double totalArea(GoodRing[] goodRings) {
double total=0;
for(int i=0;i<goodRings.length;i++) {
total+=goodRings[i].getArea();
}
return total;
}
//将数组中的GoodRing对象按面积从大到小排序。
public static void sort(GoodRing[] goodRings) {
//Arrays.sort()双轴快排
Arrays.sort(goodRings, (o1, o2) -> Double.compare(o2.getArea(), o1.getArea()));//降序o2-o1
// int n=goodRings.length;
// for(int i=0;i<n/2;i++) {
// if(goodRings[i].getArea()<goodRings[n-i].getArea()) {
// double temp=goodRings[i].getArea();
// goodRings[i].setArea(goodRings[n-i].getArea());
// goodRings[n-i].setArea(temp);
// }
// }
}
//输出数组中所有的GoodRing对象,包括内外半径、颜色和面积。每行输出一个GoodRing对象。
public static void outputGoodRings(GoodRing[] goodRings) {
for(int i=0;i<goodRings.length;i++) {
System.out.print("环的内圆半径为:"+goodRings[i].getinnerRadius()+";");
System.out.print("环的外圆半径为:"+goodRings[i].getouterRadius()+";");
System.out.print("环的颜色为:"+goodRings[i].getfillColor()+";");
System.out.println("环的面积为:"+goodRings[i].getArea());
}
}
}
|