0% found this document useful (0 votes)
102 views4 pages

Java Book Class Tester Example

This document defines classes for a Book including: - Book class with attributes for author, title, publication date, and publisher - Date class to represent publication date - Address class to represent publisher address - Publisher class to combine address and name It creates Book objects, prints their details, and checks if their age is over 3 years.

Uploaded by

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

Java Book Class Tester Example

This document defines classes for a Book including: - Book class with attributes for author, title, publication date, and publisher - Date class to represent publication date - Address class to represent publisher address - Publisher class to combine address and name It creates Book objects, prints their details, and checks if their age is over 3 years.

Uploaded by

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

//tester

public class BookTester {

// main method
public static void main(String[] args) {

// create a object of Book class and print it


Book b1 = new Book("Clark Den","C# object oriented programming
Language",24,8,2015,"Apress",23,"California","USA");

[Link]("\nBook 1 : \n"+b1);

Book b2 = new Book("Abhinav Bindra","DSA in JAVA",23,2,2016,"Cpress",13,"Los


Angles","USA");

[Link]("\nBook 2 : \n"+b2);

}
}

// class named Date


class Date{

// instance variables

private int day;


private int month;
private int year;

// constructor

public Date(int day, int month, int year) {

// initialize instance variables


[Link] = day;
[Link] = month;
[Link] = year;

// get year that returns year


public int getyear() {

return year;

// return monthname

public String convertMonth() {

String months[] =
{"","January","February","March","April","May","June","July","August","September","
October","November","December"};

return months[month];
}

// return details of Date

public String toString() {

return convertMonth().substring(0, 3)+" "+year;

//class named Address


class Address{

// instance variables

private int num;


private String city;
private String country;

// constructor

public Address(int num,String city, String country){

// initialize instance variables


[Link] = city;
[Link] = country;

// return details of Address

public String toString() {

return city+", "+country+".";

// class named Publisher


class Publisher{

// instance variables

private String name;


private Address address;

// constructor

public Publisher(String name, Address address) {

// initialize instance variables

[Link] = name;
[Link] = address;

// return details of Publisher

public String toString() {

return name+", "+address;

// class named BOOK


class Book{

// instance variables

private String author;


private String title;
private Date pubDate;
private Publisher publisher;

// constructor

public Book(String author, String title, int day, int month, int year, String
pubname, int number, String city, String country ){

// initialize instance variables

[Link] = author;

[Link] = title;

Date date = new Date(day,month,year);

[Link] = date;

Address add = new Address(number,city,country);

Publisher pub = new Publisher(pubname, add);


[Link] = pub;

// return details of Book

public String toString() {

return author+", "+title+", "+publisher+" "+pubDate;

// check checkBookAge
public int checkBookAge() {

// if book is 3 years old then return 1

if([Link]() < 2019) {

return 1;

// else return 0

else {

return 0;

Common questions

Powered by AI

The document does not explicitly show constructor overloading, as each class features a single constructor designed to initialize all relevant fields directly upon object creation. This absence might reflect a design decision prioritizing straightforward object instantiation over flexibility. Consequently, each constructor tightly couples object instantiation with its full initialization .

The use of composition in managing nested data suggests a use of the Composite design pattern. The Book class encapsulates instances of other classes like Date and Publisher, which in turn contains an Address instance. This structure effectively organizes related components into a coherent whole, assembling complex objects from simpler ones while maintaining individual specialization .

The Address class omits the 'num' instance variable in its toString method because it focuses on representing the city and country components of the address. The omission suggests a design choice to simplify the output or an incomplete implementation where 'num' may refer to additional address details not used in this context .

The publisher's full address is formed in the Publisher class by calling the Address class's toString method, which concatenates the city and country with a comma and a period. The Publisher class's toString method then concatenates the publisher's name with this address string, resulting in the format 'name, city, country.' .

The Book class uses the toString method to print the details of a book. This method constructs a string that includes the author's name, book title, publisher's details, and publication date. The format is 'author, title, publisher pubDate', where the publisher and pubDate are retrieved by calling their respective toString methods .

The Date class uses the convertMonth method to convert numeric month information into its string representation. It utilizes an array of month names, where each index corresponds to a specific month number. The method returns the month name corresponding to the provided month number by accessing the array with 'months[month]' and returns the string with the first three characters .

The Book class uses the checkBookAge method to determine if a book is 'old'. This method checks if the book's publication year, obtained via the getyear method of the Date class, is less than 2019. If it is less than 2019, the method returns 1, indicating the book is 'old'. Otherwise, it returns 0 .

Encapsulation in the Book class is achieved by declaring instance variables as private, thereby restricting direct access to them from outside the class. Access is controlled through public methods like the constructor and toString, which safely manage data exposure and manipulation. This principle safeguards the integrity of the class's internal state .

The Book class benefits from separation of concerns by delegating different responsibilities to specific classes. Date handles temporal data, Address manages geographic information, and Publisher encapsulates publishing details. This modular approach simplifies maintenance and enhances readability by isolating functionalities and enabling components to evolve independently without entangling the Book class itself .

Object composition is illustrated through the use of instances of one class as fields within another. The Book class contains fields for a Date object and a Publisher object, encapsulating both temporal and publishing data. The Publisher class further composes an Address object to handle location details. This layered object composition allows complex data modeling, imbuing the Book object with detailed subcomponents .

You might also like