主题:Thinking in Java(4th)答案免費分享
// TIJ4 Chapter Object, Exericise 1, page 89
// object/PrimitiveTest.java
// Create a class containing an int and a char that are not intitialized
// and print their values to verify that Java performs default initialization.
public class PrimitiveTest {
static int i;
static char c;
public static void main(String[] args) {
System.out.println("int = " + i);
System.out.println("char = " + c);
}
}
// TIJ4 Chapter Object, Exericise 2, page 89
// object/HelloWorld.java
// Following the HelloDate.java example in this chapter, create a "hello, world"
// program that simply displays that statement.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
// TIJ4 Chapter Object, Exericise 3, page 90
// object/ATNTest.java
// Find the code fragments involving ATypeName and turn them into a program
// that compiles and runs.
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.71828;
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
public class DataOnlyTest {
public static void main(String[] args) {
class DataOnly {
int i;
double d;
boolean b;
void show() {
System.out.println(i);
System.out.println(d);
System.out.println(b);
}
}
DataOnly data = new DataOnly();
data.i = 3;
data.d = 2.71828;
data.b = false;
data.show();
}
}
// object/DOTest2.java
// TIJ4 Chapter Object, Exercise 5, page 90
// Modify the previous exercise so that the values of the data in DataOnly are
// assigned to and printed in main().
public class DOTest2 {
public static void main(String[] args) {
class DataOnly {
int i;
double d;
boolean b;
void show() {
System.out.println(i);
System.out.println(d);
System.out.println(b);
}
}
DataOnly data = new DataOnly();
data.i = 234;
data.d = 2.1234545;
data.b = true;
data.show();
}
}
// object/StorageTest.java
// TIJ4 Chapter Object, Exercise 6, page 90
// Write a program that includes and calls the storage() method defined as a
// code fragment in this chapter.
public class StorageTest {
public static void main(String[] args) {
class StoreStuff {
int storage(String s) {
return s.length() * 2;
}
}
StoreStuff x = new StoreStuff();
System.out.println(x.storage("hi"));
}
}
// object/ITest.java
// TIJ4 Chapter Object, Exercise 7, page 90
// Turn the Incrementable code fragments into a working program.
class StaticTest {
static int i = 47;
}
class Incrementable {
static void increment() { StaticTest.i++; }
}
public class ITest {
public static void main(String[] args) {
System.out.println("StaticTest.i= " + StaticTest.i);
StaticTest st1 = new StaticTest();
StaticTest st2 = new StaticTest();
System.out.println("st1.i= " + st1.i);
System.out.println("st2.i= " + st2.i);
Incrementable sf = new Incrementable();
sf.increment();
System.out.println("After sf.increment() called: ");
System.out.println("st1.i = " + st1.i);
System.out.println("st2.i = " + st2.i);
Incrementable.increment();
System.out.println("After Incrementable.increment called: ");
System.out.println("st1.i = " + st1.i);
System.out.println("st2.i = " + st2.i);
}
}
// object/PrimitiveTest.java
// Create a class containing an int and a char that are not intitialized
// and print their values to verify that Java performs default initialization.
public class PrimitiveTest {
static int i;
static char c;
public static void main(String[] args) {
System.out.println("int = " + i);
System.out.println("char = " + c);
}
}
// TIJ4 Chapter Object, Exericise 2, page 89
// object/HelloWorld.java
// Following the HelloDate.java example in this chapter, create a "hello, world"
// program that simply displays that statement.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
// TIJ4 Chapter Object, Exericise 3, page 90
// object/ATNTest.java
// Find the code fragments involving ATypeName and turn them into a program
// that compiles and runs.
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.71828;
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
public class DataOnlyTest {
public static void main(String[] args) {
class DataOnly {
int i;
double d;
boolean b;
void show() {
System.out.println(i);
System.out.println(d);
System.out.println(b);
}
}
DataOnly data = new DataOnly();
data.i = 3;
data.d = 2.71828;
data.b = false;
data.show();
}
}
// object/DOTest2.java
// TIJ4 Chapter Object, Exercise 5, page 90
// Modify the previous exercise so that the values of the data in DataOnly are
// assigned to and printed in main().
public class DOTest2 {
public static void main(String[] args) {
class DataOnly {
int i;
double d;
boolean b;
void show() {
System.out.println(i);
System.out.println(d);
System.out.println(b);
}
}
DataOnly data = new DataOnly();
data.i = 234;
data.d = 2.1234545;
data.b = true;
data.show();
}
}
// object/StorageTest.java
// TIJ4 Chapter Object, Exercise 6, page 90
// Write a program that includes and calls the storage() method defined as a
// code fragment in this chapter.
public class StorageTest {
public static void main(String[] args) {
class StoreStuff {
int storage(String s) {
return s.length() * 2;
}
}
StoreStuff x = new StoreStuff();
System.out.println(x.storage("hi"));
}
}
// object/ITest.java
// TIJ4 Chapter Object, Exercise 7, page 90
// Turn the Incrementable code fragments into a working program.
class StaticTest {
static int i = 47;
}
class Incrementable {
static void increment() { StaticTest.i++; }
}
public class ITest {
public static void main(String[] args) {
System.out.println("StaticTest.i= " + StaticTest.i);
StaticTest st1 = new StaticTest();
StaticTest st2 = new StaticTest();
System.out.println("st1.i= " + st1.i);
System.out.println("st2.i= " + st2.i);
Incrementable sf = new Incrementable();
sf.increment();
System.out.println("After sf.increment() called: ");
System.out.println("st1.i = " + st1.i);
System.out.println("st2.i = " + st2.i);
Incrementable.increment();
System.out.println("After Incrementable.increment called: ");
System.out.println("st1.i = " + st1.i);
System.out.println("st2.i = " + st2.i);
}
}