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
in JAVA Write a class Store which includes the attributes: store name and sales tax rate....
in JAVA Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Yarn Store, which inherits from Store. A Yarn Store has the following additional attributes: how many skeins of yarn are sold every year and the average price per skein. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Yarn Store; In the Yarn Store class, also code a method returning the...
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: Write a class Cow with attributes name (string) and numLegs (int). Do not make the...
Java: Write a class Cow with attributes name (string) and numLegs (int). Do not make the attributes private. Make a constructor with two parameters to instantiate the attributes, and a toString method that prints only the name, a comma, a space, and the number of legs (e.g. Bossie, 3). Add a main method that makes two cows, Bossie with 3 legs and Elsie with 4 legs, and puts them into an ArrayList. Print the ArrayList, one element per line, using...
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...