Question

On java create the following:

please solve it complete or don't since it considered as part of problem 1 not even the whole question.

  1. The main method that does:
    • Create 2 stacks s1 and s2
    • Insert 23 integers between 20 and 60 (do not insert duplicates) into s1
    • Insert 35 integers between 10 and 80 (do not insert duplicates) into s2.
    • Give the user the choice which methods (2-7) to call and option to exit


LinkedList<Integer> LL = new LinkedList<Integer>) ; //CREATE LINKED LIST C public int push (int V) (/CREATE PUSH METHOD LL.ad
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//dub_stac.java

import java.util.*;
class A
{
   ArrayList<Integer> s=new ArrayList<Integer>();

   String push(int a)
   {
       if(s.contains(a)==false)
       {
          s.add(a);
       return   Integer.toString(a)+" has been inserted";

       }
       return "Dublicate value";
   }

   int pop()
   {
       int b=s.get(s.size()-1);
       s.remove(s.size()-1);
       return b;
   }
}
class B
{
   ArrayList<Integer> s=new ArrayList<Integer>();
   String push(int a)
   {
       if(s.contains(a)==false)
       {

           s.add(a);
           return Integer.toString(a)+"has been inserted";
       }
       return "Dublicate value";
   }

   int pop()
   {
       int b=s.get(s.size()-1);
       s.remove(s.size()-1);
       return b;
   }
}
class dub_stac
{
  
   public static void main(String arg[])
   {
   Scanner sc=new Scanner(System.in);

   //enter integer

       A s1=new A();//stack s1
       B s2=new B();//stack s2
       System.out.println("Insert for stack 1");
       for(int i=0;i<23;i++)
       {
           int a=sc.nextInt();
           if(a>=20 && a<=60)
           {
               String ar=s1.push(a);
               if(ar.equals("Dublicate value"))
               {
                   System.out.println(ar);
                   i--;
                   continue;
               }
               System.out.println(ar);
               continue;
           }
           else
           {
               System.out.println("Insert in Range");
               i--;
           }
       }

       System.out.println("Insert for stack 2");
       for(int i=0;i<35;i++)
       {
           int a=sc.nextInt();
           if(a>=10 && a<=80)
           {
               String ar=s1.push(a);
               if(ar.equals("Dublicate value"))
               {
                   System.out.println(ar);
                   i--;
                   continue;
               }
               System.out.println(ar);
               continue;
           }
           else
           {
               System.out.println("Insert in Range");
               i--;
           }
       }

       while(true)
       {
           System.out.println("methods (2-7) -1 to exit");
           int a=sc.nextInt();
           int f=0;
           switch(a)
           {
               case 2:
               //method 2
               break;
               case 3:
               //method 3
               break;
               case 4:
               //method 4
               break;
               case 5:
               //method 5
               break;
               case 6:
               //method 6
               break;
               case 7:
               //method 7
               break;
               case -1:
               f=1;
               break;

           }
           if(f==1)
               break;
       }


   }
  
}

//Thank you

Add a comment
Know the answer?
Add Answer to:
On java create the following: please solve it complete or don't since it considered as part...
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
  • Create a new Java Application that test MyStack by having the following methods in main class:...

    Create a new Java Application that test MyStack by having the following methods in main class: 1. A method to generate a number of element between two given values and save them in a stack 2. A method to print a stack (10 elements per line). The original stack should remain as is after the print 3. A method to exchange the first element and the last element in a stack 4. A Boolean method to search for a value...

  • how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 }...

    how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...

  • how would I complete this code without calling any built-in java collection framework classes like ArrayList,...

    how would I complete this code without calling any built-in java collection framework classes like ArrayList, LinkedList, etc? import java.util.Iterator; class CallStack<T> implements Iterable<T> { // You'll want some instance variables here public CallStack() { //setup what you need } public void push(T item) { //push an item onto the stack //you may assume the item is not null //O(1) } public T pop() { //pop an item off the stack //if there are no items on the stack, return...

  • JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array...

    JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an ...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private...

  • 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...

  • java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

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