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)