Java object-oriented programming exercises (once)

Source: Internet
Author: User

Recently, I'm going to share some of the little exercises I've studied before, to be a solid knowledge. Although it is a small exercise, but the more you see the more interesting, warm and know new.

Exercise: Function diving competition, 8 judges rating. The average score after the top and bottom of the athlete's performance is removed

code example:

1. Guide Package

Import Java.io.BufferedReader; Import java.io.IOException; import Java.io.InputStreamReader;

2. Test class

 public  class    Demo6 { /**   *   @param   args  */ public  static  void   main (string[] args) { //  TODO auto-generated method stub  Judge judge=new   Judge ();  // judge.getlastfen (); Do not print, how to produce results oh      System.out.println (Judge.getlastfen ()); }}

3.Judge Class (encapsulation method, very good object-oriented programming)

classJudge {//define one that can hold 8 decimal places    floatFens[] =NULL; //defined here, convenient for later change    intSize = 8; //Constructor Code initialization is specialized, it becomes more canonical     PublicJudge () {fens=New float[size]; //InitializeBufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); //for (int i=0;i<fens.length;i++) {//cannot be written here: System.out.println ("Please enter" + (i+1) + "referee's score");        Try {             for(inti = 0; i < fens.length; i++) {System.out.println ("Please enter" + (i + 1) + "Referee's score"); Fens[i]=float.parsefloat (Br.readline ()); }        } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally {            Try{br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }    }    //get the athlete's final score     Public floatGetlastfen () {//Features://find the lowest, highest (no way to get rid of it, only to find its subscript) and remove the lowest highest to draw the final score        floatAllfen = 0; intMinindex = This. Getlowfenindex (); intMaxindex = This. Gethightfenindex ();  for(inti = 0; i < fens.length; i++) {            if(I! = Minindex && I! =Maxindex) {Allfen+=Fens[i]; }        }        returnAllfen/(Fens.length-2); }     Public intGetlowfenindex () {//Selection method: Think the first one is the lowest score        floatMinfen = Fens[0]; intMinindex = 0;  for(inti = 1; i < fens.length; i++) {            //Modify the minimum score            if(Minfen <Fens[i]) {Minfen=Fens[i]; Minindex=i; }        }        returnMinindex; }     Public intGethightfenindex () {//Selection method: Think the first one is the lowest score        floatMaxfen = Fens[0]; intMaxindex = 0;  for(inti = 1; i < fens.length; i++) {            //Modify the minimum score            if(Maxfen <Fens[i]) {Maxfen=Fens[i]; Maxindex=i; }        }        returnMaxindex; }}

In addition, recently felt that the generics really is a very magical existence. What did the designer think of it?! Smart dead.

Generics can use an indeterminate type to represent any type. Great location!!!

Java object-oriented programming exercises (once)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.