TsetsoP

Cat and lion; Constructor

Oct 5th, 2021 (edited)
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. import java.sql.SQLOutput;
  2.  
  3. public class Cat {
  4.     private String name;
  5.     private String color;
  6.     private int years;
  7.  
  8.     //default constructor
  9.     public Cat(){
  10.         this.name = "Tom";
  11.         this.color = "gray";
  12.         this.years = 2;
  13.  
  14.     }
  15.     //Constructor with parametres
  16.     public Cat(String nameLion, String colorLion, int yearsLion){
  17.         this.name = nameLion;
  18.         this.color = colorLion;
  19.         this.years = yearsLion;
  20.  
  21.     }
  22.  
  23.     static void foodCat( ){
  24.         System.out.println("The cat eats five times a day.");
  25.     }
  26.     static void sleepCat( ){
  27.         System.out.println("The cat sleeps 12-16 hours a day.");
  28.     }
  29.  
  30.     static void foodLion( ){
  31.         System.out.println("The lion eats around 6kg every day.");
  32.     }
  33.     static void sleepLion( ){
  34.         System.out.println("The lion spends between 16 and 20 hours each day resting and sleeping.");
  35.     }
  36.  
  37.     public static void main(String[] args) {
  38.         Cat cat1 = new Cat();
  39.         foodCat();
  40.         sleepCat();
  41.         System.out.println("Cat's name is: " +  cat1.name + "\n" + "Cat's color is: " + cat1.color + "\n" + "Cat's years are: " + cat1.years);
  42.         System.out.println();
  43.  
  44.         Cat lion = new Cat("Jack", "orange", 5 );
  45.         foodLion();
  46.         sleepLion();
  47.         System.out.println("Lion's name is: " +  lion.name + "\n" + "Lion's color is: " + lion.color + "\n" + "Lion's years are: " + lion.years);
  48.  
  49.  
  50.     }
  51. }
Add Comment
Please, Sign In to add comment