Using NetBeans Problem 1 Stacks use the class StackLL withou

Using NetBeans

Problem 1: Stacks (use the class StackLL without adding any method to it)

Create a new Java Application that has the following methods:

A method to generate a number of element between two given values and save them in a stack.

A method to print a stack (10 elements per line). The original stack should remain as is after the print.

A method to search for a value in a stack. The stack should remain the same after the search is finished.

A method to print the second element of the stack and leave the original stack unchanged

A method to count the number of elements in a stack that are larger than a given value and leave the original stack unchanged.

A method to inverse a stack.

A method to make a copy of a stack into another stack

In the main method test all your methods including the following cases:

-Create 3 Stacks S1, S2, S3

- Insert 20 integers between 20 and 60 ( do not insert duplicates) into S1

-Insert 30 integers between 10 and 80 (do not insert duplicates) into S2.

Solution

a)public static int getRandom(int from, int to)

{

if (from < to)

return from + new Random().nextInt(Math.abs(to - from));

return from - new Random().nextInt(Math.abs(to - from));

}

b)

public static void main(String[] args)

{

int[] numbers = new int[10]; //Generates 10 Random Numbers in the range 1 -20

for(int i = 0; i < numbers.length; i++)

{

numbers[i] = (int)(Math.random()*20 + 1);

}//end for loop

System.out.println(\"Numbers Generated: \" + Arrays.toString(numbers));

}

c)

import java.util.Stack;

public class MainClass {
public static void main (String args[]) {
   Stack s = new Stack();
   s.push(\"A\");
   s.push(\"B\");
   s.push(\"C\");

   System.out.println(\"Next: \" + s.peek());
   s.push(\"D\");

   System.out.println(s.pop());
   s.push(\"E\");
   s.push(\"F\");

   int count = s.search(\"E\");
   while (count != -1 && count > 1) {
     s.pop();
     count--;
   }
   System.out.println(s);
}
}
d)

if(stck.size() < 2)

throw \"Stack has no second element!\";

Object obj = stck.top();

stck.pop();

Object ret = stck.top();

stack.push(obj); return ret

e)

#include<iostream>

#include<stack>

using namespace std;

stack<int> s;

int BottomInsert(int x){

  if(s.size()==0) s.push(x);

  else{

      int a = s.top();

      s.pop();

      BottomInsert(x);

      s.push(a);

  }

}

int reverse(){

  if(s.size()>0){

      int x = s.top();

      s.pop();

      reverse();

      BottomInsert(x);

  }

}

int main()

{

  int n,a;

  cin>>n;

  for(int i=0;i<n;i++){

      cin>>a;

      s.push(a);

  }

  reverse();

  return 0;

}

f)

The simplest way of copying stack is to clone it

Stack temp = a.clone();

Other wise get all elements in the stack by calling method toArray() and

copy each of them into new stack;

Stack temp = new Stack(a.size());

Object[] obj = a.toArray();

for(int i=0; i<obj.length; i++)

temp.push(obj[i]);

g)

mysql> create table foo (id serial primary key, u int, unique key (u));

mysql> insert into foo (u) values (10);

mysql> select * from foo;

mysql> show create table foo\\G CREATE TABLE `foo` (

`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `

u` int(11) DEFAULT NULL,

PRIMARY KEY (`id`), UNIQUE KEY `u` (`u`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 mysql> insert into foo (u) values (10) on duplicate key update u = 20;

mysql> select * from foo;

mysql> show create table foo\\G CREATE TABLE `foo` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,

`u` int(11) DEFAULT NULL,

PRIMARY KEY (`id`),

UNIQUE KEY `u` (`u`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1

Using NetBeans Problem 1: Stacks (use the class StackLL without adding any method to it) Create a new Java Application that has the following methods: A method
Using NetBeans Problem 1: Stacks (use the class StackLL without adding any method to it) Create a new Java Application that has the following methods: A method
Using NetBeans Problem 1: Stacks (use the class StackLL without adding any method to it) Create a new Java Application that has the following methods: A method
Using NetBeans Problem 1: Stacks (use the class StackLL without adding any method to it) Create a new Java Application that has the following methods: A method

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site