Java实验5——圆环类的数据封装及使用

Java实验5——圆环类的数据封装及使用,第1张

Java实验5——圆环类的数据封装及使用 收获

学习到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 Double.compare(o2.getArea(), o1.getArea()));//降序o2-o1
    	 
//    	 int n=goodRings.length;
//    	 for(int i=0;i 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4694377.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-07
下一篇 2022-11-07

发表评论

登录后才能评论

评论列表(0条)

保存