Question

12. Implement and test a contains method for the LinkedStack dass in the following UML. Your method should return True if the item is found in the LinkedStack. Also make sure that your method is a helper method. (5 pts) ecinterface pop svold top s0b Ject Lotepty0 sboolean StacknderflovExceptLo SeackUnderfiovException(Sering sessage) push (Object elenent) swoldpuh (Ob fect lement)od StackOverflovxton StackoverflovExceptlon(String message doPu1booleam aStack op:LLObjeetlode DEPCAPiint stack:Ob feet LinkedStack push objeet elementvoid pop ) svold op () 0bjeet LoEapty0 beolean opIndexint ArrayStack push (Objeet element)sd Key uses op)sObjeet LsEnptysboolean mplement 13. Design and implement a VectorStack using the UML in question 12. You must figure out which instance variables and methods your class should have at first place. (10 pts) 14. Draw the UML diagram of your your VectorStack, place it in the correct spot in the UML Class Diagram given in Question 12. Do not forget to draw the relations between your VectorStack and other classes (arrows). (10 pts)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//LinkedStackImplements.java

import java.util.NoSuchElementException;

/*Node class represent each node of linked stack */

class Node

{

protected int data; //data value in stack node

protected Node link; //next node link in stack

//default constructor

public Node() {

link = null;

data = 0;

}

//argument constructor

public Node(int data, Node link) {

this.data = data;

this.link = link;

}

public int getData() {

return data;

}

public void setData(int data) {

this.data = data;

}

public Node getLink() {

return link;

}

public void setLink(Node link) {

this.link = link;

}

}

class LinkedStack {

protected Node top;

public LinkedStack() {

top = null;

}

public void push(int data)

{

Node nptr = new Node (data, null);

if (top == null)

top = nptr;

else

{

nptr.setLink(top);

top = nptr;

}

}

public void pop()

{

if (isEmpty() )

throw new NoSuchElementException("Underflow Exception") ;

Node ptr = top;

top = ptr.getLink();

}   

/* Function to check the top element of the stack */

public int top()

{

if (isEmpty() )

throw new NoSuchElementException("Underflow Exception") ;

return top.getData();

}

  

/* Function to check if stack is empty */

public boolean isEmpty()

{

return top == null;

}

/*helper function to check for data contains or not*/

public boolean contains(int data)

{

if(top == null)

{

return false;

}

else

{

Node ptr = top;

while (ptr != null)

{

if(ptr.getData() == data )

return true;

ptr = ptr.getLink();

}

}

return false;

}

}

public class LinkedStackImplements

{

public static void main(String[] args) {

LinkedStack stack = new LinkedStack();

stack.push(4);

stack.push(6);

stack.push(16);

stack.push(9);

stack.push(8);

if(stack.contains(88))

System.out.println("Stack contains data");

else

System.out.println("Stack does not contains data");

}

}

//output:

* Project ▼ File ▼ Edit 에〉 codinggrou COMPILE AND EXECUTE JAVA-8 ONLINE >Share Code LinkedStacklmplementsj ava x compile l Ex

codingground COMPILE AND EXECUTE JA VA 8 o NLINE rojec *New Project-20170817日다+ 白a root ,Compile ! Execute ! >Share Code Link

13.

We need to use vector as member variable and use it as a stack.Add elements as end always and remove from end also.

//VectorStackImplements.java

import java.util.NoSuchElementException;

import java.util.Vector;

class VectorStack {

Vector myvector;

public VectorStack() {

myvector = new Vector();

}

public void push(Object obj)

{

myvector.addElement(obj);

}

public void pop()

{

if(myvector.size()>0)

{

myvector.removeElementAt(myvector.size()-1);

}

else

System.out.println("Stack Underflow");

  

}   

/* Function to check the top element of the stack */

public Object top()

{

Object obj = null;

if(myvector.size()>0)

obj=(int) myvector.elementAt(myvector.size()-1);

else

System.out.println("Stack Underflow");

return obj;

}

  

/* Function to check if stack is empty */

public boolean isEmpty()

{

return myvector.size() == 0;

}

}

public class VectorStackImplements

{

public static void main(String[] args) {

VectorStack stack = new VectorStack();

stack.push(4);

System.out.println("Added... "+stack.top());

stack.push(6);

System.out.println("Added... "+stack.top());

stack.push(16);

System.out.println("Added... "+stack.top());

stack.push(9);

System.out.println("Added... "+stack.top());

stack.push(8);

System.out.println("Added... "+stack.top());

System.out.println();

System.out.println("The top of stack now is... "+stack.top());

stack.pop();

System.out.println("The top of stack now is... "+stack.top());

stack.pop();

System.out.println("The top of stack now is... "+stack.top());

stack.pop();

System.out.println("The top of stack now is... "+stack.top());

stack.pop();

}

}

//output:

14.

*pop() :void top s0b feet etnptyOba +puah(Object ·lesent):vo- seh (Object Pu120 8lean ·leseat):vol4 StapkOverflovxen kOvertlo

Add a comment
Know the answer?
Add Answer to:
Implement and test a "contains" method for the LinkedStack class in the following UML. Your method...
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
  • (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...

    (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...

  • this is written in python Goal: Implement and test the Onlinestudent class as shown in the Student/OnlineStudent UML Dia...

    this is written in python Goal: Implement and test the Onlinestudent class as shown in the Student/OnlineStudent UML Diagram Relevant Examples: EmployeeHierarchy PersonArray Pet Input Files: students.txt study-groups.txt The study_group instance variable is a list that contains the username values of the other members in the student's study group Details: 1. Implement the onlinestudent class in the module onlinestudent.py as specified by the Student/OnlineStudent UML Diagram. Onlinestudent is the derived class of the base class student. 2. The_1t__method for an...

  • What this Lab Is About: Given a UML diagram, learn to design a class Learn how...

    What this Lab Is About: Given a UML diagram, learn to design a class Learn how to define constructor, accessor, mutator and toStringOmethods, etc Learn how to create an object and call an instance method. Coding Guidelines for All ments You will be graded on this Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc) Keep identifiers to a reasonably short length. Use upper case for constants. Use title case (first letter is u case)...

  • Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design...

    Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make Chicken and Cow as subclasses of Dairy. The Sheep and Dairy classes implement the Edible interface. howToEat) and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML...

  • Once your UML diagram is complete implement the class naming the Java file MyInteger.java. Create another...

    Once your UML diagram is complete implement the class naming the Java file MyInteger.java. Create another Java file, a program named MyIntegerTester.java that tests all methods in the class. One technique is to write both classes at the same time. As much as possible you implement and test one method at a time. This minimized the amount of new code you have to look at when debugging a method. Only the Version 4.0 “tester” file will have main() method in...

  • UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships...

    UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships Labwork Please read all of the directions carefully. You will create a UML class diagram reflecting the class hierarchy for a fictional program that manages university personnel as constructed according to the graph displayed below. You will need to think about the attributes and behaviors that are unique to each class, and also those attributes and behaviors that are common amongst the subclasses and...

  • Create a UML diagram to help design the class described in exercise 3 below. Do this...

    Create a UML diagram to help design the class described in exercise 3 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Baby class object; should they be private or public? Determine what class methods are required; should they be private or public? Write Java code for a Baby class. A Baby has a name of type String and an age of type integer. Supply two constructors:...

  • 1) This exercise is about Inheritance (IS-A) Relationship. A) First, draw the UML diagram for class...

    1) This exercise is about Inheritance (IS-A) Relationship. A) First, draw the UML diagram for class Student and class ComputerSystemsStudent which are described below. Make sure to show all the members (member variables and member functions) of the classes on your UML diagram. Save your UML diagram and also export it as a PNG. B) Second, write a program that contains the following parts. Write each class interface and implementation, in a different .h and .cpp file, respectively. a) Create...

  • 42) Create a toString method for the LinkedStack class. This method should create and return a...

    42) Create a toString method for the LinkedStack class. This method should create and return a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the LinkedStack class and for testing and debugging applications that use the LinkedStack class. 46) Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

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
ADVERTISEMENT