Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Testing Method Create two hot dog stands using the overloaded constructor Create

ID: 3603528 • Letter: T

Question

Testing Method

Create two hot dog stands using the overloaded constructor

Create one hot dog stand using the default constructor

o Use the set methods to initialize this one.

o Use the get methods to verify that the set methods work.

Buy hotdogs for each of the three stands. Vary the numbers

Display each of the objects to verify their content and count sold, and total for all stands.

Use the ‘copy’ constructor to create a ‘near’ duplicate of one of the original stands.

Use the ‘equals’ method to verify the equality of the two stands.

Verify the sold counts for all stands as well as the totals.

Defined class should Contain:

At least two constructor methods,

Getters and Setters

A ‘copy’ constructor,

A ‘toString’ method,

An ‘equals’ method,

A ‘finalize’ method,

Language JAVA

Programming Projects 1. 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 num ber 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 both values. 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 hot dogs.

Explanation / Answer

Project Structure is as follows

Source
|- com.java.hotdog // package
   |- HotDogStand.java // class for business logic
|- com.java.app // package
|- App.java // class for main method

-----------------------------------------------------------------------------------------

Lets see the HotDogStand.java class

package com.java.hotdog;

/**
* This class represents the Hot Dog stand Object
*
*/
public class HotDogStand {
/**
* this is unique stand id
*/
private int id;

/**
* this field denote total sale of hot dogs by this stand.
*/
private int numberOfHotDogSold;

/**
* this field shows total hot dogs sold by all stands.
*/
static int totalHotDogSoldCount;

/**
* This is default constructor
*/
public HotDogStand() {
   this.numberOfHotDogSold = 0;
}

/**
* This is parameterized constructor
* @param id it is stand id
* @param numberOfHotDogSold this field shows total hotdog count sold by stand
*/
public HotDogStand(int id, int numberOfHotDogSold) {
   super();
   this.id = id;
   this.numberOfHotDogSold = numberOfHotDogSold;
}

/**
* Copy constructor
* @param obj is the object of class {@link HotDogStand}
*/
public HotDogStand(HotDogStand obj) {
   super();
   /**
   * check of null object to avoid NullPointerException  
   */
   if(obj != null) {
      this.setId(obj.getId());
      this.setNumberOfHotDogSold(obj.getNumberOfHotDogSold());
   }
}

/**
* This method is used to increment sale count.
* This method should be called after each hot dog sold
*/
public void justSold() {
   // lets increment sale count of this stand
   this.numberOfHotDogSold++;

   //lets increment global sale count
   totalHotDogSoldCount++;
}

/**
* This method return count of total hot dog sold by all stands
*/
public static int getTotalHotDogSoldCount() {
   return totalHotDogSoldCount;
}

/**
* This method returns count total hot dog sold by stand
*/
public int getNumberOfHotDogSoldByStand() {
   return this.numberOfHotDogSold;
}

/**
* This is getter for id
*/
public int getId() {
   return id;
}

/**
* This is setter for id
*/
public void setId(int id) {
   this.id = id;
}

/**
* This is getter for count of hot dog sold by stand
*/
public int getNumberOfHotDogSold() {
   return numberOfHotDogSold;
}

/**
* This is setter for hot dog count sold by stand
* @param numberOfHotDogSold
*/
public void setNumberOfHotDogSold(int numberOfHotDogSold) {
   this.numberOfHotDogSold = numberOfHotDogSold;
}

/**
* This is String representation of class {@link HotDogStand}
*/
@Override
public String toString() {
   return "HotDogStand [Stand id: " + id + ", Total Hot Dog Sold = " + numberOfHotDogSold + "]";
}

@Override
public int hashCode() {
   final int prime = 31;
   int result = 1;
   result = prime * result + id;
   result = prime * result + numberOfHotDogSold;
   return result;
}

/**
* This is equal methods to check equality of two {@link HotDogStand} objects
*/
@Override
public boolean equals(Object obj) {
   if (this == obj)
      return true;
   if (obj == null)
      return false;
   if (getClass() != obj.getClass())
      return false;
   HotDogStand other = (HotDogStand) obj;
   if (id != other.id)
      return false;
   if (numberOfHotDogSold != other.numberOfHotDogSold)
      return false;
   return true;
}


@Override
protected void finalize() throws Throwable {
   super.finalize();
}

}

-------------------------------------------------------------------------
Lets see the App.java class. It is a entry point of application.

package com.java.app;

import com.java.hotdog.HotDogStand;

public class App {
/**
* main method: entry point of application
*/
public static void main(String[] args) {

   /*
   * Lets create first hot dog stand object
   * using default constructor
   */
   HotDogStand stand1 = new HotDogStand();
   stand1.setId(123);
   stand1.setNumberOfHotDogSold(0);

   /*
   * Lets create second hot dog stand object
   * using parameterized constructor  
   */
   HotDogStand stand2 = new HotDogStand(456, 0);

   /*
   * Lets create third hot dog stand object
   * using copy constructor
   */
   HotDogStand stand3 = new HotDogStand(stand2);

   /*
   * Lets check equality of two objects i.e. stand2 and stand3
   * as stand3 is created using copy constructor so that
   * both object must be same
   */
   if(stand2.equals(stand3)) {
      System.out.println("Both objects are equals ");
   }

   /*
   * As ID must be unique
   * So lets change the ID of stand3 object
   */
   stand3.setId(789);


   // stand1 sold one hot dog
   stand1.justSold();

   // lets print sale of stand1 and total sale of all stands
   System.out.println(" "+stand1.toString());
   System.out.println("Total Hot Dog sold by All Stand: "+ HotDogStand.getTotalHotDogSoldCount());


   // stand2 sold four hot dogs
   stand2.justSold();
   stand2.justSold();
   stand2.justSold();
   stand2.justSold();

   // lets print sale of stand2 and total sale of all stands
   System.out.println(" "+stand2.toString());
   System.out.println("Total Hot Dog sold by All Stand: "+ HotDogStand.getTotalHotDogSoldCount());

   // stand1 sold three hot dogs
   stand3.justSold();
   stand3.justSold();
   stand3.justSold();

   // lets print sale of stand3 and total sale of all stands
   System.out.println(" "+stand3.toString());
   System.out.println("Total Hot Dog sold by All Stand: "+ HotDogStand.getTotalHotDogSoldCount());

}
}

---------------------------------------------------------------------------------------------

Compilation and output of Program:

Compile:  
javac comjavahotdogHotDogStand.java <enter>
javac comjavappApp.java <enter>
Run Program:
java com.java.app.App <enter>

Output:

Both objects are equals


HotDogStand [Stand id: 123, Total Hot Dog Sold = 1]
Total Hot Dog sold by All Stand: 1

HotDogStand [Stand id: 456, Total Hot Dog Sold = 4]
Total Hot Dog sold by All Stand: 5

HotDogStand [Stand id: 789, Total Hot Dog Sold = 3]
Total Hot Dog sold by All Stand: 8

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote