//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:
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.
Implement and test a "contains" method for the LinkedStack class in the following UML. Your method...
(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 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 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 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 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 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 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 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 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 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...