Class Design USING JAVA - Create a class that defines an object (e.g. a Cat, Boo
ID: 3774829 • Letter: C
Question
Class Design USING JAVA
- Create a class that defines an object (e.g. a Cat, Book, etc.)
- Include some instance variables representing properties of the object.
- Include some methods representing actions that the object can do or actions that can be performed on the object (you don’t have to write the code for the method).
-Include at least one method that takes in a parameter.
-Create at least one instance of the class.
-Execute at least one method of the class instance.
-What should it do?
Explanation / Answer
Book.java
public class Book {
//Declaring instance variables
private String book_name;
private String author_name;
private int no_of_pages;
//Parameterized constructor
public Book(String book_name, String author_name, int no_of_pages) {
this.book_name = book_name;
this.author_name = author_name;
this.no_of_pages = no_of_pages;
}
//This method returns the author name
String getAuthorName()
{
return author_name;
}
//This method returns the book name
String getBookName()
{
return book_name;
}
//This method return the no of pages
int getNo_of_pages()
{
return no_of_pages;
}
//Thsi method returns the no of copies sold
String getNo_of_copies_sold()
{
return "20 Million";
}
public static void main(String[] args) {
//Creating an object to the Book class
Book b=new Book("Harry Potter","J.K Rolling",300);
//Displaying the Book name
System.out.println("Book Name :"+b.getBookName());
//Displaying the Author name
System.out.println("Author Name :"+b.getAuthorName());
//Displaying the No of pages
System.out.println("No of pages :"+b.getNo_of_pages());
//Displaying the no of copies of book sold
System.out.println("No of Copies Sold :"+b.getNo_of_copies_sold());
}
}
____________________
Output:
Book Name :Harry Potter
Author Name :J.K Rolling
No of pages :300
No of Copies Sold :20 Million
______Thank You