Write a class Store which includes the attributes: store name and sales tax rate
ID: 3750945 • Letter: W
Question
Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Book Store, which inherits from Store. A Book Store has the following additional attributes: how many books are sold every year and the average price per book. Java
Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Book Store; In the Book Store class, also code a method returning the average taxes per year.
You should create a test class which creates 1 Store object and 2 Book Store objects, then calls your set methods, get methods, toString and equals methods and average taxes per year for the Book Store objects..
Explanation / Answer
//Class Store defined here.
class Store {
//defining attributes of Store class
public String storeName;
public double salesTaxRate;
//setting Up default and parameterized constructors.
//Usually compiler generates default construct even if you don't write.
public Store() {
}
public Store(String storeName, double salesTaxRate) {
this.salesTaxRate = salesTaxRate;
this.storeName = storeName;
}
//getters and setters methods are defined for all the attributes of Store Class.
public String getStoreName() {
return storeName;
}
public void setStoreName(String storeName) {
this.storeName = storeName;
}
public double getSalesTaxRate() {
return salesTaxRate;
}
public void setSalesTaxRate(double salesTaxRate) {
this.salesTaxRate = salesTaxRate;
}
}
// Defining BookStore Class which extends Store Class
//Thus BookStore will also have the attributes from Store class i.e salesTaxRate and storeName.
class BookStore extends Store {
//defining attributes of Store class
public Long noOfBooksPerYear;
public double avgPricePerBook;
//setting Up default and parameterized constructors.
public BookStore() {
super();
}
public BookStore(String storeName, double salesTaxRate) {
super(storeName, salesTaxRate);
}
//getters and setters methods are defined for all the attributes of BookStore Class.
public Long getNoOfBooksPerYear() {
return noOfBooksPerYear;
}
public void setNoOfBooksPerYear(Long noOfBooksPerYear) {
this.noOfBooksPerYear = noOfBooksPerYear;
}
public double getAvgPricePerBook() {
return avgPricePerBook;
}
public void setAvgPricePerBook(double avgPricePerBook) {
this.avgPricePerBook = avgPricePerBook;
}
//defining the method to calculate average tax per year
//average of two numbers will be (a+b)/2;
//this methods return the double value which will be in decimal(floating point value)
public double avgTaxesPerYear(double salesTaxRate1, double salesTaxRate2) {
double avgSalesTaxPerYear = (salesTaxRate1 + salesTaxRate2) / 2;
return avgSalesTaxPerYear;
}
}
//definint test class
public class Test {
//defining main method.
public static void main(String args[]) {
//defining objects of Store and BookStore Class
Store s1 = new Store("Store1",763.78);
//here we can set the inherited attributes from Store class to BookStore Class.
BookStore bs1 = new BookStore("Store2", 100.40);
BookStore bs2 = new BookStore("Store3", 200.60);
//Using setter methods to set the values (using random values just to complete the flow)
bs1.setNoOfBooksPerYear(500L);
bs1.setAvgPricePerBook(120.70);
bs2.setNoOfBooksPerYear(800L);
bs2.setAvgPricePerBook(80.12);
//calling avgTaxesPerYear function to get the average of both the bookStores.
double avgSalesTaxPerYear = new BookStore().avgTaxesPerYear(bs1.getSalesTaxRate(),bs2.getSalesTaxRate());
//avgSalesTaxPerYear Stores it in double format since the return type of method defined is double.
//toString method from object Class can be used to Convert that value to String and apply string //operations.
String avgSalesTaxPerYearString = Double.toString(avgSalesTaxPerYear);
//avgSalesTaxPerYearString holds the string value of average.
//equals methods is used to check the value of returned average and actual average
//(we can calculate mathematically)
//If both will be same it will print "Inside Equals method"
if(avgSalesTaxPerYearString.equals("150.5")) {
System.out.print("Inside Equals method");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.