Java Pet Practice

Source: Internet
Author: User
Tags stub

First, set a pet's abstract class, put all common attribute methods into the sub-class, for subclasses to inherit the implementation.

Package Backing2;abstract public class Pet {private String name;  Private String sex;  private int age;  private int helthy;  private int happy;  private int hungry;  Private Boolean alive; public abstract void Play ();p ublic abstract void Yiyuango ();p ublic abstract void Youeryuango ();p ublic abstract void Youlec Hanggo ();p ublic abstract void Eat ();p ublic Pet (string name, String sex) {super (); this.name = Name;this.sex = sex;this.age= 1;this.helthy=100;this.happy=80;this.hungry=70; System.out.println ("Pet Name:" +this.name+ "\ n" + "Gender:" +this.sex+ "\ T" + "age" +this.age+ "Health value" +this.helthy+ "Hunger value" + This.hungry);} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} public int getage () {return age;} public void Setage (int.) {this.age = age;} public int gethelthy () {return helthy;} public void sethelthy (int helthy) {this.helthy = Helthy;} public int Gethappy () {return happy;} public void SetHAppy (int happy) {this.happy = happy;} public int gethungry () {return hungry;} public void sethungry (int hungry) {this.hungry = hungry;} public Boolean isAlive () {return alive;} public void Setalive (Boolean alive) {this.alive = Alive;}}

The

Then defines the small class of pet inheritance, the subclass implements all the methods of overriding the parent class, note that the permission modifier of the method cannot be less than the permission modifier of the parent class, and then define a method in the subclass to judge several property values of a pet each time each method is used

Package Backing2;public class Qie extends Pet {public Qie (string name, String sex) {super (name, sex);//TODO Auto-generate D constructor stub} @Overridepublic void Play () {System.out.println ("Little Penguin Bouncing Off"); This.sethappy (Gethappy () +5); check (); SYSTEM.OUT.PRINTLN ("Health Value" +this.gethelthy () + "Mood value" +this.gethappy () + "Hunger Value" +this.gethungry ()); @Overridepublic void Yiyuango () {System.out.println ("The Little Penguin went to the hospital"); This.sethelthy (Gethelthy () +20); check (); SYSTEM.OUT.PRINTLN ("Health Value" +this.gethelthy () + "Mood value" +this.gethappy () + "Hunger Value" +this.gethungry ()); @Overridepublic void Youeryuango () {System.out.println ("The Little Penguin goes to school happily"); This.sethungry (Gethungry () +10); this.sethappy (Gethappy () +5); check (); SYSTEM.OUT.PRINTLN ("Health Value" +this.gethelthy () + "Mood value" +this.gethappy () + "Hunger Value" +this.gethungry ()); @Overridepublic void Youlechanggo () {System.out.println ("Go to the amusement park of the Little Penguin Huanhuan"), This.sethappy (Gethappy () +10); check (); SYSTEM.OUT.PRINTLN ("Health Value" +this.gethelthy () + "Mood value" +this.gethappy () + "Hunger Value" +this.gethungry ()); @Overridepublic void Eat () {System.out.println ("Little Penguin Eats Fish <. )) (≦)); This.sethungry (Gethungry ()-30); SYSTEM.OUT.PRINTLN ("Health Value" +this.gethelthy () + "Mood value" +this.gethappy () + "Hunger Value" +this.gethungry ()); public void Check () {if (This.gethappy () <5| | This.gethungry () >90| |      This.gethelthy () <5) {System.out.println ("Your pet should go to the hospital");    } this.getage (); }}package Backing2;public class Tuzi extends Pet {public Tuzi (string name, String sex) {super (name, sex);//TODO Auto-gene Rated constructor stub} @Overridepublic void Play () {check (); System.out.println ("The Little Bunny is bouncing Around"); This.sethappy (Gethappy () +5); check (); SYSTEM.OUT.PRINTLN ("Health Value" +this.gethelthy () + "Mood value" +this.gethappy () + "Hunger Value" +this.gethungry ()); @Overridepublic void Yiyuango () {check (); System.out.println ("The rabbit is listless to the hospital"); This.sethelthy (Gethelthy () +20); check (); SYSTEM.OUT.PRINTLN ("Health Value" +this.gethelthy () + "Mood value" +this.gethappy () + "Hunger Value" +this.gethungry ()); @Overridepublic void Youeryuango () {check (); System.out.println ("The Little Bunny goes to school happily"); This.sethungry (Gethungry () +10); This.sethappy (Gethappy () +5); check (); System.out.pRINTLN ("Health Value" +this.gethelthy () + "Mood value" +this.gethappy () + "Hunger Value" +this.gethungry ()); @Overridepublic void Youlechanggo () {check (); System.out.println ("The Little bunny fun to go to the playground"); This.sethappy (Gethappy () +10); check (); SYSTEM.OUT.PRINTLN ("Health Value" +this.gethelthy () + "Mood value" +this.gethappy () + "Hunger Value" +this.gethungry ()); @Overridepublic void Eat () {check (); System.out.println ("Bunny eats radish"); This.sethungry (Gethungry ()-30);    SYSTEM.OUT.PRINTLN ("Health Value" +this.gethelthy () + "Mood value" +this.gethappy () + "Hunger Value" +this.gethungry ()); public void Check () {if (This.gethappy () <5| | This.gethungry () >90| |      This.gethelthy () <5) {System.out.println ("Your pet should go to the hospital");    } this.getage (); }}

The

Finally defines a run class in which a static method is defined to output a hint message

Package Backing2;import Java.util.scanner;public class Testpet {public static void main (string[] args) {Scanner s=new Scan NER (system.in); System.out.println ("Please enter the pet you wish to select"); System.out.println ("the Rabbit"); System.out.println ("2--penguin");p et p=null; String S1=s.nextline (), if ("1". Equals (S1)) {System.out.print ("Please enter your pet name"); String S2=s.nextline (); System.out.print ("Please enter your pet's gender"); String s3=s.nextline ();p =new Tuzi (S2,S3);} else if ("2". Equals (S1)) {System.out.print ("Please enter your pet name"); String S2=s.nextline (); System.out.print ("Please enter your pet's gender"); String s3=s.nextline ();p =new Tuzi (S2,S3);} Else{system.out.println ("Other pets are hatching. ");} Boolean Falg=true;while (Falg) {Testpet.tishi (); String S5=s.nextline (), if ("1". Equals (S5)) {P.play ();} else if ("2". Equals (S5)) {P.yiyuango ();} else if ("3". Equals (S5)) {P.youlechanggo ();} else if ("4". Equals (S5)) {p.eat ();} else if ("5". Equals (S5)) {P.youeryuango ();} else if ("Exit". Equals (S5)) {Falg=false;} ELSE{SYSTEM.OUT.PRINTLN ("Please come on request");}}   S.close ();} public static void Tishi () {System.out.println ("friendly communication with pets. ");   System.out.println ("Play with Pets");   System.out.println ("2--accompany Pets to hospital");   System.out.println ("3--accompany pets to the amusement park");   System.out.println ("4--accompany the pet to eat dinner");   System.out.println ("5--Send pets to Kindergarten");      System.out.println ("exit--exit"); }}

  

Java Pet Practice

Related Article

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.