import java.util.Scanner;
import java.util.Stack;
public class StackMerging {
public static void main(String[] args) {
int count1,count2;
Stack<Integer> p1 = new
Stack<Integer>(); // Instance of Stack P1
Stack<Integer> p2 = new
Stack<Integer>(); // Instance of Stack P1
Stack<Integer> p0 = new
Stack<Integer>(); // Instance of Stack P1
Scanner s = new
Scanner(System.in);
System.out.println("Enter no. of
elements of stack1:");
count1 = s.nextInt();
System.out.println("Enter no. of
elements of stack2:");
count2 = s.nextInt();
System.out.println("Enter elements
into stack1:");
for (int i=0;i<count1;i++)
{
p1.push(s.nextInt()); // Pushing into Stack
}
System.out.println("Enter elements
into stack2:");
for (int i=0;i<count2;i++)
{
p2.push(s.nextInt()); // Pushing into Stack
}
System.out.println("Elements of p1
before merging:"+p1);
System.out.println("Elements of p2
before merging:"+p2);
p0 = stackMerge(p1,p2); //
static method for Merging p1,p2 stacks
System.out.println("Elements of p0
after merging is:"+p0);
}
private static Stack<Integer>
stackMerge(Stack<Integer> p1,
Stack<Integer> p2) {
Stack<Integer> p0 = new
Stack<Integer>();
int i=0,j=0;
while(i<p1.size()||j<p2.size()){ // Pushing into Stack P0 by
retrieving elements from P1 and P2
if(i<p1.size()){
p0.push(p1.get(i));
i++;
}
else{
if(!p0.contains(p2.get(j))) // Removing
duplicates by checking in the P0
p0.push(p2.get(j));
j++;
}
}
return p0;
}
}
Program in Netbeans Write a method Stack Merge (Stack p1, Stack p2) to return a stack...
Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....
Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...
In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList) a) Create a DateTime class 1. Add day, month, year, hours, minutes as attributes 2. Add a constructor and a toString() method 3. Implement the Comparable interface, and add a CompareTo() method 4. Add methods to get and set all attributes. b) Add to MyLinkedList class the following methods: 1. Insert a Node to a particular position in the List 2. Insert a Node...
Java generics Write a program that calls a method to multiply two integers and return the integer result. Modify the program to make the method generic, calling it with two generic data types and then returning the result in the same type which also has to be generic. Call the method two times, once with an integer and once with a double in your main program and display the results
Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The application will be to add large numbers. Review adding large numbers Remember that we can use stacks to safely add integer values that overflow the int data type g. in Java, the maximum possible int value Integer.MAX_VALUE is: 2147483647 so any int addition larger than this will overflow and fail Using stacks to add large numbers safely Will actually represent the large integers to...
Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...
JAVA PROGRAM: public class Stack { private DList mList; public Stack() { setList(new DList<>()); } private int indexOfTop() { return getList().isEmpty() ? -1 : getList().getSize() - 1; } public E peek() { return getList().get(indexOfTop()); } public E pop() { return getList().remove(indexOfTop()); } public Stack push(E pData) { getList().append(pData); return this; } private DList getList() { return mList; } private void setList(DList pList) { mList = pList; } Q1 Because Stack encapsulates an instance variable which is of the class DList,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once. This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...
Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...