Applets :: Simulate Stack - Using Push / Pop
May 10, 2014
I have designed an applet to simulate a stack (last-in, first-out) of strings. The applet consists of:
Two JButtons
Two JTextFields
Two JLabels
A TextArea
The user enters text in the first text field then clicks the push button and it goes in the textArea. After entering several strings the user can click the pop button and the top string goes into the second text field. I am a total newbie and am stuck. I can get a string of text to push to the textArea and then pop it out; however I can't stack them LIFO. Most examples I've seen use integers and I'm not sure how to translate the concept to strings. I'm just not sure how the stack concept works even though I read the API webpage.
My code
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JApplet;
import javax.swing.JButton;
[Code] ....
View Replies
ADVERTISEMENT
Apr 26, 2015
This method keeps throwing a NullPointerException. Not only that, but its not doing what I want it to do.
Heres the code:
/*
Remove all the numbers from the queue and push onto the stack until an operator is reached
*/
private static void processQueue() {
// Check what is at the front of the queue. If an operator is found or the queue is empty, exit method.
while(queue.front() != null || !queue.front().equals("*") || !queue.front().equals("/") || !queue.front().equals("%")
|| !queue.front().equals("+") || !queue.front().equals("-"))
{
stack.push(queue.removeFront()); // push the operand onto the stack
}
}
Here is the method that calls the processQueue() method
/*
Process the mathematical expression using a stack and a queue
*/
private static double processExpression() {
// insert all elements from the stack to the queue for processing
while(stack.top() != null) {
queue.insertBack(stack.pop());
[Code] .....
View Replies
View Related
Dec 9, 2014
I'm quite new in android programming and I've meet some problem in push and pull method. I have created 2 activity in my app 1 of them call Push_activity and Pull activity(Code are shown below). The String that I keyed in PushActivity have to "transfer" to the PullActivity when the button is pressed:
PullActivity
but1 = (Button)findViewById(R.id.buttonll);
tx = (TextView)findViewById(R.id.editText1);
tx.setText(getIntent().getStringExtra("extra"));
but1.setonclickListener(new onclickListener(){
[Code] ....
The app works fine when it started. When I press the button in PushActivity, it crashed and the error came from my textview in PullActivity.
View Replies
View Related
Jan 18, 2012
I have some data in the database and values can be added on demand. so when ever the value added to the database i need to promt that message to all users which are accessing my website, so how can i acheive this....
View Replies
View Related
Dec 26, 2013
I am new to applets, and my manifest file is:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Permissions: all-permissions
Created-By: 1.6.0-internal (Sun Microsystems Inc.)
Name: com/myPackage/test/client/TaskApplet.class
SHA1-Digest: pLmraui35IkgfAq+v3WGj1LwCYQ=
The error I get is as follows...When the page is loaded I get the following error: java.lang.SecurityException: class "com.myPackage. test.client. TaskApplet" does not match trust level of other classes in the same package
View Replies
View Related
Oct 18, 2014
Write a method called statistic that simulates the rolling of two dice 1000 times. (1000 times is a parameter of the method statistic).The program should have no input, and should use pseudo random numbers to simulate the rolls (one random number per die). Store the sum resulting from each roll of two dice, determine the number of times each 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12 was rolled. The out produced by the program is:
Sum 2 3 4 5 6 7 8 9 10 11 12
Total 33 49 93 103 127 159 150 124 85 49 28
This is my code:
public static void main(String[] args) {
statistic(1000);
}
public static void statistic(int number) {
Random randomNumbers = new Random();
[code]...
i have been trying for hours to make it look like the output above but no luck. I was wondering if there's any other way writing the code without using arrays.
View Replies
View Related
Nov 2, 2014
Suppose that a certain airport has one runway, that each airplane takes LandingTime minutes to land and TakeOffTime minutes to take off, and that on the average, TakeOffTime planes take off and LandingTime planes land each hour.
Assume that the planes arrive at random instants of time. (Delays make the assumption to randomness quite reasonable.) There are 2 types of queues: a queue of airplanes waiting to land and a queue of airplanes waiting go take off. Because it is more expensive to keep a plane airborne than to have one waiting on the ground, we assume that the airplanes in the landing queue have priority over those in the take off queue.
Write a program to simulate this airport's operation. You might assume a simulated clock that advances in one-minute intervals. For each minute, generate two random numbers: If the first in less than LandingTime /60, a "landing arrival" has occurred and is added to the landing queue, and if the second is less than TakeOffTime /60, a "takeoff arrival" has occurred and is added to the takeoff queue. Next, check whether the runway is free. If it is, first check whether the landing queue is nonempty, and if so, allow the first plane to land; otherwise, consider the takeoff queue. Have the program calculate the average queue length and the average time that an airplane spends in a queue. You might also investigate the effect of varying arrival and departure rates to simulate the prime and slack times of day, or what happens if the amount of time to land or takeoff is increased or decreased.
My Queue Interface: [URL] ....
My Queue Implementation: [URL] ....
My Demo (Main Method) Program: [URL] ....
Right now, I'm struggling with the demo program. I wrote some pseudocode in which I tried to match what the instructions were asking:
for(each minute) {
rand1 = generator.nextInt();
rand2 = generator.nextInt();
if(rand1 < LANDING_RATE / 60)
Add to the landing queue
if(rand2 < TAKE_OFF_RATE/60)
[Code] .....
Firstly, exactly how many minutes am I supposed to be doing this for? The assignment gave me a variable called final int ITERATIONS = 1440 , could that have something to do with how long I loop? Secondly, what exactly do I add to the queue if the conditions are true. For example, if rand1 < LANDING_RATE/60, what would I enqueue to the landingQueue? Thirdly, how am I supposed to check if the runway is free? Does that mean check to see whether the takeOffQueue is empty or not? Fourth, would allowing the first plane to land mean removing an item from the landingQueue? Also, what does it mean by "otherwise, consider the takeoff queue". Does that mean if the landingQueue is empty, I should start removing items from the takeOff queue?
The biggest problem is the fact that I have to calculate the average queue length and average time that an airplane spends in a queue.
View Replies
View Related
Nov 13, 2014
*Write a program to simulate this airport's operation. You might assume a simulated clock that advances in one-minute intervals. For each minute, generate two random numbers: If the first in less than LandingTime /60, a "landing arrival" has occurred and is added to the landing queue, and if the second is less than TakeOffTime /60, a "takeoff arrival" has occurred and is added to the takeoff queue*.
*Next, check whether the runway is free. If it is, first check whether the landing queue is nonempty, and if so, allow the first plane to land; otherwise, consider the takeoff queue. Have the program calculate the average queue length and the average time that an airplane spends in a queue.*
I have most of the code done as you can see below:
* Queue Interface: Queue.java
* Queue Implementation: ArrayQueue.java
* Demo Program: Airport.java
Right now, I am stuck on the first calculation which is trying to figure out the average size of the landing queue. For some reason, as you can see at the bottom of my demo program, the average always comes out to be 0.0 and I'm not sure what's wrong.
Secondly, how to calculate the average time a plane stays inside a queue for. Write a program to simulate this airport's operation. You might assume a simulated clock that advances in one-minute intervals. For each minute, generate two random numbers: If the first in less than LandingTime /60, a "landing arrival" has occurred and is added to the landing queue, and if the second is less than TakeOffTime /60, a "takeoff arrival" has occurred and is added to the takeoff queue.
Next, check whether the runway is free. If it is, first check whether the landing queue is nonempty, and if so, allow the first plane to land; otherwise, consider the takeoff queue. Have the program calculate the average queue length and the average time that an airplane spends in a queue.
I have most of the code done as you can see below:
* Queue Interface: Queue.java
* Queue Implementation: ArrayQueue.java
* Demo Program: Airport.java
Right now, I am stuck on the first calculation which is trying to figure out the average size of the landing queue. For some reason, as you can see at the bottom of my demo program, the average always comes out to be 0.0 and I'm not sure what's wrong.
Secondly, how to calculate the average time a plane stays inside a queue for.
View Replies
View Related
Jul 1, 2014
my assignment is to code a program to simulate the rolling of 2 dice. I have most of it set but when I run it and have it print out how many times each number (2-12) was rolled, it displays the numbers 1 off. for instance it will display the number of 4s rolled next to the 5s.
here it is
import java.util.Scanner; //allows info to be inputted//
import java.util.Random; //opens random number generator//
public class RollTheDiceWithArray13
{
public static void main(String[] args)
[code]....
View Replies
View Related
Feb 25, 2015
Write a program to simulate the rolling of two dice. The program should use Math.random (google how to use Math.random if needed) to roll the first die and should use Math.random again to roll the second die. The sum of the two values should then be calculated your program should roll 50,000 times. Use a single dimensional array to tally the numbers of times each possible sum appear(the possible values are 2 through 12, think about why?). Print the results in a tabular format.
Here's the code I'm working on so far:
public class RollingDice{
public static void main(String[] args){
int die1;
int die2;
int roll;
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
roll = die1 + die2;
System.out.println("The roll of the first dice is: " + die1);
System.out.println("The roll of the second dice is: " + die2);
System.out.println("Your total roll is: " + roll);
}
}
What am I missing at this point, and what parts did I do wrong to change it?
View Replies
View Related
Sep 28, 2014
I need to create and simulate an ATM interface for my computer programming class using scanner class, if else & switch statements. I've been working on this assignment all day and I'm still no closer to figuring out how to do it. I'm currently working in NetBeans to try and solve it. I have attached the pdf , what I need to do. This is what I have so far:
package bankatmifelse;
//Gator Bank ATM Program
import java.util.Scanner;
public class BankATMIfElse {
[code]...
View Replies
View Related
Jul 1, 2014
I'm on windows 8 and I want to use notepad or notepad++ to simulate the press of the space bar every 15 minutes so my computer doesn't auto lock.
View Replies
View Related
Jan 20, 2015
import java.util.Random;
/**
* A very basic Dice that can be rolled and represent int values
*/
public class Dice{
private Random rand;
public Dice(){
rand=new Random();
}
/**simulates rolling the dice*/
[Code] ....
Why the Random rand is initiated at the Constructor and not at roll? Like the code works for both but I remember our teacher telling us something about one uses more memory than the other.
View Replies
View Related
Apr 26, 2015
Write a program to simulate the operation of a simple robot. The robot moves in four directions: Forward, backwawrd, left, and right. The job of the robot is to move items and place it in the right slots in each station. There are 8 stations plus the pickup station. Pick up station is the initial start where the robot picks the items. 8 items can be placed on each station. The nearest station must be filled before placing items on the next station. The items are marked with identification or serial numbers. The items with odd serial number go to the right and items with even number go to the eft. The last slot, number 7 is reserved for light items which are less than 80kg. The serial number is a five digit number, the MSB digit 5 indicates that the product must be placed in the fifth station which is keeping the product at 20 degree F.
View Replies
View Related
Sep 9, 2014
How can i write a java program that simulate a simple calculator that performs the basic arithmetic operations of JAVA. (JOption Pane and Repeat control structure)
Example : Addition, Subtraction, Multiplication, Division.
The program will prompt to input two floating point numbers and a character that represents the operations presented above. In the event that the operator input is valid, prompt the user to input the operator again.
Sample Output :
First number 7
Second number 4
Menu
<+> Addition
<-> Subtraction
<*> Multiplication
</> Division
Enter Choice: * <enter>
The answer is 28.
View Replies
View Related
Nov 13, 2014
Write a program to simulate this airport's operation. You might assume a simulated clock that advances in one-minute intervals. For each minute, generate two random numbers: If the first in less than LandingTime /60, a "landing arrival" has occurred and is added to the landing queue, and if the second is less than TakeOffTime /60, a "takeoff arrival" has occurred and is added to the takeoff queue.
Next, check whether the runway is free. If it is, first check whether the landing queue is nonempty, and if so, allow the first plane to land; otherwise, consider the takeoff queue. Have the program calculate the average queue length and the average time that an airplane spends in a queue.
I have most of the code done as you can see below:
* Queue Interface: Queue.java
* Queue Implementation: ArrayQueue.java
* Demo Program: Airport.java
Right now, I am stuck on the first calculation which is trying to figure out the average size of the landing queue. For some reason, as you can see at the bottom of my demo program, the average always comes out to be 0.0 and I'm not sure what's wrong.
Secondly, how to calculate the average time a plane stays inside a queue for.
View Replies
View Related
Sep 4, 2014
this is what I clobbered together for the O'Reilly Java 2 Course, final assignment. There may well be several issues still with this code, my main grief at the moment is: The mouseReleased does not react (it did in the assignment I lifted it from and made only minor changes). No matter where I click on the grid, nothing happens.
import java.awt.event.MouseAdapter;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.Container;
[code]....
View Replies
View Related
May 14, 2014
I do not think this is possible, but I'd like to confirm. If I have an HTML page that has an embedded Java applet, and that applet in turn renders an HTML document (within the applet window), is the HTML *within* the applet part of / accessible through the DOM for the parent page?
View Replies
View Related
Jan 31, 2014
We are using an applet in our web application. The applet of our application is dependent on bouncycastle jar,bcprov-jdk15.jar and few other jar's whose size comes around 4 mb. When using the appliaction on jre7, the applet is taking too long time to load than usual time. Is there any way to place these jar's in client machine? Will it improve the performance? Is there any other way to reduce the loading time of applet apart from placing jars in client machine?
View Replies
View Related
Feb 26, 2014
I have a simple applet that doesn't stray outside the sandbox. It used to work fine before Java 7 but now it craps out with security warnings. It does nothing but play a game, it doesn't even save the state of the game. This is the applet, it's a very simple chess program.
The warnings and popups I'm getting are:
activate javaTM platform SE 7U?allow now?activation blocked by security settings - I never changed any security settings and as I said, this applet stays firmly in the sandbox so I can't see what the issue is.Viewing the java error window I see
Java Plug-in 10.51.2.13
Using JRE version 1.7.0_51-b13 Java HotSpot(TM) Client VM
User home directory = C:UsersMike
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
[code]....
why it shows the C:UsersMike directory in the above listing, there is no access to files etc.The applet plays fine if I run it in appletviewer.What do I need to do to make this simply work in anyone's browser? I have a similar problem with a (rather funky) Connect 4 program I wrote. It worked fine for years and then just stopped with the Java 7.
View Replies
View Related
Jan 5, 2015
is this program correct? for Implementing two Stack in one array. how can i solve it ?
public class Stackation11 {
int max = 10;
int [] array = new int[max];
int top;
int top1;
Stackation11(){
[code]....
View Replies
View Related
Apr 24, 2014
after i am done calculating everything from numbers stack, i pop the last number and return it... my question is how can i catch an exception if the size of my numbers stack is greater than 1;
public static String evaluate(String input) {
char[] a = input.toCharArray();
if (input.isEmpty())
return "No input";
else if (input.equals(" "))
return "No input";
else if (input.equals(" "))
[code]....
View Replies
View Related
Apr 11, 2014
Write a method compressDuplicates that accepts a stack of integers as a parameter and that replaces each sequence of duplicates with a pair of values: a count of the number of duplicates, followed by the actual duplicated number. For example, suppose a variable called s stores the following sequence of values:
bottom [2, 2, 2, 2, 2, -5, -5, 3, 3, 3, 3, 4, 4, 1, 0, 17, 17] top
If we make the call of compressDuplicates(s);, after the call s should store the following values:
bottom [5, 2, 2, -5, 4, 3, 2, 4, 1, 1, 1, 0, 2, 17] top
This new stack indicates that the original had 5 occurrences of 2 at the bottom of the stack followed by 2 occurrences of -5 followed by 4 occurrences of 3, and so on. This process works best when there are many duplicates in a row. For example, if the stack instead had stored:
bottom [10, 20, 10, 20, 20, 10] top
Then the resulting stack after the call ends up being longer than the original:
bottom [1, 10, 1, 20, 1, 10, 2, 20, 1, 10] top
If the stack is empty, your method should not change it. You may use one queue as auxiliary storage to solve this problem. You may not use any other auxiliary data structures to solve this problem, although you can have as many simple variables as you like. You may not use recursion to solve this problem. For full credit your code must run in O(n) time where n is the number of elements of the original stack.
I wrote a code but still having a problem with it , am I allowed to use 3 while loops ?
public void compressDuplicates(Stack<Integer> s ){
Stack<Integer> backup= new Stack<Integer>();
int count = 1;
while(!s.isEmpty()){
int temp = s.pop();
[Code] .....
// example 1
Expected output : bottom [5, 2, 2, -5, 4, 3, 2, 4, 1, 1, 1, 0, 2, 17] top
My output: //bottom [17, 2, 2, 2, 2, 2, -5, -5, 3, 3, 3, 3, 4, 4, 1, 0, 17, 17] top
View Replies
View Related
May 2, 2015
For my classes I wrote I have puts strings into a stack and also a queue and am wondering how to take the top of the stack and the front of the queue and turn those into strings in my main class and run them through while loops that will detect if they are palindromes or not. Right now I am trying to peek and use first to put in my while loop but they don't work with the .charAt because they are not considered strings I think.
import java.util.Stack;
public class Palindrome {
public static void main(String[] args) {
// TODO Auto-generated method stub
String enteredLine;
int leftStack, rightStack;
int leftQueue, rightQueue;
[Code] .....
View Replies
View Related
Jun 16, 2014
In my application a series of inputs and a calculated result. At the end of each loop these inputs and calculations are displayed. After the loop is over with the user does not enter a string that is "y" or "Y", I want these inputs and the calculation to be displayed in a First in First Out format or a stack. I am using a LinkedList that is used in a class creating a stack.
Here is the code for my stack.
Java Code:
import java.util.LinkedList;
public class GenericStack<E> {
LinkedList<E> stack = new LinkedList<>();
public void Push(E element) {
stack.addFirst(element);
[Code] ....
Here is the code containing the main method. The methods other than the main method are probably not relevant to the problem, but take a look if you like.
Java Code:
import java.util.*;
import java.text.*;
public class FutureValueApp
{
public static void main(String[] args) {
GenericStack<String> stack = new GenericStack<>();
[Code] ....
The stack seems to be adding the same inputs and the same calculation from the first loop, even when it is on it's 2nd or third loop. I am getting this output.
Java Code:
Monthly Inv.Int. RateYearsFuture Value
$5.002.0%5$315.76
$5.002.0%5$315.76 mh_sh_highlight_all('java');
View Replies
View Related
Jan 3, 2015
I tried to write a program to implement a stack but it has a bug I am unable to solve.The bug is that whenever I choose an operation to perform, eg push, After performing the operation, the loop is executed once again and Invalid choice message appears, i.e. the default case. And then the loop again executes to choose further option. Here is my code
class Stack {
private char[] stck;
private int len;
private int top;
[code]....
View Replies
View Related