Question

Java: Code a class encapsulating a stack of clothes using an array. A clothing item has...

Java: Code a class encapsulating a stack of clothes using an array. A clothing item has the following attributes: names, color, and whether it can be washed at high temperature. Limit your stack to 20 clothing items. In addition to creating your push, pop, and peek methods; create two additional methods: a method that return all of the clothing items of a given color; a method that returns how many clothing items in the stack can be washed at high temperature. Write a program to test your stack with all these methods.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//##################### PGM START ###########################

//class for Clothing
class Clothing{
  
   //class variables
   String name;
   String color;
   boolean isHigh;
   //constructor
   Clothing(String n,String c,boolean h){
       name=n;
       color=c;
       isHigh=h;
   }
  
}

class Stack
{
    static final int MAX = 20;
    int top;
    Clothing a[] = new Clothing[MAX]; // Maximum size of Stack

    //method to check stack empty or not
    boolean isEmpty()
    {
        return (top < 0);
    }
    //constructor
    Stack()
    {
        top = -1;
    }

    //method to push clothing object to stack
    boolean push(Clothing x)
    {
        if (top >= (MAX-1))
        {
            System.out.println("Stack Overflow");
            return false;
        }
        else
        {
          
            a[++top] = x;
            System.out.println(x.name + " pushed into stack");
            return true;
        }
    }
    //method to find number of high temp clothes
    int highTemp(){
       int count=0;
       for(int i=0;i<=top;i++){
           if(a[i].isHigh)
               count+=1;
       }
       return count;
    }
    //method to get clothes of a color
    String findCloths(String c){
       String out="";
       for(int i=0;i<=top;i++){
           if(a[i].color.equalsIgnoreCase(c))
               out+=a[i].name+"\n\t";
       }
       return out;
    }
    //mehtod to pop an object from stack
    Clothing pop()
    {
        if (top < 0)
        {
            System.out.println("Stack Underflow");
            return null;
        }
        else
        {
            Clothing x = a[top--];
            return x;
        }
    }
  
    //main method
    public static void main(String args[]) {
      
       //creating object of stack
       Stack s = new Stack();
      
       //pushing elements to stack
        s.push(new Clothing("pants","blue",true));
        s.push(new Clothing("shirt","black",true));
        s.push(new Clothing("trousers","black",false));
      
      
        System.out.println("\nNumber of cloths that can be washed in high temp: "+s.highTemp());
        System.out.println("Names of cloths of color black:\n\t"+s.findCloths("black"));
      
        Clothing p=s.pop();
        if(p!=null)
           System.out.println("Element poped from stack=> NAME: "+p.name+"\tCOLOR: "+p.color+"\tHigh_Temp_Washable: "+p.isHigh);
      
      
        System.out.println("Number of cloths that can be washed in high temp: "+s.highTemp());
        System.out.println("Names of cloths of color black:\n\t"+s.findCloths("black"));
  
    }
}

//############################ PGM END #############################

OUTPUT
#########

Add a comment
Know the answer?
Add Answer to:
Java: Code a class encapsulating a stack of clothes using an array. A clothing item has...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

  • Create an array-based implementation of a stack. Each element of the stack should store a string....

    Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a string),...

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

  • C++ Create an array-based implementation of a stack. Each element of the stack should store a...

    C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() =...

    template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...

  • Create a Java Program that uses a Stack to implement a deck of cards. Import java.util.stack...

    Create a Java Program that uses a Stack to implement a deck of cards. Import java.util.stack and use its classes and methods. Note that java.uitl.stack uses peek instead of top (as we did in class). Look up the documentation for java.uitl.stack to see what methods it contains and how to call them. Create a Stack of cards that will hold strings. Push new items to the stack (Use "Qclubs" for Queen of clubs and so on) After your 'deck' has...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings...

    JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings in the stack b. A private data field size to store the number of strings in the stack c. A constructor to construct an empty stack with a default capacity of 4 d. A constructor to construct an empty stack with a specified capacity e. A method empty() that returns true if the stack is empty f. A method push(String value) that stores value...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT