Question

In: Computer Science

Encapsulation You are tasked with building a BookStore. Your Architect has told you that the BookStore...

Encapsulation

You are tasked with building a BookStore. Your Architect has told you that the BookStore class has the following members : long id; String name; Address address; Book book; The Address class has the following attributes : String streetName; String houseNumber; String apartmentNumber; String zipCode; The book class has the following members String name; String isbn; Author author; The Author class within the book class has the following member : String firstName; String lastName; Date dateOfBirth; Address address; //Address class has already been created You have to create your BookStore class. You have to use encapsulation to make sure all the variables of the bookstore class are protected. For the classes that are within the bookstore class (Address and Book and Author within book) you have to use encapsulation as well for the member variables. Now you need to create another class that will have a main method. Call this class App. Populate your variables with data using the setter methods and then print them out using System.out.println().

Use Java

Solutions

Expert Solution

For the above program, we need to have the following thing into mind for encapsulation to be there

  • For the program, we need to have private variables in order to have encapsulation.

Following is the program.

import java.util.Date;

class Author{
   private String firstName, lastName;
   private Date dateOfBirth;
   private Address address;
   public Author(String firstName, String lastName, Date dateOfBirth, Address address) {
       super();
       this.firstName = firstName;
       this.lastName = lastName;
       this.dateOfBirth = dateOfBirth;
       this.address = address;
   }
   public String getFirstName() {
       return firstName;
   }
   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }
   public String getLastName() {
       return lastName;
   }
   public void setLastName(String lastName) {
       this.lastName = lastName;
   }
   public Date getDateOfBirth() {
       return dateOfBirth;
   }
   public void setDateOfBirth(Date dateOfBirth) {
       this.dateOfBirth = dateOfBirth;
   }
   public Address getAddress() {
       return address;
   }
   public void setAddress(Address address) {
       this.address = address;
   }
   @Override
   public String toString() {
       return "Author [firstName=" + firstName + ", lastName=" + lastName + ", dateOfBirth=" + dateOfBirth
               + ", address=" + address + "]";
   }
  
}
class Book{
   private String name;
   private String isbn;
   private Author author;
   public Book(String name, String isbn, Author author) {
       super();
       this.name = name;
       this.isbn = isbn;
       this.author = author;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public String getIsbn() {
       return isbn;
   }
   public void setIsbn(String isbn) {
       this.isbn = isbn;
   }
   public Author getAuthor() {
       return author;
   }
   public void setAuthor(Author author) {
       this.author = author;
   }
   @Override
   public String toString() {
       return "Book [name=" + name + ", isbn=" + isbn + ", author=" + author + "]";
   }
  
}
class Address{
   private String streetName;
   private String houseNumber;
   private String appartmentNumber;
   private String zipCode;
   public Address(String streetName, String houseNumber, String appartmentNumber, String zipCode) {
       super();
       this.streetName = streetName;
       this.houseNumber = houseNumber;
       this.appartmentNumber = appartmentNumber;
       this.zipCode = zipCode;
   }
   public String getStreetName() {
       return streetName;
   }
   public void setStreetName(String streetName) {
       this.streetName = streetName;
   }
   public String getHouseNumber() {
       return houseNumber;
   }
   public void setHouseNumber(String houseNumber) {
       this.houseNumber = houseNumber;
   }
   public String getAppartmentNumber() {
       return appartmentNumber;
   }
   public void setAppartmentNumber(String appartmentNumber) {
       this.appartmentNumber = appartmentNumber;
   }
   public String getZipCode() {
       return zipCode;
   }
   public void setZipCode(String zipCode) {
       this.zipCode = zipCode;
   }
   @Override
   public String toString() {
       return "Address [streetName=" + streetName + ", houseNumber=" + houseNumber + ", appartmentNumber="
               + appartmentNumber + ", zipCode=" + zipCode + "]";
   }
  
}
class BookStore{
   private long id;
   private String name;
   private Address address;
   private Book book;
   public BookStore(long id, String name, Address address, Book book) {
       super();
       this.id = id;
       this.name = name;
       this.address = address;
       this.book = book;
   }
   public long getId() {
       return id;
   }
   public void setId(long id) {
       this.id = id;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public Address getAddress() {
       return address;
   }
   public void setAddress(Address address) {
       this.address = address;
   }
   public Book getBook() {
       return book;
   }
   public void setBook(Book book) {
       this.book = book;
   }
   @Override
   public String toString() {
       return "BookStore [id=" + id + ", name=" + name + ", address=" + address + ", book=" + book + "]";
   }
  
  
}

public class App {
   public static void main(String args[]) {
       Address address = new Address("Street Name", "houseNumber", "appartmentNumber", "zipCode");
       Author author = new Author("John", "Charlie", new Date(1997, 12, 12), address);
       Book book = new Book("name", "isbn", author);
       BookStore bookStore = new BookStore(12356, "name", address, book);
       System.out.println(bookStore);
      
   }

}

Following is the snippet of the output:

That was a nice question to answer
Friend, If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Thanks


Related Solutions

Encapsulation You are tasked with building a BookStore. Your Architect has told you that the BookStore...
Encapsulation You are tasked with building a BookStore. Your Architect has told you that the BookStore class has the following members : long id; String name; Address address; Book book; The Address class has the following attributes : String streetName; String houseNumber; String apartmentNumber; String zipCode; The book class has the following members String name; String isbn; Author author; The Author class within the book class has the following member : String firstName; String lastName; Date dateOfBirth; Address address; //Address...
An architect wants to determine the relationship between the heights (in feet) of a building and...
An architect wants to determine the relationship between the heights (in feet) of a building and the number of stories in the building. The data for a sample of 10 buildings are shown. Stories (x) 64 54 40 31 45 38 42 41 37 40 Height (y) 841 725 635 616 615 582 535 520 511 485 Is there a significant linear correlation between the two variables ? Use α = .05 What would be the best predicted Height of...
Your mortgage loan has been approved and started on a Tuesday, and you were told your...
Your mortgage loan has been approved and started on a Tuesday, and you were told your first payment is due in 90 calendar days. What is the day of week of your first payment day? Write down your calculation process, the arithmetic expression you use, as well as your answer. No java coding is needed. If you only provide the answer without the other details to explain the process, you will not receive credit on this question.
Your manager has told you that capital budgeting is a waste of time. How would you...
Your manager has told you that capital budgeting is a waste of time. How would you respond to your manager? Include a discussion of the screening decision calculations available for use in making the appropriate decisions.
Your boss has told you that you need to process a full 55 gallon drum of...
Your boss has told you that you need to process a full 55 gallon drum of 1-butene that is stored at 15 atm and ambient temperature. You are going to combust this material with the stoichiometric amount of air at 1 atm and the product gasses will emerge at 579 oF with a conversion of 97%. You have a metal gas sphere that is 55 feet in diameter at ambient temperature that contains the air you are feeding in. You...
Your company has decided to investment in the stock market. Management has tasked you to conduct...
Your company has decided to investment in the stock market. Management has tasked you to conduct and assessment of the two stocks listed below Year Stock A Ra Stock B Rb 2014 (18%) (14,50%) 2015 33 21,8 2016 15 30,5 2017 (0,5) (7,6) 2018 27 26,3 1.1 Calculate the average rate of return for each stock during the period 2014 through 2018. 1.2 Assume that someone held a portfolio consisting of 50% of Stock A and 50% of Stock B....
Problem: An electric company has committed to building a solar power plant. An engineer tasked with...
Problem: An electric company has committed to building a solar power plant. An engineer tasked with the company has been tasked with evaluating three solar power generation technologies. The power company uses an interest rate of 10% and a 20-year planning horizon for choosing the project that best fits its budgetary and financial goals. Design 1: Flat Solar Panels: A field of flat solar panels angled to best catch the incident solar radiation wis expected to yield a power of...
You are a software development employee at a startup company. Your HR department has tasked the...
You are a software development employee at a startup company. Your HR department has tasked the IT department with developing a simple application (Windows Form Application) to load and display employee records. Your application will need to do the following: Create an Employee Class with the following properties: First Name Last Name Street Address City State Zip Create a sub class for Managers which inherits from the Employee Class. The Manager class will have the following additional properties: Cost Center...
Your company has tasked you with the evaluation of two projects for investments, Project X and...
Your company has tasked you with the evaluation of two projects for investments, Project X and Project Y. Each project has a cost of capital of R10,000 and the required return of 12 percent. The projects’ expected cash flows (in Rands) are listed below: Year Project X Project Y 0 (10,000.00) (10,000.00) 1 6,500.00 4,000.00 2 3,000.00 4,000.00 3 3,000.00 4,000.00 4 2,000.00 4,000.00 Required: 11.1. Calculate each project’s payback period, net present value (NPV and profitability index (PI). 11.2....
You are working as an assistant to your cousin, who is an architect. She is currently...
You are working as an assistant to your cousin, who is an architect. She is currently designing the lobby for a new luxury hotel. The lobby will include a walkway suspended above the main level. Her design will include the following features. Above the walkway, attached at various points along its length, will be vertical steel cables of diameter 1.27 cm and unstressed length 5.45 m. These cables will run upward from the walkway and be attached to a rigid...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT