Question

Write a class Store which includes the attributes: store name and sales tax rate. Write another...

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.

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

Homework Answers

Answer #1
// Write a class Store
class Store {

    //  which includes the attributes: store name and sales tax rate
    private String storeName;
    private double saleTaxRate;

    // constructor
    public Store(String storeName, double saleTaxRate) {
        this.storeName = storeName;
        this.saleTaxRate = saleTaxRate;
    }

    //  accessors, mutators
    public String getStoreName() {
        return storeName;
    }

    public void setStoreName(String storeName) {
        this.storeName = storeName;
    }

    public double getSaleTaxRate() {
        return saleTaxRate;
    }

    public void setSaleTaxRate(double saleTaxRate) {
        this.saleTaxRate = saleTaxRate;
    }

    // equals method
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Store store = (Store) o;

        if (Double.compare(store.saleTaxRate, saleTaxRate) != 0) return false;
        return storeName != null ? storeName.equals(store.storeName) : store.storeName == null;
    }

    // toString method
    @Override
    public String toString() {
        return "Store{" +
                "storeName='" + storeName + '\'' +
                ", saleTaxRate=" + saleTaxRate +
                '}';
    }
}

//  Write another class encapsulating a Book Store, which inherits from Store.
class BookStore extends Store {

    // A Book Store has the following additional attributes: how many books are sold every year and the average price per book
    private int numberOfBooksSoldPerYear;
    private double averagePrice;

    // constructor
    public BookStore(String storeName, double saleTaxRate, int numberOfBooksSoldPerYear, double averagePrice) {
        super(storeName, saleTaxRate);
        this.numberOfBooksSoldPerYear = numberOfBooksSoldPerYear;
        this.averagePrice = averagePrice;
    }

    //  accessors, mutators
    public int getNumberOfBooksSoldPerYear() {
        return numberOfBooksSoldPerYear;
    }

    public void setNumberOfBooksSoldPerYear(int numberOfBooksSoldPerYear) {
        this.numberOfBooksSoldPerYear = numberOfBooksSoldPerYear;
    }

    public double getAveragePrice() {
        return averagePrice;
    }

    public void setAveragePrice(double averagePrice) {
        this.averagePrice = averagePrice;
    }

    // equals method
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;

        BookStore bookStore = (BookStore) o;

        if (numberOfBooksSoldPerYear != bookStore.numberOfBooksSoldPerYear) return false;
        return Double.compare(bookStore.averagePrice, averagePrice) == 0;
    }

    // toString method
    @Override
    public String toString() {
        return "Book" + super.toString() +
                "numberOfBooksSoldPerYear=" + numberOfBooksSoldPerYear +
                ", averagePrice=" + averagePrice +
                '}';
    }

    //  In the Book Store class, also code a method returning the average taxes per year.
    public double averageTaxesPerYear() {
        return this.averagePrice * this.numberOfBooksSoldPerYear / this.getSaleTaxRate();
    }
}

public class StoreTest {
    public static void main(String[] args) {

        //  You should create a test class which creates 1 Store object
        Store store = new Store("Bajaj", 2.5);
        System.out.println(store);

        //  2 Book Store objects, then calls your set methods, get methods, toString and equals methods
        BookStore bookStore1 = new BookStore("MDG", 3.5, 100, 250);
        BookStore bookStore2 = new BookStore("MSB", 3.8, 300, 500);

        // testing toString method and average taxes per year
        System.out.println(bookStore1);
        System.out.println(bookStore1.averageTaxesPerYear());
        System.out.println(bookStore2);
        System.out.println(bookStore2.averageTaxesPerYear());

        //  test equals method
        System.out.println(bookStore1.equals(bookStore2));

    }
}

sample output screenshot:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Haviing trouble with my homework (java) 1)Write the class CustomerAccount, which has fields/attributes called custID of...
Haviing trouble with my homework (java) 1)Write the class CustomerAccount, which has fields/attributes called custID of type String, custName of type String and checkingBalance of type double and savingBalance of type double 2)Write the constructor for CustomerAccount class. The constructor takes parameters to initialize custID, custName, checkingBalance and savingBalance. 3)Write the mutator method for all attributes of the class CustomerAccount. then Write the toString method for class CustomerAccount.
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods....
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have...
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a...
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a sub class's constructor? Select one: a. It must be the first statement in the constructor b. If you don't include it Java will c. If you don't include it you must have a 0 parameter constructor coded in the super class or no constructors coded at all in the super class d. The sub class constructor containing the super call and the called super...
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields and appropriate types and permissions diceType numSides sideUp The class should have A 0 argument (default) constructor default values are diceType: d6, numSides: 6, sideUp: randomValue 1 argument constructor for the number of sides default values are diceType: d{numSides}, sideUp: randomValue 2 argument constructor for the number of sides and the diceType appropriate accessors and mutators *theoretical question: can you change the number of...
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have...
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have types str, int and float. Your class must have a constructor method to initialize the variables to an appropriate value, and methods for setting the value of each instance variable (set methods) and for retrieving the instance variable values (get methods). You can use the credit card code we looked at for assistance.
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Goal:   Manage the inventory of objects sold in an online or brick and mortar store. If...
Goal:   Manage the inventory of objects sold in an online or brick and mortar store. If you can't implement all of the features of Project described in this document, implement what you can. Start by implementing the StockItem class, and its derived classes. Then add in the InventoryManager class later. In each case, the test files must be in separate classes. UML Diagram: Use Violet or other drawing software to draw the UML diagrams of each class that you use...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT