1、 ATypeName a = new ATypeName(); a.i = 3; a.d = 2.71828; a.b = false; a.show();练习4:public class DataOnlyTest class DataOnly DataOnly data = new DataOnly(); data.i = 3; data.d = 2.71828; data.b = false; data.show();练习5:public class DOTest2 data.i = 234; data.d = 2.1234545; data.b = true;练习6:public cla
2、ss StorageTest class StoreStuff int storage(String s) return s.length() * 2; StoreStuff x = new StoreStuff(); System.out.println(x.storage(hi);练习7:class StaticTest static int i = 47;class Incrementable static void increment() StaticTest.i+;public class ITest System.out.println(StaticTest.i= + Static
3、Test.i); StaticTest st1 = new StaticTest(); StaticTest st2 = new StaticTest();st1.i= + st1.i);st2.i= + st2.i); Incrementable sf = new Incrementable(); sf.increment();After sf.increment() called: st1.i = st2.i = Incrementable.increment();After Incrementable.increment called:练习8:public class OneStatic
4、Test StaticTest st1 = new StaticTest(); StaticTest st2 = new StaticTest(); Incrementable.increment();After Incrementable.increment() called: st1.i = 3;After st1.i = 3, Create another StaticTest, st3. StaticTest st3 = new StaticTest();st3.i = + st3.i);练习9:public class AutoboxTest boolean b = false; c
5、har 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;boolean b = + b); Boolean B = + B); Character C = c;char c = Character C = + C); Byte T = t;byte t = + t);Byte T = + T); Short S = s;short s = + s);Short S = + S); Integer I = i;int i = Inte
6、ger I = + I); Long L = l;long l = + l);Long L = + L); Float F = f;float f = + f);Float F = + F); Double D = d;double d = + d);Double D = + D);练习10:public class CommandArgTest args0 = + args0);args1 = + args1);args2 = + args2);练习11:public class Rainbow AllTheColorsOfTheRainbow atc = new AllTheColorsO
7、fTheRainbow();atc.anIntegerRepresentingColors = + atc.anIntegerRepresentingColors); atc.changeColor(7); atc.changeTheHueOfTheColor(77);After color change, atc.anIntegerRepresentingColors = atc.hue = + atc.hue);class AllTheColorsOfTheRainbow int anIntegerRepresentingColors = 0; int hue = 0; void chan
8、geTheHueOfTheColor(int newHue) hue = newHue; int changeColor(int newColor) return anIntegerRepresentingColors = newColor;练习12:public class DocTest /* Entry poing to class & application. * param args array of string arguments * throws exceptions No exceptions thrown */Hello, its: System.out.println(n
9、ew Date();练习13-1:public class Documentation1 /* A field comment */ public int i; /* A method comment */ public void f() 2:public class Documentation2 Date d = new Date(); void showDate() Date = 3:public class Documentation3 Date d = new Date();d = 练习14:public class Documentation4 public int i = 2;pr
10、ivate int j = 3;public static void main(String args) Date d = new Date();练习15:public class HelloDocTest 练习16:class Tree int height; Tree() Planting a seedling height = 0;Tree(int initialHeight) height = initialHeight;Creating new tree that is + height + feet tallvoid info() Tree is void info(String
11、s) System.out.println(s + : Tree is public class Overloading for(int i = 0; i 5; i+) Tree t = new Tree(i); t.info(); t.info(overloading method / Overloaded constructor: new Tree();第三章public class PrintTest print(Hello, from short form. P.rintln(Hello from greggordon form.Hello from long form.class T
12、ube float level;public class Assign Tube t1 = new Tube(); Tube t2 = new Tube(); t1.level = 0.9f; t2.level = 0.47f;1: t1.level: + t1.level + , t2.level: + t2.level); t1 = t2;2: t1.level = 0.27f;3:class Box float a;public class PassObject2 static void f(Box y) y.a = 2.71828f; Box x = new Box(); x.a =
13、3.1416f; x.a = + x.a); f(x);class VelocityCalculator static float velocity (float d, float t) if(t = 0) return 0f; else return d/t; public class VelocityTester float d = 565.3f; float t = 3.6f;Distance:Time: float v = VelocityCalculator.velocity(d, t);Velocity: + v);class Dog String name; String say
14、s; void setName(String n) name = n; void setSays(String s) says = s; void showName() P.rintln(name); void speak() P.rintln(says);public class DogTest Dog spot = new Dog(); spot.setName(Spot spot.setSays(Ruff! Dog scruffy = new Dog(); scruffy.setName(Scruffy scruffy.setSays(Wurf! spot.showName(); spo
15、t.speak(); scruffy.showName(); scruffy.speak();public class DogCompare Dog butch = new Dog(); butch.setName(Butch butch.setSays(Hello! butch.showName(); butch.speak();Comparison:spot = butch: + (spot = butch);spot.equals(butch): + spot.equals(butch);butch.equals(spot): + butch.equals(spot);Now assign: spot = butch spot = butch;Compare again: + (spot =
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1