please help me. You operate several hot dog stands distributed throughout town.
ID: 3684823 • Letter: P
Question
please help me.
You operate several hot dog stands distributed throughout town. Define a d named Hothogstand that has an instance variable for the hot dog stand's ID num ber and an instance variable for how many hot dogs the stand has sold that Create a constructor that allows a user of the dass to initialize both values create a method named guntSold that increments by one the number of hot gs the stand has sold. The idea is that this method will be invoked each time the sand sellsa hot dog so that you can track the total number of hot dogs sold by the stmd Add another method that returns the number of hot dogs sold ilyiledd a static variable that tradks the total number of hot dogs sold by all hot dog mands and a static method that returns the value in this variable wiein method to test your das with at least three hot dog stands that each variess of hot dogsExplanation / Answer
public class HotDogStand {
private int standId;
private int soldDogNum;
// static variable to keep track of total sold dogs from all hot stand
private static int totalSoldDog = 0;
//first constructor
public HotDogStand(int id) {
standId = id;
soldDogNum = 0;
}
// second constructor
public HotDogStand(int standId, int soldDogNum) {
super();
this.standId = standId;
this.soldDogNum = soldDogNum;
}
// copy constructor
public HotDogStand(HotDogStand hotDogStand){
standId = hotDogStand.getStandId();
soldDogNum = hotDogStand.getSoldDogNum();
}
// getters and setters
public int getStandId() {
return standId;
}
public int getSoldDogNum() {
return soldDogNum;
}
public void setStandId(int standId) {
this.standId = standId;
}
public void setSoldDogNum(int soldDogNum) {
this.soldDogNum = soldDogNum;
}
//incremet sold dog number
public void justSold(){
totalSoldDog++;
soldDogNum++;
}
@Override
public String toString() {
return "Stand Id: "+standId+", Nubber of sold dogs: "+soldDogNum;
}
@Override
public boolean equals(Object obj) {
HotDogStand temp = (HotDogStand)obj;
if(this.standId == temp.getStandId())
return true;
return false;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
}
public static int getTotalSoldDogNum(){
return totalSoldDog;
}
// main method
public static void main(String[] args) {
HotDogStand h1 = new HotDogStand(1);
HotDogStand h2 = new HotDogStand(2);
h1.justSold();
System.out.println(h1.toString());
System.out.println(h2.toString());
h2.justSold();
System.out.println(h2.toString());
System.out.println("Total Sold dogs: "+HotDogStand.getTotalSoldDogNum());
h1.justSold();
h1.justSold();
h2.justSold();
System.out.println("Total Sold dogs: "+HotDogStand.getTotalSoldDogNum());
}
}
/*
Sample run:
Stand Id: 1, Nubber of sold dogs: 1
Stand Id: 2, Nubber of sold dogs: 0
Stand Id: 2, Nubber of sold dogs: 1
Total Sold dogs: 2
Total Sold dogs: 5
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.