Title
import java.util.Random;
public class Book {
public String title;
public static int bookCount;
private final Date createDate;
public Book(String title){
this.title = title;
this.createDate = new Date();
}
public int generateID(){
Random rand = new Random();
int Id = rand.nextInt(100);
bookCount++;
return Id;
}
public int getBookCount(){
return bookCount;
}
// Overwrites default toString()
public String toString(){
return("Title of book: " + title);
}
public Long shelfLife(){
Date currentDate = new Date();
long difference = Math.abs(this.createDate.getTime() - currentDate.getTime());
return(difference);
}
public static void tester(){
Book book1 = new Book("All about turtles");
Book book2 = new Book("All about birds");
System.out.println("ID for book1: " + book1.generateID() + ", " + book1.toString());
System.out.println("ID for book2: " + book2.generateID() + ", " + book2.toString());
System.out.println(bookCount);
}
public static void main(String[] args){
tester();
}
}
Book.main(null);
public class Novel extends Book {
public String author;
public Novel(String title){
super(title);
}
public String getAuthor() {
return author;
}
public void setAuthor(String newAuthor) {
this.author = newAuthor;
}0
public static void main(String[] args){
try{
Novel novel1 = new Novel("Angels and Demons");
Novel novel2 = new Novel("Harry Potter");
novel1.setAuthor("Hetvi");
novel2.setAuthor("J.K. Rowling");
Thread.sleep(1000);
System.out.println(novel1.getAuthor());
System.out.println(novel2.getAuthor());
System.out.println(novel1.shelfLife());
System.out.println(novel2.shelfLife());
}
catch (Exception e){
System.out.println(e);
}
}
}
Novel.main(null);
public class Textbook extends Book {
public String author;
public Textbook(String title){
super(title);
}
public String getAuthor() {
return author;
}
public void setAuthor(String newAuthor) {
this.author = newAuthor;
}
public static void main(String[] args){
try{
Textbook textbook1 = new Textbook("Turtles are the Best In the World");
Textbook textbook2 = new Textbook("How to Write Harry Potter");
textbook1.setAuthor("Nathan Shih");
textbook2.setAuthor("J.K. Rowling");
Thread.sleep(1000);
System.out.println(textbook1.getAuthor());
System.out.println(textbook2.getAuthor());
System.out.println(textbook1.shelfLife());
System.out.println(textbook2.shelfLife());
}
catch (Exception e){
System.out.println(e);
}
}
}
Textbook.main(null);
class Vehicle {
public String name;
public Vehicle(String name){
this.name = name;
}
public void honk(){
System.out.println("HONK MEEP BEEEEEEEP!");
}
}
class Car extends Vehicle {
public Car(String name){
super(name);
}
// methods and attributes
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car("Lexus GS430");
car1.honk();
System.out.println(car1.name);
}
}
// Parent class
public class Animal {
protected String name;
protected int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void speak() {
System.out.println("I AM AN ANIMALIO!!!!!!!!");
}
}
// Subclass 1
public class Turtle extends Animal {
private String breed;
public Turtle(String name, int age, String breed) {
super(name, age);
this.breed = breed;
}
@Override
public void speak() {
System.out.println("Turtle Noises Commence");
}
public void eat() {
System.out.println("I nomp lettuce.");
}
}
// Subclass 2
public class Cat extends Animal {
private boolean isIndoor;
public Cat(String name, int age, boolean isIndoor) {
super(name, age);
this.isIndoor = isIndoor;
}
@Override
public void speak() {
System.out.println("Meow!");
}
public void scratch() {
System.out.println("I am scratching the furniture.");
}
}
// Tester method
public class AnimalTester {
public static void main(String[] args) {
Animal animal1 = new Turtle("Shellby", 3, "Russian Tortoise");
Animal animal2 = new Cat("Whiskers", 2, true);
animal1.speak();
((Turtle) animal1).eat();
System.out.println(animal1.name);
animal2.speak();
((Cat) animal2).scratch();
System.out.println(animal2.age);
}
}
AnimalTester.main(null);