标签:mystra java java编程思想 第2章 练习
//: object/HelloDate.java
/**
* Default initialize class.
* @author C.L.Wang
* @author Caroline Wendy
* @version 1.0
*/
public class Main extends ShowProperties {
/**
* Entrv point to class & application.
* @param args array of string arguments
* @throws java.lang.Exception No exceptions thrown
*/
public static void main(String[] args) {
DefaultInit di = new DefaultInit();
System.out.println("default int = " + di.i + ", default char = " + di.c);
}
/**
* Output:
* default int = 0, default char =
*///:~
}
//: object/HelloDate.java
/**
* Say Hello World
* @author C.L.Wang
* @author Caroline Wendy
* @version 1.0
*/
public class Main extends ShowProperties {
/**
* Entrv point to class & application.
* @param args array of string arguments
* @throws java.lang.Exception No exceptions thrown
*/
public static void main(String[] args) {
System.out.println("Hello, World");
}
/**
* Output:
* Hello, World
*///:~
}
public class ATNTest {
public static void main(String[] args) {
class ATypeName {
int i;
double d;
boolean b;
void show() {
System.out.println(i);
System.out.println(d);
System.out.println(b);
}
}
ATypeName a = new ATypeName();
a.i = 3;
a.d = 2.7;
a.b = false;
a.show();
}
}
//: object/DataOnlyTest.java
// TIJ4 Chapter Object Exercise 4 page 90
// Turn the DataOnly code fragments into a program that compiles and runs
class DataOnly {
int i;
double d;
boolean b;
void show() {
System.out.println(i);
System.out.println(d);
System.out.println(b);
}
}
public class DataOnlyTest {
public static void main(String[] args) {
DataOnly data = new DataOnly();
data.i = 47;
data.d = 1.1;
data.b = false;
data.show();
}
}
/**
* Output:
* 47
* 1.1
* false
*///:~
//: StorageTest.java
/**
* 存储字节数
* Created by wangchenlong on 15/7/6.
*/
public class StorageTest {
/**
* 字符串的字节数
* @param s 字符串
* @return 字节数
*/
public static int storage(String s) {
return s.length()*2;
}
public static void main(String[] args) {
String s = "Caroline";
System.out.println(s + "字节数占用: " + storage(s));
}
/**
* Output:
* Caroline字节数占用: 16
*///:~
}
//: IncrementableTest.java
/**
* 静态变量测试
* Created by wangchenlong on 15/7/6.
*/
class StaticTest {
static int i = 47;
}
class Incrementable {
static void increment() {
StaticTest.i++;
}
}
public class IncrementableTest {
public static void main(String[] args) {
Incrementable.increment();
System.out.println("数字变为" + StaticTest.i);
}
}
/**
* output:
* 数字变为48
*///:~
//: OneStaticTest.java
/**
* static变量单实例测试
* Created by C.L.Wang on 15/7/6.
*/
public class OneStaticTest {
public static void main(String[] args) {
StaticTest s1 = new StaticTest();
StaticTest s2 = new StaticTest();
//noinspection AccessStaticViaInstance
System.out.println("s1.i = " + s1.i + ", s2.i = " + s2.i);
Incrementable.increment();
//noinspection AccessStaticViaInstance
System.out.println("after increment: " + "s1.i = " + s1.i + ", s2.i = " + s2.i);
}
}
/**
* output:
* s1.i = 47, s2.i = 47
* after increment: s1.i = 48, s2.i = 48
*///:~
//: AutoBoxTest.java
/**
* 基本类型和包装器类型
* Created by C.L.Wang on 15/7/6.
*/
public class AutoBoxTest {
public static void main(String[] args) {
boolean b = false;
char c = ‘x‘;
byte t = 8;
short s = 16;
int i = 32;
long l = 64;
float f = 0.32f;
double d = 0.64;
Boolean B = b;
System.out.println("boolean b = " + b);
System.out.println("Boolean B = " + B);
Character C = c;
System.out.println("char c = " + c);
System.out.println("Character C = " + C);
Byte T = t;
System.out.println("byte t = " + t);
System.out.println("Byte T = " + T);
Short S = s;
System.out.println("short s = " + s);
System.out.println("Short S = " + S);
Integer I = i;
System.out.println("int i = " + i);
System.out.println("Integer I = " + I);
Long L = l;
System.out.println("long l = " + l);
System.out.println("Long L = " + L);
Float F = f;
System.out.println("float f = " + f);
System.out.println("Float F = " + F);
Double D = d;
System.out.println("double d = " + d);
System.out.println("Double D = " + D);
}
}
/**
* Output:
* ...
*///:~
//: CommentLineTest.java
/**
* 命令行参数
* Created by C.L.Wang on 15/7/6.
*/
public class CommandLineTest {
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("参数不足3个");
return;
}
String result = "";
for (String s : args) {
result += s + " ";
}
System.out.println("参数: " + result);
}
}
/**
* Output:
* 参数: caroline wendy girl
*///:~
//:AllTheColorsOfTheRainbow
/**
* 编码风格测试
* Created by C.L.Wang on 15/7/6.
*/
public class RainbowTest {
public static void main(String[] args) {
AllTheColorOfTheRainbow atc = new AllTheColorOfTheRainbow();
atc.changeTheHusOfTheColor(16);
System.out.println("theHueOfTheColor = " + atc.theHueOfTheColor);
System.out.println("anIntegerRepresentingColors = " + atc.changeAnIntegerRepresentingColors(32));
}
}
class AllTheColorOfTheRainbow {
int anIntegerRepresentingColors;
int theHueOfTheColor;
void changeTheHusOfTheColor(int newHue) {
theHueOfTheColor = newHue;
}
int changeAnIntegerRepresentingColors(int theHueOfTheColor) {
return anIntegerRepresentingColors = theHueOfTheColor;
}
}
/**
* Output:
* theHueOfTheColor = 16
* anIntegerRepresentingColors = 32
*///:~
参考: http://blog.csdn.net/caroline_wendy/article/details/46779719
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:mystra java java编程思想 第2章 练习
原文地址:http://blog.csdn.net/caroline_wendy/article/details/46780623