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

Lab #5-Classes and Objects (cont.) NAME Problem: Complete Programming Project 1

ID: 3606036 • Letter: L

Question

Lab #5-Classes and Objects (cont.) NAME Problem: Complete Programming Project 1 (pg. 339, Savitch). 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. · Requirements: You will submit the following with this lab (face-to-face sections). Online sections will turn their labs in through Blackboard as usual: The assignment sheetlcover sheet with your name on the front b. A printout of source code (program listing). c. A copy of the source code and related project files will be placed in a directory on the I: drive as per the usual directions:

Explanation / Answer

The functionalities of all the methods of both the classes are already defined and described in the question. I have used the same and implemented them. I hope this helps.

If you have and more queries, Kindly comment.

Kindly go through the sample outputs for both the classes to understand better. main() method is the driver for both the classes.

************************************************************************************************************************

Q1.

Code:


public class HotDogStand {

   private int hotDogStandId;
   private int hotDogSold;
   public static int totalSold=0;
  
   //Non Parameterized constructor
   public HotDogStand() {
       hotDogStandId=0;
       hotDogSold=0;
   }
   //Parameterized constructor
   public HotDogStand(int hotDogStandId, int hotDogSold) {
       this.hotDogStandId=hotDogStandId;
       this.hotDogSold=hotDogSold;
   }
   //Copy constructor
   public HotDogStand(HotDogStand hotDogStand) {
        hotDogStandId=hotDogStand.hotDogStandId;
        hotDogSold=hotDogStand.hotDogSold;
    }
   public int getHotDogStandId() {
       return hotDogStandId;
   }
   public void setHotDogStandId(int hotDogStandId) {
       this.hotDogStandId = hotDogStandId;
   }
   public int getHotDogSold() {
       return hotDogSold;
   }
   public void setHotDogSold(int hotDogSold) {
       this.hotDogSold = hotDogSold;
   }
   @Override
   public String toString() {
       return " HotDogStand [hotDogStandId=" + hotDogStandId + ", hotDogSold=" + hotDogSold + "] ";
   }
   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + hotDogSold;
       result = prime * result + hotDogStandId;
       return result;
   }
   @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 (hotDogSold != other.hotDogSold)
           return false;
       if (hotDogStandId != other.hotDogStandId)
           return false;
       return true;
   }
   public void justSold() {
       setHotDogSold(getHotDogSold()+1);
       totalSold++;
   }
   public static void main(String[] args) {
       HotDogStand stand1 = new HotDogStand(1001,0);
       HotDogStand stand2 = new HotDogStand(1002,0);
       HotDogStand stand3 = new HotDogStand(1003,0);
       System.out.println("The three stands in the begining are: "+stand1+stand2+stand3);
      
       System.out.println("Let us sell some hotdogs!");
       stand1.justSold();
       stand1.justSold();
       stand1.justSold();
       stand1.justSold();
       stand2.justSold();
       stand2.justSold();
       stand2.justSold();
       stand3.justSold();
       stand3.justSold();
      
       System.out.println("The three stands are: "+stand1+stand2+stand3);
       System.out.println("The three stands after selling some hotdogs are: "+stand1+stand2+stand3);
       System.out.println("Total hotdogs sold are: "+totalSold);
   }
  
}

Sample output:


*********************************************************************************************************************************

Q2.

Code:

import java.util.Scanner;

public class Fraction {

   private int numerator;
   private int denominator;
   //Parameterized constructor
   public Fraction(int numerator, int denominator) {
       this.numerator = numerator;
       this.denominator = denominator;
   }
   //Non Parameterized constructor
   public Fraction() {
       this.numerator = 1;
       this.denominator = 1;
   }
   //Copy constructor
   public Fraction(Fraction fraction) {
       this.numerator = fraction.numerator;
       this.denominator = fraction.denominator;
   }
   public int getNumerator() {
       return numerator;
   }
   public void setNumerator(int numerator) {
       this.numerator = numerator;
   }
   public int getDenominator() {
       return denominator;
   }
   public void setDenominator(int denominator) {
       this.denominator = denominator;
   }
   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + denominator;
       result = prime * result + numerator;
       return result;
   }
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Fraction other = (Fraction) obj;
       if (denominator != other.denominator)
           return false;
       if (numerator != other.numerator)
           return false;
       return true;
   }
   @Override
   public String toString() {
       return "The Fraction is: "+numerator+"/"+denominator;
   }
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the numerator: ");
       int numerator=sc.nextInt();
       System.out.println("Enter the denominator: ");
       int denominator=sc.nextInt();
       Fraction fraction = new Fraction(numerator, denominator);
       System.out.println(fraction);
      
   }
}

Sample outputs:

*********************************************************************************************************************************

I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)