You operate several hot dog stands distributed throughout town. Define a class n
ID: 3601691 • Letter: Y
Question
You operate several hot dog stands distributed throughout town. Define a class named HotDogStand that has an instance variable for the hot dog Stand's ID number and an instance variable for how many hot dogs the stand has sold that day.Create a constructor that allows a user of the class to initialize values. Also Also create a method named justSold that increments by one the number of hot dogs the stand has sold. The idea is that this method will be invoked each time the stand sells a hot dog so that you can track the total number of hot dogs sold by the stand. Add another method that returns the number of hot dogs sold. Finally, Add a static variable that tracks the total number of hot dogs sold by all hot dog stands and a static method that returns the value in this variable. write a main method to test your class with at least three hot dog stands that each sell a variety of hotdog. The defined class will contain: At least two constructor methods, Getters and Setters A ‘copy’ constructor, A ‘toString’ method, An ‘equals’ method, A ‘finalize’ method, A ‘dispose’ method.
Explanation / Answer
HotDogStand.java
public class HotDogStand {
// Declaring instance variables
private int id;
private int soldHotDogsInStand;
// Declaring static variable
static int totalHotDogsSold;
// Parameterized constructor
public HotDogStand(int id, int soldHotDogsInStand) {
super();
this.id = id;
this.soldHotDogsInStand = soldHotDogsInStand;
totalHotDogsSold += soldHotDogsInStand;
}
// Parameterized constructor
public HotDogStand(HotDogStand hds) {
this.id = hds.getId();
this.soldHotDogsInStand = hds.getSoldHotDogsInStand();
}
// getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSoldHotDogsInStand() {
return soldHotDogsInStand;
}
public void setSoldHotDogsInStand(int soldHotDogsInStand) {
this.soldHotDogsInStand = soldHotDogsInStand;
}
// This method will increment when the hot dogs sold
public void justSold() {
totalHotDogsSold++;
}
// This method will return the total no hot dogs sold
public int noOfHotDogsSold() {
return totalHotDogsSold;
}
@Override
public void finalize() throws Throwable {
super.finalize();
}
// toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "HotDogStand [id=" + id + ", no of HotDogs sold by the stand=" + soldHotDogsInStand + " Total Hot Dogs Sold =" + totalHotDogsSold + "]";
}
}
_________________
Test.java
public class Test {
public static void main(String[] args) throws Throwable {
//Creating an instances of HodDogStand
HotDogStand hds1 = new HotDogStand(111, 50);
HotDogStand hds2 = new HotDogStand(222, 75);
HotDogStand hds3 = new HotDogStand(333, 35);
//calling the method on the HOtDogStand#1
hds1.justSold();
hds1.justSold();
hds1.justSold();
hds1.justSold();
//calling the method on the HOtDogStand#2
hds2.justSold();
hds2.justSold();
hds2.justSold();
hds2.justSold();
hds2.justSold();
//calling the method on the HOtDogStand#3
hds3.justSold();
hds3.justSold();
hds3.justSold();
hds3.justSold();
hds3.justSold();
//Displaying each HOtDogStand info
System.out.println(hds1);
System.out.println(hds2);
System.out.println(hds3);
//calling the finalize method on each stand
hds1.finalize();
hds2.finalize();
hds3.finalize();
}
}
__________________
Output:
HotDogStand [id=111, no of HotDogs sold by the stand=50 Total Hot Dogs Sold =174]
HotDogStand [id=222, no of HotDogs sold by the stand=75 Total Hot Dogs Sold =174]
HotDogStand [id=333, no of HotDogs sold by the stand=35 Total Hot Dogs Sold =174]
______________Thank YOu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.