Advertisement
Gamerkin

работа первая java

Sep 1st, 2023
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. package Lab1;
  2.  
  3. public class Dog {
  4.     private String name;
  5.     private int age;
  6.     public Dog(String n, int a){
  7.         name = n;
  8.         age = a;
  9.     }
  10.     public Dog(String n){
  11.         name = n;
  12.         age = 0;
  13.     }
  14.     public Dog(){
  15.         name = "Sam";
  16.         age = 0;
  17.     }
  18.     public void setAge(int age){
  19.         this.age = age;
  20.     }
  21.     public void setName(String name){
  22.         this.name = name;
  23.     }
  24.     public String getName(String name){
  25.         return name;
  26.     }
  27.     public String toString(){
  28.         return this.name + ", age " + this.age;
  29.     }
  30.     public void intoHumanAge(){
  31.         System.out.println(name+"'s age in human years is " + age * 7 + " years");
  32.     }
  33. }
  34.  
  35.  
  36. package Lab1;
  37.  
  38. public class TestDog {
  39.     public static void main(String[] args){
  40.         Dog d1 = new Dog("Mike", 2);
  41.         Dog d2 = new Dog("Helen", 7);
  42.         Dog d3 = new Dog("Bob"); d3.setAge(1);
  43.         System.out.println(d1);
  44.         d1.intoHumanAge();
  45.         d2.intoHumanAge();
  46.         d3.intoHumanAge();
  47.     }
  48. }
  49.  
  50.  
  51. package Lab1;
  52. public class Ball {
  53.     private String type;
  54.     private String color;
  55.     public Ball(String t, String c){
  56.         type = t;
  57.         color = c;
  58.     }
  59.     public Ball(String c){
  60.         type = "Volleyball";
  61.         color = c;
  62.     }
  63.     public Ball(){
  64.         type = "Football";
  65.         color = "blue";
  66.     }
  67.     public void setColor(String color){
  68.         this.color = color;
  69.     }
  70.     public void setType(String type){
  71.         this.type = type;
  72.     }
  73.     public String toString(){
  74.         return "The " + this.type + " is " + this.color + " color";
  75.     }
  76.     public void showStatus(){
  77.         System.out.println("The " + type + " is " + color + " color");
  78.     }
  79. }
  80.  
  81.  
  82. package Lab1;
  83.  
  84. public class TestBall {
  85.     public static void main(String[] args){
  86.         Ball b1 = new Ball("Basketball", "Orange");
  87.         Ball b2 = new Ball("Blue");
  88.         Ball b3 = new Ball("Rugby", "Yellow");
  89.         b2.setType("Volleyball");
  90.         b1.showStatus();
  91.         b2.showStatus();
  92.         b3.showStatus();
  93.  
  94.     }
  95. }
  96.  
  97.  
  98. package Lab1;
  99.  
  100. public class Book {
  101.     private String name;
  102.     private int pages;
  103.     public Book(String n, int p){
  104.         name = n;
  105.         pages = p;
  106.     }
  107.     public Book(String n){
  108.         name = n;
  109.         pages = 1;
  110.     }
  111.     public Book(){
  112.         name = "Alphabet";
  113.         pages = 1;
  114.     }
  115.     public void setPages(int pages){
  116.         this.pages = pages;
  117.     }
  118.     public void setName(String name){
  119.         this.name = name;
  120.     }
  121.     public String getName(String name){
  122.         return name;
  123.     }
  124.     public String toString(){
  125.         return "In " + this.name + " " + this.pages + " pages";
  126.     }
  127.     public void howManySheets(){
  128.         System.out.println("In " + name + " " + (pages / 2) + " sheets");
  129.     }
  130. }
  131.  
  132.  
  133. package Lab1;
  134.  
  135. public class TestBook {
  136.     public static void main(String[] args) {
  137.         Book b1 = new Book("Kolobok", 30);
  138.         Book b2 = new Book("Harry Potter");
  139.         Book b3 = new Book("Iron Man", 100);
  140.         b2.setPages(346);
  141.         System.out.println(b1);
  142.         b1.howManySheets();
  143.         b2.howManySheets();
  144.         b3.howManySheets();
  145.     }
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement