Question

In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass.

Code for Store below(Super class aka Parent class)

public class Store
{
public final double SALES_TAX_RATE = 0.06;
private String name;

/**
* Overloaded constructor:<BR>
* Allows client to set beginning value for name
* This constructor takes one parameter<BR>
* Calls mutator method
* @param newName the name of the store
*/
public Store( String newName )
{
setName( newName );
}

/** getName method
* @return a String, the name of the store
*/
public String getName( )
{
return name;
}

/**
* Mutator method:<BR>
* Allows client to set value of name
* @param newName the new name for the store
*/
public void setName( String newName )
{
name = newName;
}

/**
* @return a string representation of the store
*/
public String toString( )
{
return( "name: " + name );
}

/**
* equals method
* Compares two Store objects for the same field value
* @param o another Store object
* @return a boolean, true if this object
* has the same field value as the parameter s
*/
public boolean equals( Object o )
{
if ( ! ( o instanceof Store ) )
return false;
else
{
Store s = (Store) o;
return ( name.equalsIgnoreCase( s.name ) );
}//end else

}//end equals method


}//end class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Store.java

public class Store

{

public final double SALES_TAX_RATE = 0.06;

private String name;

/**

* Overloaded constructor:<BR>

* Allows client to set beginning value for name

* This constructor takes one parameter<BR>

* Calls mutator method

* @param newName the name of the store

*/

public Store( String newName )

{

setName( newName );

}

/** getName method

* @return a String, the name of the store

*/

public String getName( )

{

return name;

}

/**

* Mutator method:<BR>

* Allows client to set value of name

* @param newName the new name for the store

*/

public void setName( String newName )

{

name = newName;

}

/**

* @return a string representation of the store

*/

public String toString( )

{

return( "name: " + name );

}

/**

* equals method

* Compares two Store objects for the same field value

* @param o another Store object

* @return a boolean, true if this object

* has the same field value as the parameter s

*/

public boolean equals( Object o )

{

if ( ! ( o instanceof Store ) )

return false;

else

{

Store s = (Store) o;

return ( name.equalsIgnoreCase( s.name ) );

}//end else

}//end equals method

}//end class

Restaurant.java

public class Restaurant extends Store {

private int personsServedEveryYear;

private double averagePrice;

public Restaurant(String newName, int persons, double avgPrice)

{

super(newName); // Calls the Store constructor is one.

setPersonsServed(persons);

setAveragePrice(avgPrice);

}

//accessors and mutuators

/**

* Mutator method:<BR>

* Allows client to set value of number of persons served every year

* @param persons the new value for personsServedEveryYear

*/

public void setPersonsServed(int persons)

{

personsServedEveryYear = persons;

}

/**

* Mutator method:<BR>

* Allows client to set value of average price per person

* @param avgPrice the new value for averagePrice

*/

public void setAveragePrice(double avgPrice)

{

averagePrice = avgPrice;

}

/**

* @return an int representing number of persons served every year

*/

public int getPersonsServed()

{

return personsServedEveryYear;

}

/**

* @return an double representing average price per person

*/

public double getAveragePrice()

{

return averagePrice;

}

/**

* @return a string representation of the Restaurant

*/

public String toString( )

{

return super.toString() + "\n"

+ "persons served per year: " + getPersonsServed() +

"\naverage price per person: " + getAveragePrice();

}

/**

* equals method

* Compares two Store objects for the same field value

* @param o another Store object

* @return a boolean, true if this object

* has the same field value as the parameter s

*/

public boolean equals( Object o )

{

if ( ! ( o instanceof Restaurant ) )

return false;

else

{

Restaurant r = (Restaurant) o;

return (r.getName().equalsIgnoreCase(getName()) && personsServedEveryYear == r.personsServedEveryYear && averagePrice == r.averagePrice);

}//end else

}//end equals method

/**

* averageTax method

* Calculate and return average tax

*/

public double getAverageTax()

{

return getPersonsServed()*getAveragePrice()*SALES_TAX_RATE;

} //end of getAverageTax method

}//end class

ClientClass.java

public class ClientClass {

public static void main(String[] args) {

//create an object for store

Store s = new Store("ABC");

//create another object for store

Store s1 = new Store("XYZ");

//print s and s1

System.out.println(s.toString());

System.out.println(s1.toString());

//check if s and s1 are equal

if(s.equals(s1))

System.out.println("objects s and s1 are same");

else

System.out.println("objects s and s1 are not same");

//declare an object for store

Store s3;

//set s1 to s3

s3=s1;

//print s1 and s3

System.out.println("\n"+s1.toString());

System.out.println(s3.toString());

//check if s1 and s3 are equal

if(s3.equals(s1))

System.out.println("objects s3 and s1 are same");

else

System.out.println("objects s3 and s1 are not same");

//create two objects for restaurant

Restaurant r1 = new Restaurant("ABC", 10, 49.99);

Restaurant r2 = new Restaurant("XYZ", 15, 45.99);

//print r1 and r2

System.out.println("\n"+r1.toString());

System.out.println(r2.toString());

//check if r1 and r2 are same

if(r2.equals(r1))

System.out.println("objects r1 and r2 are same");

else

System.out.println("objects r1 and r2 are not same");

//declare object r3

Restaurant r3;

//set r3 = r1

r3 = r1;

//check if r3 and r1 are same

System.out.println("\n"+r1.toString());

System.out.println(r3.toString());

if(r3.equals(r1))

System.out.println("objects r1 and r3 are same");

else

System.out.println("objects r1 and r3 are not same");

//print the average tax for restaurant r3

System.out.println("\nThe average sales tax is: " + r3.getAverageTax());

}

}

Output:

Add a comment
Know the answer?
Add Answer to:
In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a super class called Store which will have below member variables, constructor, and methods Member...

    Create a super class called Store which will have below member variables, constructor, and methods Member variables: - a final variable - SALES_TAX_RATE = 0.06 - String name; /** * Constructor:<BR> * Allows client to set beginning value for name * This constructor takes one parameter<BR> * Calls mutator method setName to set the name of the store * @param name the name of the store */ /** getName method * @return a String, the name of the store */...

  • How to solve this problem? Consider the following class : public class Store Public final double...

    How to solve this problem? Consider the following class : public class Store Public final double SALES TAX_RATE = 0.063B private String name; public Store(String newName) setName ( newName); public String getName () { return name; public void setName (String newName) { name = newName; public String toString() return "name: “ + name; Write a class encapsulating a web store, which inherits from Store. A web store has the following additional attributes: an Internet Address and the programming language in...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Write a class encapsulating a music store, which inherits from Store. A music store has the...

    Write a class encapsulating a music store, which inherits from Store. A music store has the following additional attributes: the number of titles it offers and its address. Code the constructor and the toString method of the new class. You also need to include a client class (with the main method) to test your code.

  • You will need to first create an object class encapsulating a Trivia Game which INHERITS from...

    You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game. Game is the parent class with the following attributes: description - which is a string write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money - double 3. number of questions that must be answered to win - integer. 4. write the accessor, mutator, constructor,...

  • Write in java and the class need to use give in the end copy it is...

    Write in java and the class need to use give in the end copy it is fine. Problem Use the Plant, Tree, Flower and Vegetable classes you have already created. Add an equals method to Plant, Tree and Flower only (see #2 below and the code at the end). The equals method in Plant will check the name. In Tree it will test name (use the parent equals), and the height. In Flower it will check name and color. In...

  • public class Item implements Comparable { private String name; private double price; /** * Constructor for...

    public class Item implements Comparable { private String name; private double price; /** * Constructor for objects of class Item * @param theName name of item * @param thePrice price of item */ public Item(String theName, double thePrice) { name = theName; price = thePrice; }    /** * gets the name * @return name name of item */ public String getName() { return name; }    /** * gets price of item * @return price price of item */...

  • 1. Write a new class, Cat, derived from PetRecord – Add an additional attribute to store...

    1. Write a new class, Cat, derived from PetRecord – Add an additional attribute to store the number of lives remaining: numLives – Write a constructor that initializes numLives to 9 and initializes name, age, & weight based on formal parameter values 2. Write a main method to test your Cat constructor & call the inherited writeOutput() method 3. Write a new writeOutput method in Cat that uses “super” to print the Cat’s name, age, weight & numLives – Does...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT