Java Related Basics (7)

Source: Internet
Author: User
Tags abstract constructor empty final variables sort string format stringbuffer

121. Can the inner class reference the members of the class that he contains? Are there any restrictions?
An inner class object can access the contents of the external class object that created it
122, the WEB service noun explanation. Introduction to the JSWDL development package. The explanation of JAXP and JAXM. SOAP, uddi,wsdl interpretation.
The Web Serviceweb service is a network-based, distributed, modular component that performs specific tasks and adheres to specific technical specifications that enable Web service interoperability with other compatible components.
JAXP (Java API for XML parsing) defines a generic interface for using DOM, SAX, and XSLT in Java. So in your program you just use these generic interfaces, and you don't need to modify the code when you need to change the implementation.
JAXM (Java API for XML messaging) is an API that provides access methods and transport mechanisms for SOAP traffic.
WSDL is an XML format used to describe a network service as a set of endpoints that operate on messages that contain document-oriented information or process-oriented information. This format first abstracts actions and messages, and then binds them to specific network protocols and message formats to define endpoints. The related concrete endpoints are grouped into abstract endpoints (services).
Soap is a Simple Object Access Protocol (PROTOCOL), which is a lightweight protocol for exchanging XML encoded information.
The purpose of UDDI is to establish standards for E-commerce; UDDI is a web-based, distributed, implementation-standard specification for Web service-information registries, as well as a set of Web service providers that enable businesses to register themselves. To enable other enterprises to discover the implementation criteria of the Access Protocol.


Java code Error Checking
1.
Abstract class Name {
private String name;
Public abstract Boolean Isstupidname (String name) {}
}
Heroes, what's wrong with that?
Answer: wrong. Abstract method must end with a semicolon with no curly braces.
2.
public class Something {
void DoSomething () {
Private String s = "";
int L = s.length ();
}
}
Is it wrong?
Answer: wrong. You cannot place any access modifiers (private,public, and protected) before a local variable. Final can be used to modify local variables
(final, like abstract and STRICTFP, are non-access modifiers, STRICTFP can only modify class and method rather than variable).
3.
Abstract class Something {
Private abstract String dosomething ();
}
There seems to be nothing wrong with that, right?
Answer: wrong. The methods of abstract cannot be decorated with private. Abstract methods is to let subclass implement (achieve) specific details, how can use private to abstract
method to seal it up? (Similarly, abstract method cannot be added before final).
4.
public class Something {
public int AddOne (final int x) {
return ++x;
}
}
This is more obvious.
Answer: wrong. int x is modified to final, meaning that 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;
}
Similar to the above, is the final question, is this wrong?
Answer: Right. In AddOne method, the parameter o is decorated to final. If we modify the reference of O in AddOne method
(for example: o = new Other ();), so it is wrong to do so as in the previous example. But the modified here is O's member vairable
(member variable), and O's reference does not change.
6.
Class Something {
int i;
public void dosomething () {
System.out.println ("i =" + i);
}
}
What's wrong with it? I can't see it.
Answer: Right. The output is "i = 0". int I belongs to instant variable (instance variable, or called 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);
}
}
There is only one place different from the one above, that is, one more final. Is that a mistake?
Answer: wrong. Final int i is a final instant variable (instance variable, or called member variable). The final instant variable does not have default value and must be given a definite value before the end of the constructor (constructor). Can be modified 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 ... ';
}
}
It looks perfect.
Answer: wrong. It looks like there's nothing wrong with call dosomething in Main, after all, two methods are in the same class. But look closely, main is static. Static method cannot directly call non-static methods. Can be changed to "System.out.println (" s.dosomething () returns "+ s.dosomething ());". Similarly, static method cannot access non-static instant variable.
9.
Here, the something class file is named Otherthing.java
Class Something {
private static void Main (string[] something_to_do) {
System.out.println ("Do something ...");
}
}
This seems to be obvious.
Answer: Right. No one has ever said that the Java class name must be the same as its filename. But the name of the public class 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: wrong. An error occurs at compile time (error description different JVM has different information, meaning ambiguous x calls, two x matches (just like declaring date directly when you import Java.util and java.sql two packages at the same time). For variables of the parent class, you can use Super.x to specify that the properties of the interface are implicitly public static final by default. So you can make it clear by a.x.
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 mistake is not easy to find.
Answer: wrong. "Interface Rollable extends playable, bounceable" no problem. Interface can inherit multiple interfaces, so this is true. The problem is in the interface rollable "Ball Ball = new Ball (" Pingpang ");". Any interface variable (interface variable, also known as a member variable) declared in interface, is public static final by default. That is to say "Ball Ball = new Ball" ("Pingpang"); is actually "public static final Ball Ball = new Ball (" Pingpang ");". In the play () method of the ball class, "ball = new Ball (" Football ");" Changed the reference of ball, and here ball from rollable interface,rollable interface is public and final, The final object cannot be changed reference. The compiler will therefore be in the "ball = new Ball (" Football "); This shows the error.


Java Programming Questions
1. Now enter n numbers, separated by commas, then select ascending or Descending sort, and press the Submit key on the other page to display what sort of order, as a result, to provide reset
Import 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.nexttoken ();
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);
Asc
for (int i=0;i<s.length;i++) {
System.out.println (Ii[i]);
}
Desc
for (int i= (s.length-1); i>=0;i--) {
System.out.println (Ii[i]);
}
}
}
2. The amount of the conversion, the amount of Arabic numerals converted into Chinese traditional forms such as: (¥1011)-> (1001 pick up one yuan whole) 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 = "0";
public static final String one = "one";
public static final String two = "II";
public static final String THREE = "three";
public static final String FOUR = "Restaurant";
public static final String FIVE = "WU";
public static final String SIX = "land";
public static final String SEVEN = "Qi";
public static final String eight = "ba";
public static final String NINE = "Nine";
public static final String TEN = "Pick up";
public static final String hundred = "Bai";
public static final String thousand = "thousand";
public static final String Ten_thousand = "million";
public static final String hundred_million = "billion";
public static final String YUAN = "Yuan";
public static final String jiao = "Corner";
public static final String FEN = "min";
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.setminimumfractiondigits (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, dot);

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 = new 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));
}

Public 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.substring (i, i + 1)));
}
Collect hundred thousand trillion and so on are the Chinese characters inside only have the unit, add 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 = Moneypatterncursor = = 8? 1:moneypatterncursor + 1;
}

String Fractionpart = cmoneystringbuffer.substring (Cmoneystringbuffer.indexof ("."));
Cmoneystringbuffer.delete (Cmoneystringbuffer.indexof ("."), Cmoneystringbuffer.length ());
while (Cmoneystringbuffer.indexof ("0 pick up")!=-1) {
Cmoneystringbuffer.replace (Cmoneystringbuffer.indexof ("0 pick Up"), Cmoneystringbuffer.indexof ("0 pick up") + 2, ZERO);
}
while (Cmoneystringbuffer.indexof ("0 bai")!=-1) {
Cmoneystringbuffer.replace (Cmoneystringbuffer.indexof ("0 bai"), Cmoneystringbuffer.indexof ("0 bai") + 2, ZERO);
}
while (Cmoneystringbuffer.indexof ("0 thousand")!=-1) {
Cmoneystringbuffer.replace (Cmoneystringbuffer.indexof ("0 thousand"), Cmoneystringbuffer.indexof ("0 thousand") + 2, ZERO);
}
while (Cmoneystringbuffer.indexof ("0")!=-1) {
Cmoneystringbuffer.replace (Cmoneystringbuffer.indexof ("0"), Cmoneystringbuffer.indexof ("0") + 2, TEN_THOUSAND);
}
while (Cmoneystringbuffer.indexof ("0")!=-1) {
Cmoneystringbuffer.replace (Cmoneystringbuffer.indexof ("0"), Cmoneystringbuffer.indexof ("0") + 2, HUNDRED_MILLION );
}
while (Cmoneystringbuffer.indexof ("00")!=-1) {
Cmoneystringbuffer.replace (Cmoneystringbuffer.indexof ("00"), Cmoneystringbuffer.indexof ("00") + 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 Addunitstochinesemoneystring (string moneystr) {
String result;
StringBuffer cmoneystringbuffer = new StringBuffer (MONEYSTR);
int Indexofdot = Cmoneystringbuffer.indexof (DOT);
Cmoneystringbuffer.replace (Indexofdot, Indexofdot + 1, YUAN);
}






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.