Advertisement
Guest User

the inheritance exc1

a guest
Dec 11th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. //the Testet class
  2. //the main
  3. import java.util.Scanner;
  4. public class Tester {
  5.  
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub
  8.         Scanner s= new Scanner(System.in);
  9.         String name,color,favFood;
  10.         int whiskers,battelsNum;
  11.         Cat c1=new Cat("Tom","Grey",50);
  12.         StreetCat c2=new StreetCat("Mini","Yellow",40,3);
  13.         SiamiCat c3=new SiamiCat("Boosy","White",30,"fish");
  14.         //receiving data for a streetCat
  15.         System.out.println("Enter a cat's name");
  16.         name=s.next();
  17.         System.out.println("Enter a cat's color");
  18.         color=s.next();
  19.         System.out.println("Enter the cat's whiskers length (cm)");
  20.         whiskers=s.nextInt();
  21.         System.out.println("Enter how many times the cat got envolved in a battel");
  22.         battelsNum=s.nextInt();
  23.         StreetCat c4=new StreetCat(name,color,whiskers,battelsNum);
  24.         //recieving data for a siamiCat
  25.         System.out.println("Enter a cat's name");
  26.         name=s.next();
  27.         System.out.println("Enter a cat's color");
  28.         color=s.next();
  29.         System.out.println("Enter the cat's whiskers length (cm)");
  30.         whiskers=s.nextInt();
  31.         System.out.println("enter the cat's favourite food");
  32.         favFood=s.next();
  33.         SiamiCat c5=new SiamiCat(name,color,whiskers,favFood);
  34.         System.out.println(c1);
  35.         System.out.println(c2);
  36.         System.out.println(c3);
  37.         System.out.println(c4);
  38.         System.out.println(c5);
  39.        
  40.     }
  41.  
  42. }
  43. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  44. ///the Cat class
  45. //////////////////////////////
  46.  
  47. public class Cat {
  48.     protected String _name,_color;
  49.     protected int _whiskers;//in centimeters
  50.     //constructor for all fields
  51.     public Cat(String _name, String _color, int _whiskers) {
  52.         super();
  53.         this._name = _name;
  54.         this._color = _color;
  55.         this._whiskers = _whiskers;
  56.     }
  57.     //toString for all fields
  58.     @Override
  59.     public String toString() {
  60.         return "Cat [_name=" + _name + ", _color=" + _color + ", _whiskers=" + _whiskers + "]";
  61.     }
  62. }
  63. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  64. //the StreetCat class
  65. //////////////////////////////////////
  66.  
  67. public class StreetCat extends Cat {
  68.     protected int _battelsNum;
  69.     //constructor for all fields
  70.     public StreetCat(String _name, String _color, int _whiskers,int _battelsNum) {
  71.         super(_name, _color, _whiskers);
  72.         this._battelsNum=_battelsNum;
  73.         // TODO Auto-generated constructor stub
  74.        
  75.     }
  76.     //toString for all fields
  77.     @Override
  78.     public String toString() {
  79.        
  80.         return super.toString()+ "StreetCat [_battelsNum=" + _battelsNum + "]";
  81.     }
  82.  
  83. }
  84. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  85. ///the SiamiCat class
  86. ///////////////////////////////////////////////
  87.  
  88. public class SiamiCat extends Cat {
  89.     protected String _favFood;
  90.     //constructor for all fields
  91.     public SiamiCat(String _name, String _color, int _whiskers,String _favFood) {
  92.         super(_name, _color, _whiskers);
  93.         this._favFood=_favFood;
  94.         // TODO Auto-generated constructor stub
  95.     }
  96.     //toStrin for all fields
  97.     @Override
  98.     public String toString() {
  99.        
  100.         return super.toString()+"SiamiCat [_favFood=" + _favFood + "]";
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement