-
Notifications
You must be signed in to change notification settings - Fork 0
/
StaticMethod.java
60 lines (56 loc) · 2.12 KB
/
StaticMethod.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Scanner;
class Book {
private static int noOfBooks = 0;
private String title, author;
private int yearOfPublication;
private int edition;
public Book(String title, String author, int yearOfPublication, int edition) {
this.title = title;
this.author = author;
this.yearOfPublication = yearOfPublication;
this.edition = edition;
noOfBooks++;
}
public static void dispNoOfBooks() { //static method which displays the number of books
System.out.println(noOfBooks);
}
public boolean isSameAuthor(Book b) {
return author.equals(b.author);
}
public boolean isPublishedEarlier(Book b) {
return yearOfPublication < b.yearOfPublication;
}
}
public class StaticMethod {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.printf("Book 1\nTitle:");
String title = sc.next();
System.out.printf("Author:");
String author = sc.next();
System.out.printf("Year Of Publicaiton:");
int yearOfPublication = sc.nextInt();
System.out.printf("Edition:");
int edition = sc.nextInt();
Book b1 = new Book(title, author, yearOfPublication, edition);
System.out.printf("Book 2\nTitle:");
title = sc.next();
System.out.printf("Author:");
author = sc.next();
System.out.printf("Year Of Publication:");
yearOfPublication = sc.nextInt();
System.out.printf("Edition:");
edition = sc.nextInt();
Book b2 = new Book(title, author, yearOfPublication, edition);
System.out.println("No of books:");
Book.dispNoOfBooks(); //calling a static method
if(b1.isSameAuthor(b2))
System.out.println("Same Author");
else
System.out.println("Different Authors");
if(b1.isPublishedEarlier(b2))
System.out.println("Book 1 is published earlier");
else
System.out.println("Book 1 is not published earlier");
}
}