JAVA code error

Source: Internet
Author: User

JAVA code error 1. abstract class Name {private String name; public abstract boolean isStupidName (String name) {}} what is the error? Answer: Yes. Abstract method must end with a semicolon without curly braces. 2. Is there an error in public class Something {void doSomething () {private String s = ""; int l = s. length? Answer: Yes. You cannot place any access modifiers (private, public, and protected) before local variables ). Final can be used to modify local variables (final is like abstract and strictfp, both of which are non-access modifiers. strictfp can only modify class and method, rather than variable ). 3. abstract class Something {private abstract String doSomething ();} is there nothing wrong with this? Answer: Yes. Abstract methods cannot be modified in private mode. Abstract methods specifies the implementation details of the subclass implement. How can I block abstractmethod with private? (Likewise, final cannot be added before abstract method ). 4. public class Something {public int addOne (final int x) {return ++ x ;}} is obvious. Answer: Yes. Int x is modified to final, meaning x cannot be modified in addOne method. 5. public class Something {public static void main (String [] args) {Other o = new Other (); new Something (). addOne (o);} public void addOne (final Other o) {o. I ++ ;}} class Other {public int I;} is similar to the above. They are all about final. Is this wrong? Answer: Correct. In addOne method, the parameter o is modified to final. If we modify the reference (for example, o = new Other ();) of o in addOne method, it is also wrong as in the previous example. But here we modify the member vairable (member variable) of o, and the reference of o is not changed. 6. What is wrong with class Something {int I; public void doSomething () {System. out. println ("I =" + I? It cannot be seen. Answer: Correct. The output is "I = 0 ". Int I belongs to instant variable (instance variable, or member variable ). Instant variable has default value. The default value of int is 0. 7. class Something {final int I; public void doSomething () {System. out. println ("I =" + I) ;}} is different from the above one, that is, an additional final. Is that true? Answer: Yes. Final int I is a final instant variable (instance variable, or member variable ). The instant variable of final has no default value and must be assigned a clear value before the constructor ends. It can be changed to "final int I = 0 ;". 8. public class Something {public static void main (String [] args) {Something s = new Something (); System. out. println ("s. doSomething () returns "+ doSomething ();} public String doSomething () {return" Do something... ";}} looks perfect. Answer: Yes. It seems that there is no problem with calling doSomething in main. After all, both methods are in the same class. But take a closer look, main is static. Static method cannot call non-static methods directly. You can change it to "System. out. println (" s. doSomething () returns "+ s. doSomething ());". Similarly, static method cannot access non-static instant variable. 9. here, the name of the Something class is OtherThing. javaclass Something {private static void main (String [] something_to_do) {System. out. println ("Do something... ") ;}} this seems obvious. Answer: Correct. No one has ever said that the Java Class name must be the same as its file name. However, the public class name must be the same as the file name. 10. interface A {int x = 0;} class B {int x = 1;} class C extends B implements A {public void pX () {System. out. println (x);} public static void main (String [] args) {new C (). pX () ;}} answer: incorrect. An error occurs during compilation (the error description varies with the JVM, which means that the unspecified x call matches both of them (just like importing java at the same time. util and java. when two SQL packages are declared, the Date is the same ). The variables of the parent class can be identified using super. x, and the interface property is implicitly public static final by default. Therefore, A. x can be used to specify the variables. 11. interface Playable {void play ();} interface Bounceable {void play ();} interface Rollable extends Playable, Bounceable {Ball ball = new Ball ("PingPang ");} class Ball implements Rollable {private String name; public String getName () {return name;} public Ball (String name) {this. name = name;} public void play () {ball = new Ball ("Football"); System. out. println (ball. getName () ;}} this error is not easy to detect. Answer: Yes. "Interface Rollable extends Playable, Bounceable" is correct. The interface can inherit multiple interfaces, so this is correct. The problem lies in "Ball ball = new Ball (" PingPang ");" in interface Rollable ");". The default value of interface variable declared in the interface is public static final. That is to say, "Ball ball = new Ball (" PingPang ");" is actually "public static final Ball = new ball (" PingPang ");". In the Play () method of the Ball class, "ball = new Ball (" Football ");" changes the reference of the ball, and the ball here comes from the Rollable interface, the ball in Rollable interface is public static final, and the final object cannot be changed by reference. Therefore, the compiler will display errors in "ball = new Ball (" Football. JAVA programming question 1. enter n numbers, separated by commas (,), and then sort them in ascending or descending order. The sort key is displayed on the other page and the result is resetimport java. util. *; public class bycomma {public static String [] splitStringByComma (String source) {if (source = null | source. trim (). equals ("") return null; StringTokenizer commaToker = new StringTokenizer (source, ","); String [] result = new String [commaToker. countTokens ()]; int I = 0; while (commaToker. hasMoreTokens () {result [I] = commaToker. ne XtToken (); I ++;} return result;} public static void main (String args []) {String [] s = splitStringByComma ("5, 8, 7, 4, 3, 9, 1 "); int [] ii = new int [s. length]; for (int I = 0; I <s. length; I ++) {ii [I] = Integer. parseInt (s [I]);} Arrays. sort (ii); // ascfor (int I = 0; I <s. length; I ++) {System. out. println (ii [I]);} // descfor (int I = (s. length-1); I> = 0; I --) {System. out. println (ii [I]) ;}} 2. amount conversion: The amount of Arabic numerals is converted to the traditional Chinese format, for example: (¥1011)-> (one thousand and one yuan) Output. Package test. format; import java. text. numberFormat; import java. util. hashMap; public class SimpleMoneyFormat {public static final String EMPTY = ""; public static final String ZERO = "ZERO"; public static final String ONE = "ONE "; public static final String TWO = ""; public static final String THREE = ""; public static final String FOUR = ""; public static final String FIVE = ""; public static final String SI X = ""; public static final String SEVEN = "login"; public static final String EIGHT = "login"; public static final String NINE = "login "; public static final String TEN = "pick up"; public static final String HUNDRED = "empty"; public static final String THOUSAND = "empty"; public static final String TEN_THOUSAND = "ten thousand "; public static final String HUNDRED_MILLION = ""; public static final String YUAN = ""; public static final Str Ing JIAO = ""; public static final String FEN = "points"; public static final String DOT = ". "; private static SimpleMoneyFormat formatter = null; private HashMap chineseNumberMap = new HashMap (); private HashMap chineseMoneyPattern = new HashMap (); private NumberFormat numberFormat = NumberFormat. getInstance (); private SimpleMoneyFormat () {numberFormat. setMaximumFractionDigits (4); numberFormat. setMinimumFr ActionDigits (2); numberFormat. setGroupingUsed (false); chineseNumberMap. put ("0", ZERO); chineseNumberMap. put ("1", ONE); chineseNumberMap. put ("2", TWO); chineseNumberMap. put ("3", THREE); chineseNumberMap. put ("4", FOUR); chineseNumberMap. put ("5", FIVE); chineseNumberMap. put ("6", SIX); chineseNumberMap. put ("7", SEVEN); chineseNumberMap. put ("8", EIGHT); chineseNumberMap. put ("9", NINE); chineseNumberMap. put (DOT, D OT); chineseMoneyPattern. put ("1", TEN); chineseMoneyPattern. put ("2", HUNDRED); chineseMoneyPattern. put ("3", THOUSAND); chineseMoneyPattern. put ("4", TEN_THOUSAND); chineseMoneyPattern. put ("5", TEN); chineseMoneyPattern. put ("6", HUNDRED); chineseMoneyPattern. put ("7", THOUSAND); chineseMoneyPattern. put ("8", HUNDRED_MILLION);} public static SimpleMoneyFormat getInstance () {if (formatter = null) formatter = n Ew SimpleMoneyFormat (); return formatter;} public String format (String moneyStr) {checkPrecision (moneyStr); String result; result = convertToChineseNumber (moneyStr); result = addUnitsToChineseMoneyString (result ); return result;} public String format (double moneyDouble) {return format (numberFormat. format (moneyDouble);} public String format (int moneyInt) {return format (numberFormat. format (moneyInt);} p Ublic String format (long moneyLong) {return format (numberFormat. format (moneyLong);} public String format (Number moneyNum) {return format (numberFormat. format (moneyNum);} private String convertToChineseNumber (String moneyStr) {String result; StringBuffer cMoneyStringBuffer = new StringBuffer (); for (int I = 0; I <moneyStr. length (); I ++) {cMoneyStringBuffer. append (chineseNumberMap. get (moneyStr. subst Ring (I, I + 1);} // The units in the Chinese characters that are contained in trillions of records, plus them int indexOfDot = cMoneyStringBuffer. indexOf (DOT); int moneyPatternCursor = 1; for (int I = indexOfDot-1; I> 0; I --) {cMoneyStringBuffer. insert (I, chineseMoneyPattern. get (EMPTY + moneyPatternCursor); moneyPatternCursor = 8? 1: moneyPatternCursor + 1;} String fractionPart = cMoneyStringBuffer. substring (cMoneyStringBuffer. indexOf (". "); cMoneyStringBuffer. delete (cMoneyStringBuffer. indexOf (". "), cMoneyStringBuffer. length (); while (cMoneyStringBuffer. indexOf ("Zero pick ")! =-1) {cMoneyStringBuffer. replace (cMoneyStringBuffer. indexOf ("Zero pick"), cMoneyStringBuffer. indexOf ("ZERO pick") + 2, ZERO);} while (cMoneyStringBuffer. indexOf ("0 bytes ")! =-1) {cMoneyStringBuffer. replace (cMoneyStringBuffer. indexOf ("zero latency"), cMoneyStringBuffer. indexOf ("0 bytes") + 2, ZERO);} while (cMoneyStringBuffer. indexOf ("0 bytes ")! =-1) {cMoneyStringBuffer. replace (cMoneyStringBuffer. indexOf ("zero latency"), cMoneyStringBuffer. indexOf ("0 bytes") + 2, ZERO);} while (cMoneyStringBuffer. indexOf ("0 thousand ")! =-1) {cMoneyStringBuffer. replace (cMoneyStringBuffer. indexOf ("0 thousand"), cMoneyStringBuffer. indexOf ("0 thousand") + 2, TEN_THOUSAND);} while (cMoneyStringBuffer. indexOf ("0 billion ")! =-1) {cMoneyStringBuffer. replace (cMoneyStringBuffer. indexOf ("0 billion"), cMoneyStringBuffer. indexOf ("0 billion") + 2, HUNDRED_MILLION);} while (cMoneyStringBuffer. indexOf ("0 ")! =-1) {cMoneyStringBuffer. replace (cMoneyStringBuffer. indexOf ("zero"), cMoneyStringBuffer. indexOf ("ZERO") + 2, ZERO);} if (cMoneyStringBuffer. lastIndexOf (ZERO) = cMoneyStringBuffer. length ()-1) cMoneyStringBuffer. delete (cMoneyStringBuffer. length ()-1, cMoneyStringBuffer. length (); cMoneyStringBuffer. append (fractionPart); result = cMoneyStringBuffer. toString (); return result;} private String addUnitsToChi NeseMoneyString (String moneyStr) {String result; StringBuffer cMoneyStringBuffer = new StringBuffer (moneyStr); int indexOfDot = cMoneyStringBuffer. indexOf (DOT); cMoneyStringBuffer. replace (indexOfDot, indexOfDot + 1, YUAN); cMoneyStringBuffer. insert (cMoneyStringBuffer. length ()-1, JIAO); cMoneyStringBuffer. insert (cMoneyStringBuffer. length (), FEN); if (cMoneyStringBuffer. indexOf ("zero angle and zero point ")! =-1) // Add cMoneyStringBuffer without a zero header. replace (cMoneyStringBuffer. indexOf ("zero-angle and zero-point"), cMoneyStringBuffer. length (), "integer"); elseif (cMoneyStringBuffer. indexOf ("0 points ")! =-1) // Add cMoneyStringBuffer without zero points. replace (cMoneyStringBuffer. indexOf ("Zero Point"), cMoneyStringBuffer. length (), "integer"); else {if (cMoneyStringBuffer. indexOf ("zero angle ")! =-1) cMoneyStringBuffer. delete (cMoneyStringBuffer. indexOf ("zero angle"), cMoneyStringBuffer. indexOf ("zero angle") + 2); // tmpBuffer. append ("whole");} result = cMoneyStringBuffer. toString (); return result;} private void checkPrecision (String moneyStr) {int fractionDigits = moneyStr. length ()-moneyStr. indexOf (DOT)-1; if (fractionDigits> 2) throw new RuntimeException ("amount" + moneyStr + "has more than two decimal places. "); // Accuracy cannot score low} public static void main (String args []) {System. out. println (getInstance (). format (new Double (10010001.01);} 3. The sequence of class execution during inheritance is generally a multiple-choice question. What will be printed? A: parent class: package test; public class FatherClass {public FatherClass () {System. out. println ("FatherClass Create") ;}} subclass: package test; import test. fatherClass; public class ChildClass extends FatherClass {public ChildClass () {System. out. println ("ChildClass Create");} public static void main (String [] args) {FatherClass fc = new FatherClass (); ChildClass cc = new ChildClass ();}} output result: C:> java test. childClassFatherC How does lass CreateFatherClass CreateChildClass Create4 implement internal classes? A: The sample code is as follows: package test; public class OuterClass {private class InterClass {public InterClass () {System. out. println ("InterClass Create") ;}} public OuterClass () {InterClass ic = new InterClass (); System. out. println ("OuterClass Create");} public static void main (String [] args) {OuterClass oc = new OuterClass () ;}} output result: C:> java test/OuterClassInterClass CreateOuterClass Create another example: public class OuterClass {pr Ivate double d1 = 1.0; // insert code here} You need to insert an inner class declaration at line 3. Which two inner class declarations arevalid? (Choose two .) a. class InnerOne {public static double methoda () {return d1 ;}} B. public class InnerOne {static double methoda () {return d1 ;}} C. private class InnerOne {double methoda () {return d1 ;}} D. static class InnerOne {protected double methoda () {return d1 ;}} E. abstract class InnerOne {public abstract double methoda ();}: 1. static internal classes can have static members, but non-static internal classes cannot have static members. Therefore, A and B are incorrect. 2. Non-static members of the static internal class can access static variables of the external class, but cannot access non-static variables of the external class. return d1 has an error. So D Error 3. Non-static members of the internal non-static class can access non-static variables of the external class. Therefore, C is correct. 4. The answer is C, E5, Java communication programming, programming questions (or Q & A). How many characters should I use JAVA SOCKET programming to read the server and then write it to the local display? A: Server program: package test; import java.net. *; import java. io. *; public class Server {private ServerSocket ss; private Socket socket; private BufferedReader in; private PrintWriter out; public Server () {try {www.2cto. comss = new ServerSocket (10000); while (true) {socket = ss. accept (); String RemoteIP = socket. getInetAddress (). getHostAddress (); String RemotePort = ":" + socket. getLocalPort (); System. out. println ("A c Lient come in! IP: "+ Remo

Related Article

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.