0% found this document useful (0 votes)
43 views3 pages

Java Classes: Author and Book Example

Uploaded by

n190455
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views3 pages

Java Classes: Author and Book Example

Uploaded by

n190455
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASSIGNMENT-7

Name:Chameswari
Id No:N190455
Create a class Author with the following information.
Member variables : name (String), email (String), and gender (char)
Parameterized Constructor: To initialize the variables
Create a class Book with the following information.
Member variables : name (String), author (of the class Author you have
just created), price (double), and qtyInStock (int)
[Assumption: Each book will be written by exactly one Author]
Parameterized Constructor: To initialize the variables
Getters and Setters for all the member variables

In the main method, create a book object and print all details of the book
(including the author details)

class Book {
private String name;
private Author author;
private double price;
private int qtyInStock;
public Book(String name, Author author, double price, int qtyInStock)
{
this.name = name;
this.author = author;
this.price = price;
this.qtyInStock = qtyInStock;
}
public String getName() {
return name;
}

public void setName(String name) {


this.name = name;
}

public Author getAuthor() {


return author;
}

public void setAuthor(Author author) {


this.author = author;
}

public double getPrice() {


return price;
}

public void setPrice(double price) {


this.price = price;
}

public int getQtyInStock() {


return qtyInStock;
}

public void setQtyInStock(int qtyInStock) {


this.qtyInStock = qtyInStock;
}
}

public class Main {


public static void main(String[] args) {
Author author = new Author("J.K. Rowling",
"[email protected]", 'F');
Book book = new Book("Harry Potter and the Philosopher's Stone",
author, 20.50, 100);
System.out.println("Book Name: " + book.getName());
System.out.println("Author Name: " + book.getAuthor().getName());
System.out.println("Author Email: " +
book.getAuthor().getEmail());
System.out.println("Author Gender: " +
book.getAuthor().getGender());
System.out.println("Price: $" + book.getPrice());
System.out.println("Quantity in Stock: " + book.getQtyInStock());
}
}

You might also like