Advertisement
malixds_

prac1

Sep 12th, 2022
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. public class Book {
  2.     private int year;
  3.     private String name;
  4.     public Book(String n, int y){
  5.         year = y;
  6.         name = n;
  7.     }
  8.     public Book(String n){
  9.         name = n;
  10.         year = 0;
  11.     }
  12.     public Book(){
  13.         name = "Demian";
  14.         year = 0;
  15.     }
  16.     public void setName(String name){
  17.         this.name = name;
  18.     }
  19.     public void setYear(int year){
  20.         this.year = year;
  21.     }
  22.     public String getName(){
  23.         return name;
  24.     }
  25.     public int getYear(){
  26.         return year;
  27.     }
  28.     public String toString(){
  29.         return this.name+"'s age is "+this.year;
  30.     }
  31. }
  32.  
  33.  
  34.  
  35. public class TestBook {
  36.     public static void main(String[] args) {
  37.         Book book1 = new Book("Night in the Lissabone", 1941);
  38.         Book book2 = new Book("Three comrades");
  39.         Book book3 = new Book();
  40.  
  41.         book3.setName("Harry Potter");
  42.         book3.setYear(1997);
  43.         book3.getName();
  44.         System.out.println(book3);
  45.  
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement