1. Two-point lookup, Java input and output
1 Packagecom;2 3 4 Public classfirstapp{5 Public Static voidMain (String args[])6 {7Java.util.Scanner sc=NewJava.util.Scanner (system.in);8 intN,start,end,middle;9System.out.println ("Please Enter:");Tenn=sc.nextint (); One inta[]={-2,1,4,5,8,12,17,23,45,56,90,100}; AStart=0; -End=a.length-1; -Middle= (start+end)/2; the intCount=0; - while(n!=A[middle]) { - if(n>A[middle]) { -start=Middle; + } - Else if(n<A[middle]) { +End=Middle; A } atMiddle= (start+end)/2; -count++; - if(COUNT>A.LENGTH/2) - Break; - } - if(COUNT>A.LENGTH/2) inSystem.out.println (":" +n+ "not in array"); - Else toSystem.out.println (":" +n+ "is an array of" +middle+ "elements"); + - } the}View Code
Class and object of 2.java, new initializes the object
1 Packagecom;2 classXiyourenwu {floatHeight,weight;3 String Head, Ear,hand,foot,mouth;4 voidspeak (String s)5 { 6Head= "Crooked Head";7 System.out.println (s);8 }9 }Ten One Public classExmple { A Public Static voidMain (String args[]) - {Xiyourenwu zhubajie,sunwukong; -Zhubajie=NewXiyourenwu (); thesunwukong=NewXiyourenwu (); -zhubajie.height=1.80f;//object assigns a value to its own variable. -zhubajie.weight=160f; -Zhubajie.hand= "Two black Hands"; +zhubajie.foot= "Two Feet"; -Zhubajie.head= "Big Head"; +Zhubajie.ear= "A pair of big ears"; AZhubajie.mouth= "a big Mouth"; atsunwukong.height=1.62f;//object assigns a value to its own variable. -sunwukong.weight=1000f; -sunwukong.hand= "Small Hands"; -sunwukong.foot= "Two Embroidered feet"; -Sunwukong.head= "embroidered floating serves floating"; -Sunwukong.ear= "A pair of little ears"; inSunwukong.mouth= "Cherry Small Mouth"; -System.out.println ("Zhubajie Height:" +zhubajie.height); toSystem.out.println ("Zhubajie's head:" +zhubajie.head); +System.out.println ("Sunwukong Weight:" +sunwukong.weight); -System.out.println ("Sunwukong's head:" +sunwukong.head); theZhubajie.speak ("My old pig I want to marry a daughter-in-law");//object to invoke the method. *System.out.println ("Zhubajie now head:" +zhubajie.head); $Sunwukong.speak ("Lao sun I weigh 1000 pounds, I want to cheat eight to recite Me");//object to invoke the method. Panax NotoginsengSystem.out.println ("Sunwukong now head:" +sunwukong.head); - the } + A}View Code
A simple Java program