Java Programming Ideas Fourth Edition Chapter three key exercises

Source: Internet
Author: User
Tags stub

  1. 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"); }}

  2. 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); }}

  3. 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); }}

  4. Write a program that calculates the speed, and the distance and time that compression is used are constants. Slightly
  5. 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); }}

  6. 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.

  7. 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; }    }}

  8. Fadsfas
  9. Fasfda
  10. Fasdf
  11. Fasdfa
  12. Fasdf
  13. Fasd
  14. fdasf
  15. Fadsf

Java Programming Ideas Fourth Edition Chapter three key exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.