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

A. Objectives 1. Implement and use Stack class and methods (20 points) 2. Use a

ID: 3807418 • Letter: A

Question

A. Objectives 1. Implement and use Stack class and methods (20 points) 2. Use a separate class to represent objects for Stack (10 points) 4. Use methods to do separate and repetitive work. The main method should not have more than 20 lines of code (10 points 5. Implement the project correctly. (40 points) 10 points will be awarded for use of meaningful identifiers, consistent indentation, explanatory comments in your code and properly formatted output B. Description For a variety of reasons, human beings track the food they intake. Assume you are working for a healthcare analytics company, and need to gather information about the eating habits of your clients. You need to create a software that helps in doing your work. For this purpose, you write a menu driven program with the following options: l. Register client and create client login (clicnt supplies the clientID and password, and thc clientID must be unique) 2. Login existing client 3. Add a food item client ate 4. Check the complete history of food a client ate in a day in reverse order (all food items listed here from dinner to breakfast 5. Check all the different types of food items a client ate in a day (all food items listed here only once) 6. Total calories eaten in a day 7. Food with the maximum calorie in a day 8. Food that the client ate maximum number of times in a day 9. Logout

Explanation / Answer

//A)

package sta;

import java.util.Arrays;

public class StackOfInts {

   private int[] items = new int[10]; // Holds the items on the stack.
     
   private int top = 0; // The number of items currently on the stack.
     
   /**
   * Add N to the top of the stack.
   */
   public void push( int N ) {
   if (top == items.length) {
   // The array is full, so make a new, larger array and
   // copy the current stack items into it. (Note that
   // java.util.Arrays must be imported.)
   items = Arrays.copyOf( items, 2*items.length );
   }
   items[top] = N; // Put N in next available spot.
   top++; // Number of items goes up by one.
   }
     
   /**
   * Remove the top item from the stack, and return it.
   * Throws an IllegalStateException if the stack is empty when
   * this method is called.
   */
   public int pop() {
   if ( top == 0 )
   throw new IllegalStateException("Can't pop from an empty stack.");
   int topItem = items[top - 1]; // Top item in the stack.
   top--; // Number of items on the stack goes down by one.
   return topItem;
   }
     
   /**
   * Returns true if the stack is empty. Returns false
   * if there are one or more items on the stack.
   */
   public boolean isEmpty() {
   return (top == 0);
   }
     
     
   /**
   * Returns false if the stack is empty or item is not found,else returns true.
   *
   */
     
     
     
   public boolean search(int item)
   {
       if(this.isEmpty())
       {
           return false;
       }
       else
       {
           for(int i=0;i<items.length;i++ )
                 
           {
               if(items[i]==item)
               {
                   return true;
               }
           }
             
             
             
           return false;
       }
         
         
   }
     

   } // end class StackOfInts

//starting of StackDriver class

package sta;

import java.util.Scanner;

public class StackDriver {

   static StackOfInts stack=new StackOfInts();
   static Scanner read=new Scanner(System.in);
  
   public static void main(String args[])
   {
       int option=0;
  
       System.out.println("menu");
       System.out.println("1.Add 2.Remove 3.Search 4.Quit ");
      
       while(option!=4)
       {
       System.out.println("pls select an option");
       option=read.nextInt();
       perform(option);// performs appropriate action based on option
  
       }
       read.close();//free the resource
      
   }
  
      

   public static void perform(int option)
   {
       switch (option) {
case 1: System.out.println("pls enter numeber to add");
stack.push(read.nextInt());
break;
case 2: System.out.println("number "+stack.pop()+" is removed");
break;
case 3: System.out.println("pls enter numeber to search");
if(stack.search(read.nextInt()))
{
   System.out.println("number found");
  
}
else
{
   System.out.println("number not found");
}
  
break;
case 4: System.out.println(" bye!");;
break;

default:System.out.println("Invalid");
break;
}
   }
  
}