- Use "short" and normal print statements to write a program
Package Net.mindview.util; Public classPrint {/** * not with carriage return * @param s*/ Public Static voidprint (Object s) {System. out. Print (s); } /** * with carriage return * @param s*/ Public Static voidprintln (Object s) {System. out. println (s); }}
Package Net.mindview.operators;import Java.util.date;importStaticnet.mindview.util.print.*; Public classHelloData { Public Static voidMain (string[] args) {print ("Hello, it is"); Print (NewDate ()); System. out. println ("normal way to print"); }}
- Create a class containing the float field and use this class to demonstrate the alias mechanism. (Here too lazy to write, that is, the demo in the int to float on it)
Package net.mindview.operators;classtank{intLevel ;} Public classAssignment { Public Static voidMain (string[] args) {Tank T1=NewTank (); Tank T2=NewTank (); T1.level= -; T2.level= A; System. out. println ("T1.level:"+ T1.level +", T2.level:"+t2.level); T2=T1; System. out. println ("T1.level:"+ T1.level +", T2.level:"+t2.level); T1.level=5; System. out. println ("T1.level:"+ T1.level +", T2.level:"+t2.level); }}
- Create a class that contains a float field and use this class to show the alias mechanism at the time of the method invocation (change char to float)
Package net.mindview.operators;classletter{CharC;} Public classPassobject {Static voidf (letter Y) {y.c='Z'; } Public Static voidMain (string[] args) {Letter x=NewLetter (); X.C='a'; System. out. println ("1:x.c="+x.c); //The reference that is passed when X points tof (x); System. out. println ("1:x.c="+x.c); }}
- Write a program that calculates the speed, and the distance and time that compression is used are constants. Slightly
- Create a class named Dog that contains two strings with: name and says. In the main () method, create two dog objects, one named Spot (which calls ruff! ), and another named Scruffy (whose call is wurf!). Then show their names and calls.
Package net.mindview.operators;classdog{ PublicString name; PublicString says; @Override PublicString toString () {return "Name:"+ This. Name +", Language:"+ This. says; }} Public classDogtest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubDog D1 =NewDog (); Dog D2=NewDog (); D1.name="Spot"; D1.says="ruff!"; D2.name="Scruffy"; D2.says="wurf!"; System. out. println (D1); System. out. println (D2); }}
- On the basis of exercise 5, create a new dog object and assign it to a spot object. Test the = = and Equals () methods to compare the results of all references.
Package net.mindview.operators;classdog{ PublicString name; PublicString says; @Override PublicString toString () {return "Name:"+ This. Name +", Language:"+ This. says; }} Public classDogtest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubDog D1 =NewDog (); Dog D2=NewDog (); D1.name="Spot"; D1.says="ruff!"; D2.name="Scruffy"; D2.says="wurf!"; System. out. println (D1); System. out. println (D2); Dog D3=NewDog (); D3.name="Spot"; System. out. println (D1.name = =d3.name); System. out. println (D1.name.equals (d3.name)); }}
Output Result:
Name: Spot, language: ruff! Name: Scruffy, language: Wurf! true true
This result needs to be specifically described, string is a special reference type, when he is directly assigned to the value of the corresponding reference position is assigned to the string variable, so, two times the result is true. If you assign a value with the new String (), the result will be different.
- Write a program that simulates the result of tossing a coin
Package Net.mindview.operators;import Java.util.random;importStaticnet.mindview.util.print.*; Public classThrowcron { Public Static voidMain (string[] args) {Random num=NewRandom (); intA = Num.nextint ( -); SwitchA2){ Case 0: println ("Front"); Break; Case 1: println ("Reverse"); Break; } }}
- Fadsfas
- Fasfda
- Fasdf
- Fasdfa
- Fasdf
- Fasd
- fdasf
- Fadsf
Java Programming Ideas Fourth Edition Chapter three key exercises