欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA 类与对象 例题4

程序员文章站 2024-03-15 17:42:06
...
public class SingGame {

	public static void main(String[] args) {
      Line line=new Line();
      line.giverPersonScore();

	}

}
import java.util.Scanner;
public class InputScore {
   DelScore del;
   InputScore(DelScore del){
	   this.del=del;
   }
   public void inputScore() {
	System.out.println("请输入评委数");
	Scanner read=new Scanner(System.in);
	int count=read.nextInt();
	System.out.println("输入各个评委的分数");
	double []a=new double[count];
	for(int i=0;i<count;i++){
		a[i]=read.nextDouble();
	}
	del.doDelete(a);
}
}
public class DelScore {
	  ComputerAver computer;
	  public DelScore(ComputerAver computer) {
	     this.computer=computer;
	}
	  public void doDelete(double [] a) {
	     java.util.Arrays.sort(a);
	     System.out.println("去掉一个最高分:"+a[a.length-1]+",");
	     System.out.println("去掉一个最低分:"+a[0]+"。");
	     double b[]=new double[a.length-2];
	     for(int i=1;i<a.length-1;i++){
	    	 b[i-1]=a[i];
	     }
	     computer.giverAver(b);
	}
	}
public class ComputerAver {
      public void giverAver(double [] b) {
    	  double sum=0;
    	  for(int i=0;i<b.length;i++) {
    		  sum=sum+b[i];
    	  }
    	  double aver=sum/b.length;
    	  System.out.println("选手最后得分"+aver);
      }
}

public class Line {
   InputScore one;
   DelScore two;
   ComputerAver three;
   Line(){
	   three=new ComputerAver();
	   two=new DelScore(three);
	   one=new InputScore(two);
   }
   public void giverPersonScore() {
	   one.inputScore();
   }
}
以上程序放在同一个包里即可运行
JAVA 类与对象 例题4
public class rational {
   int numerator=1; //分子
   int denominator=1;//分母
   void setNumerator(int a){//设置分子
	   int c=f(Math.abs(a),denominator); //计算最大公约数
	   numerator=a/c;
	   denominator=denominator/c;
	   if(numerator<0&&denominator<0){
		   numerator=-numerator;
		   denominator=-denominator;
	   }
   }
   void setDenominator(int b){
	   int c=f(numerator,Math.abs(b));
	   numerator=numerator/c;
	   denominator=b/c;
	   if(numerator<0&&denominator<0){
		   numerator=-numerator;
		   denominator=-denominator;
	   }
   }
   int getNumberator(){
	   return numerator;
   }
   int getDenominator(){
	   return denominator;
   }
   int f(int a,int b){//求a和b的最大公约数
	   if(a==0)
		   return 1;
	   if(a<b){
		   int c=a;
		   a=b;
		   b=c;
	   }
	   int r=a%b;
	   while(r!=0){
		   a=b;
		   b=r;
		   r=a%b;
	   }
	   return b;
   }
   rational add(rational r){//加法运算
	   int a=r.getNumberator();//返回有理数r的分子
	   int b=r.getDenominator();//返回有理数r的分母
	   int newNumberator = numerator*b+denominator*a;//计算出新分子
	   int newDenominator= denominator*b;//计算出新分母
	   rational result  =new rational();
	   result.setNumerator(newNumberator);
	   result.setDenominator(newDenominator);
	   return result;	   
   }
rational sub(rational r){//加法运算
	   int a=r.getNumberator();//返回有理数r的分子
	   int b=r.getDenominator();//返回有理数r的分母
	   int newNumberator = numerator*a;//计算出新分子
	   int newDenominator= denominator*b;//计算出新分母
	   rational result  =new rational();
	   result.setNumerator(newNumberator);
	   result.setDenominator(newDenominator);
	   return result;	   
}
rational muti(rational r){//加法运算
	   int a=r.getNumberator();//返回有理数r的分子
	   int b=r.getDenominator();//返回有理数r的分母
	   int newNumberator = numerator*b;//计算出新分子
	   int newDenominator= denominator*a;//计算出新分母
	   rational result  =new rational();
	   result.setNumerator(newNumberator);
	   result.setDenominator(newDenominator);
	   return result;	   
}
rational div(rational r){//加法运算
	   int a=r.getNumberator();//返回有理数r的分子
	   int b=r.getDenominator();//返回有理数r的分母
	   int newNumberator = numerator*b;//计算出新分子
	   int newDenominator= denominator*a;//计算出新分母
	   rational result  =new rational();
	   result.setNumerator(newNumberator);
	   result.setDenominator(newDenominator);
	   return result;	   
}
}
public class exam {

	public static void main(String[] args) {
	 rational r1 =new rational();
	 r1.setNumerator(1);
	 r1.setDenominator(5);
	 rational r2 = new rational();
	 r2.setNumerator(3);
	 r2.setDenominator(2);
	 rational result=r1.add(r2);
	 int a=result.getNumberator();
	 int b=result.getDenominator();
	 System.out.println("1/5+3/2 = "+a+"/"+b);
	 
	 result=r1.sub(r2);
	 a=result.getNumberator();
	 b=result.getDenominator();
	 System.out.println("1/5-3/2 = "+a+"/"+b);
	 
	 result=r1.muti(r2);
	 a=result.getNumberator();
	 b=result.getDenominator();
	 System.out.println("1/5*3/2 = "+a+"/"+b);
	 
	 result=r1.div(r2);
	 a=result.getNumberator();
	 b=result.getDenominator();
	 System.out.println("1/5 / 3/2 = "+a+"/"+b);
     int n=10,k=1;
     System.out.println("计算2/1+3/2+5/3+8/5+13/8+...的前"+n+"项和.");
     rational sum=new rational();
     sum.setNumerator(0);
     rational item=new rational();
     item.setNumerator(2);
     item.setDenominator(1);
     while(k<=n){
    	 sum=sum.add(item);
    	 k++;
    	 int fenzi=item.getNumberator();
    	 int fenmu=item.getDenominator();
    	 item.setNumerator(fenzi+fenmu);
    	 item.setDenominator(fenzi);
     }
     a=sum.getNumberator();
     b=sum.getDenominator();
     System.out.println("用分子表示:");
     System.out.println(a+"/"+b);
     double doubleresult=(a*1.0)/b;
     System.out.println("用小数表示");
     System.out.println(doubleresult);
	}

}

JAVA 类与对象 例题4





相关标签: JAVA 类与对象