Drag&Drop
unique c , addNewType(). unique ,
RunnerAnonym$1. , - . , .
, . enum, .
/* # 16: : EnumRunner.java */
package chapt06;
enum Shape {
RECTANGLE, SQUARE,
TRIANGLE { //
public double getSquare() { // TRIANGLE
return a*b/2;
}
};
public double a, b;
public void setShape(double a, double b){
if ((a<=0 || b<=0) || a!=b && this == SQUARE)
throw new IllegalArgumentException();
Else
this. a = a;
this. b = b;
}
public double getSquare(){ // RECTANGLE SQUARE return a * b;
}
public String getParameters() {
return "a=" + a + ", b=" + b;
}
}
public class EnumRunner {
public static void main(String[] args) {
int i = 4;
for (Shape f: Shape. values ()) {
f.setShape(3, i--);
System. out. println(f.name()+"-> " + f.getParameters()
+ " = " + f.getSquare());
}
}
}
:
RECTANGLE-> a=3.0, b=4.0 = 12.0
SQUARE-> a=3.0, b=3.0 = 9.0
TRIANGLE-> a=3.0, b=2.0 = 3.0
Shape$1.
6
1. Notepad ( ) , .
2. Payment () , .
3. Account () , (, , ).
4. , , , .
5. Department ( ) , , - .
6. Catalog () , .
|
|
7. , .
8. City () , , , .
9. CD (mp3-) , ,
.
10. Mobile , .
11. ,
, .
12. , .
13. Shop () , , .
14. C C O T
, , .
15. Computer () , , .
16. Park () , , .
17. Cinema () , , .
18. , .
19. , , .
B
4 . .
C
, :
1. (, , , , ). .
2. interface ß abstract class ß class -.
3. interface ß class ß class .
4. interface ß abstract class ß class .
5. interface Mobile ß abstract class Siemens Mobile ß class Model.
6. interface ß abstract class ß class .
7. interface ß class ß class .
8. interface ß class ß class .
9. interface ß abstract class ß class .
10. interface ß class ß class .
11. interface ß abstract class ß class .
|
|
12. interface ß abstract class ß class .
13. interface ß abstract class ß class .
14. interface ß class ß class .
15. interface ß abstract class ß class .
16. interface ß class ß class .
6
6.1.
?
1)
import java.util.*;
package First;
class My{/* */}
2)
package mypack;
import java.util.*;
public class First{/* */}
3)
/* */
package first;
import java.util.*;
class First{/* */}
6.2.
MyInterface ?
1) interface MyInterface{
public int result(int i){ return (i++);}}
2) interface MyInterface{
int result(int i);}
3) public interface MyInterface{
public static int result(int i);}
4) public interface MyInterface{
class MyClass{}}
5) public interface MyInterface{
public final static int i;
public abstract int result(int i);}
6.3.
,
class Owner{
class Inner{
} }
1) new Owner.Inner();
2) Owner. new Inner();
3) new Owner. new Inner();
4) new Owner(). new Inner();
5) Owner.Inner();
6) Owner().Inner().
6.4.
?
abstract class Abstract {
abstract Abstract meth();
}
class Owner {
Abstract meth() {
class Inner extends Abstract {
Abstract meth() {
System.out.print("inner ");
return new Inner();
} }
return new Inner();
}
}
public abstract class Quest4 extends Abstract{
public static void main(String a[]) {
Owner ob = new Owner();
Abstract abs = ob.meth();
abs.meth();
}
}
1) inner;
2) inner inner;
3) inner inner inner;
4) ;
5) .
6.5.
class Quest5 { char A; // 1 void A() {} // 2 class A {} // 3}
?
1) 1;
2) 2;
3) 3;
4) .
2.
Java , , , java.util, java.text, java.net, java.io, java.awt, javax.swing .
- . Java, Java-.
7
Java . char, . Java String, StringBuilder StringBuffer, java.lang, . final, . , Formatter, Pattern, Matcher .
String
, new ( ), String. String , - , . String , .
|
|
String , : String(), String(String str), String(byte asciichar[]), String(char[] unicodechar), String(StringBuffer sbuf), String(StringBuilder sbuild) . String char, byte . ,
new String(str.getChars(), "UTF-8"),
str Unicode, , . Java , , String, . , String , , new , :
String s1 = "sun.com";
String s2 = new String("sun.com");
String :
String concat(String s) + ;
boolean equals(Object ob) equalsIgnoreCase(String s) ;
int compareTo(String s) compareToIgnoreCase(String s) . . , equals() true;
boolean contentEquals(StringBuffer ob)
StringBuffer;
String substring(int n, int m) m-n, n. ;
String substring(int n) , n;
int length() ;
int indexOf(char ch) ;
static String valueOf( ) ;
String toUpperCase()/toLowerCase() / ;
String replace(char 1, char 2) ;
String intern() ;
String trim() ;
char charAt(int position) ( );
boolean isEmpty() true, 0;
byte[] getBytes(), getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) ;
static String format(String format, Object... args), format(Locale l, String format, Object... args) , , .;
String[] split(String regex), split(String regex, int limit) () .
, , String.
String .
/* # 1: : DemoString.java */
package chapt07;
public class DemoString {
static int i;
public static void main(String[] args) {
char s[] = { 'J', 'a', 'v', 'a' }; //
//
String str = new String(s); // str="Java"
|
|
if (!str.isEmpty()) {
i = str.length(); // i=4
str = str.toUpperCase(); // str="JAVA"
String num = String.valueOf(6); // num="6"
num = str.concat("-" + num); // num="JAVA-6"
char ch = str.charAt(2); // ch='V'
i = str.lastIndexOf('A'); // i=3 (-1 )
num = num.replace("6", "SE"); // num="JAVA-SE"
str.substring(0, 4).toLowerCase(); // java
str = num + "-6"; // str=JAVA-SE-6
String[] arr = str.split("-");
for (String ss: arr)
System.out.println(ss);
} else { System. out. println("String is empty!");
}
}
}
:
JAVA
SE
String, , String. String , .. . str.
/* # 2: : RefString.java */
package chapt07;
public class RefString {
public static void changeStr(String s) {
s.concat(" Microsystems"); //
}
public static void main(String[] args) {
String str = new String("Sun");
changeStr (str);
System. out. println(str);
}
}
:
Sun
, , . , concat(String s) .
equals(), String hashCode(), - .
/* # 3: : EqualStrings.java */
package chapt07;
public class EqualStrings {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Java";
String s3 = new String("Java");
System. out. println(s1 + "==" + s2 +
": " + (s1 == s2)); // true
System. out. println(s1 + "==" + s3 +
": " + (s1 == s3)); // false
System.out.println(s1 + " equals " + s2 + ": "
+ s1.equals(s2)); // true
System.out.println(s1 + " equals " + s3 + ": "
+ s1.equals(s3)); // true
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println(s3.hashCode());
}
}
, , :
Java==Java: true
Java==Java: false
Java equals Java: true
Java equals Java: true
, , - .
.. Java , , s2 , . s2 , s1 . s3 , , .
intern().
// # 4: intern(): DemoIntern.java
package chapt07;
public class DemoIntern {
public static void main(String[] args) {
String s1 = "Java"; //
String s2 = new String("Java");
System. out. println(s1 == s2); // false
s2 = s2.intern();
System. out. println(s1 == s2); // true
}
}
s1 , . intern() s2 , .
.
// # 5: : SortArray.java
package chapt07;
public class SortArray {
public static void main(String[] args) {
String a[] = {" Alena", "Alice ", " alina",
" albina", " Anastasya", " ALLA ", "AnnA "};
for (int j = 0; j < a.length; j++)
|
|
a[j] = a[j].trim().toLowerCase();
for (int j = 0; j < a.length - 1; j++)
for (int i = j + 1; i < a.length; i++)
if (a[i].compareTo(a[j]) < 0) {
String temp = a[j];
a[j] = a[i];
a[i] = temp;
}
int i = -1;
while (++i < a.length)
System. out. print(a[i] + " ");
}
}
trim() . compareTo() Unicode.
StringBuilder StringBuffer
StringBuilder StringBuffer
String, , , StringBuilder
StringBuffer .
StringBuilder StringBuffer . 1.5.0 (, ) StringBuilder, , .
StringBuffer, StringBuilder String . StringBuffer ( StringBuilder) String . String toString() String.
:
void setLength(int n) ;
void ensureCapacity(int minimum) ;
int capacity() ;
StringBuffer append( ) , , , ;
StringBuffer insert( ) , ;
StringBuffer deleteCharAt(int index) ;
StringBuffer delete(int start, int end) ;
StringBuffer reverse() .
, String, replace(), substring(), charAt(), length(), getChars(), indexOf() .
/* # 6: StringBuffer: DemoStringBuffer.java */
package chapt07;
public class DemoStringBuffer {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System. out. println(" ->" + sb.length());
System. out. println(" ->" + sb.capacity());
// sb = "Java"; // , String
sb.append("Java");
System. out. println(" ->" + sb);
System. out. println(" ->" + sb.length());
System. out. println(" ->" + sb.capacity());
System. out. println(" ->" + sb.reverse());
}
}
:
->0
->16
->Java
->4
->16
->avaJ
StringBuffer (16 ), , , . . StringBuffer , , . reverse() .
, StringBuffer, , ,
String, StringBuffer.
/* # 7: StringBuffer: RefStringBuffer.java */
package chapt07;
public class RefStringBuffer {
public static void changeStr(StringBuffer s) {
s.append(" Microsystems");
}
public static void main(String[] args) {
StringBuffer str = new StringBuffer("Sun");
changeStr (str);
System. out. println(str);
}
}
:
Sun Microsystems
StringBuffer changeStr() , .
StringBuffer equals()
hashCode(), .. , - ,
Object.
/* # 8: StringBuffer -:
EqualsStringBuffer.java */
package chapt07;
public class EqualsStringBuffer {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("Sun");
StringBuffer sb2 = new StringBuffer("Sun");
System. out. print(sb1.equals(sb2));
System. out. print(sb1.hashCode() ==
sb2.hashCode());
}
}
false.
java.util.Formatter. , , , .
Formatter format(), Formatter. PrintStream PrintWriter. , printf() format(), , format() Formatter. , printf() Fomatter printf() .
Formatter
. , . Formatter . Formatter .
. :
Formatter()
Formatter(Appendable buf)