public class ThreeStackArray { static int row = 10; static int col = 2; int[][] stack = new int[row][col]; //place numbers into the first row of array public void push(int num)
[Code] .....
I found this program idea online. Make a array with three stacks. Either I jump out of bounds if I use "continue" instead of "break" in my loop in the push method. If I use break the output looks like:
12900 0000 0000 0000 0000
if I use continue the output looks like:
An error has occured java.lang.ArrayIndexOutOfBoundsException: 2 1212 1212 1212 1212 1210
I am trying to make a calculator using Java GUI. I've managed to make an ActionListener and add it to a button, but I've made an error in my code that I'm unsure of how to solve. Because of how I've written the code, only one number can be placed in the text field. For example, the an ActionListener for the three button on the calculator was added to the button, but no matter how many times the user presses the button, only one 3 will appear in the text field. The code is below:
import javax.swing.*;//import the packages needed for gui import java.awt.*; import java.awt.event.*; public class Calculator { public static void main(String[] args) { JFrame window = new JFrame("Window");//makes a JFrame window.setSize(300,350);
[code].....
As you can see, because the compiler forces the String variable to be final, so when the user presses the button, the code simply shows how a space character and three character would look like, because the String variable can't change. How do I write my code so that every time the user presses the button, a character is added to the text field?
I want to make a calculator program as my first ever program and I've been working on the layout and I think I have it close to where I want it but will probably tweak it in the future after I get it to work. My problem is I'm trying to add a MenuBar to it but it is not showing up. I tried having it all over my code but whereever I put it the MenuBar doesn't show up.
I have to make a gpa calculator for 5 courses of grades A B+ C+ D F, and worth 4, 3.5, 3, 2.5, 2,1,0 respectively. using 10 input text boxes, and a button
Create two arrays of length 5: one to hold the numerical values of letter grades, the other to hold the number of credit hours. Initialize both to have 0’s. Write a function that takes a letter grades entered by a user and returns its numerical equivalent.
Use an ‘if…else if… else’ construct.
Write a function that can operate on the two arrays and return the GPA. Use a for loop that runs from 0 to 5, multiplies the corresponding array elements and adds them together, divides that total by the total number of credit hours, and returns that value.
Input elements:
In the first 10 textfields, use the onchange attribute to fill the arrays with user entries.
Hint: For the course grade entries it should look like
onchange= ‘array1[0] = first_function(this.value)’ etc.
The button should make the value of the last text area equal to the return value of the second function.
i have my whole code completed but when i press the button it returns the points value '0' so i think there is an error in my if else statements (ltonum function)
<DOCTYPE! html> <html> <head> <title> Assignment 9: Javascript 3 </title> <!-- Assignment 9, Due November 8, 2014, Jamie Zajac, TA Kartik--> <style> body { background-color: lemonchiffon; color: midnightblue;
So we have to ask the user to put in a string of letters, and bring those letters in as cars to where there is a storage area and an assembly area, and we have to sort them from there into the assembly area with the smallest (A) at the head. I think I set up my code pretty well, but when I run it, no matter what I put in it returns CBAo. Say I input KATE, it should return TKEA but instead CBAo or if I input JANICE it should return NJIECA but it just returns EDCBAo. Here's my code:
import java.util.Scanner; import java.util.Stack; public class carStacksDessart { public static void main(String[] args) { Stack<Integer> storage = new Stack();
I am having some trouble with this program. I am getting only one result to print when it should show all the solutions. Also the 1 solution I am getting is only printing 7 queens not 8.
import java.util.Stack; public class Queen1 { boolean conflict, complete = false; public int solve(int n) { //create a stack //each element stores the position of the queen on a different row Stack<Integer> s = new Stack<Integer> ();
I am trying to write a program that checks for parentheses matching using stacks.This is my ArrayStack class.
public class ArrayStack{ private final int DEFAULT_SIZE=10; public int tos; Object[] array; public ArrayStack(){ array=new Object[DEFAULT_SIZE];
[Code] ....
But the problem is when I compile matching I get an error as unreported exception EmptyStackException.must be caught or declared to be thrown. I think the problem is with exceptions which I don't have a good knowledge of.
I'm supposed to use stacks (implemented with an array) to check to see if a string is a palindrome. I've finished all my classes and methods, but I'm getting an ArrayIndexOutOfBoundsException when I try to run my demo program.Here are my classes:
public interface Stack { // Creates an empty stack public void initializeStack() // Returns true if the stack is empty, returns false otherwise public boolean isEmpty(); // The stack can never be full, so always return false public boolean isFullStack();
I'm getting errors on all the exceptions called EmptyCollectionException. I think this is because the import statement has a error on it but I'm not sure. I'm suppose to add methods for peek, isEmpty, size, and toString methods. I only started isEmpty also am wondering what I have to change from peek method if anything at all.
import jsjf.exceptions.*;//Error on jsjf import java.util.Arrays; public class ArrayStack<T> implements StackADT <T> { //Error on StackADT private final static int DEFAULT_CAPACITY = 100; private int top; private T[] stack;
I am doing a calculator using stacks but when i try to calculate I getting the wrong data example stack contains 8 and user enter -3 stack should change to 5.
I am given the task to create a program that evaluates infix expressions using two generic stacks, one operator stack and one value stack.
This is my GenStack.java file:
import java.util.*; public class GenStack<T>{//T is the type parameter private Node top;//top of stack public class Node {//defines each node of stack T value; Node next;
[Code] ....
I'm having trouble with the eval and apply methods. The eval method doesn't appear to pickup ')' characters, like it doesn't even see them.
So I am supposed to be changing infix notation to postfix notation using stacks. This is simply taking a string "3 + 5 * 6" (infix) and turning it into (3 5 6 * +" (postfix).
To do this, we are to scan the string from left to right and when we encounter a number, we just add it to the final string, but when we encounter an operand, we throw it on the stack. Then if the next operand has a higher input precedence than the stack precedence of the operator on the top of the stack, we add that operator to the stack too, otherwise we pop from the stack THEN add the new operator.
I am supposed to be utilizing a hash map but I don't see how you would go about doing this. We are supposed to store operators on the hash map but operators need their own character, input precedence, stack precedence, and rank. How do you use a hash map when you need to tie a character to 3 values instead of just 1? I just don't get it.
The following is our Operator class that we are to use. Another problem is this isn't really supposed to be modified, yet we were given two important variables (inputPrecedence and outputPrecedence) that we can't have nothing to be initialized to and no way of accessing? So that might be where a hash map comes in but I am not sure. I am not very sure on how they exactly work anyway...
public class Operator implements Comparable<Operator> { public char operator; // operator privateint inputPrecedence; // input precedence of operator in the range [0, 5] privateint stackPrecedence; // stack precedence of operator in the range [-1, 3]
[Code] ....
So my question mostly revolves around how I tie an Operator character to its required values, so I can use it in my code to test two operators precedence values.
My original thought was turn string into character array, but then I would need nested for/while loops to check if it is a number or letter, or if it is an operator and thus result in O(n^2) time
We are making a tic tac toe game for my CS120 class and I am having trouble figuring out how to make our X's and O's. Is there a way to make shapes besides making two lines for an X and an oval with a white smaller oval inside to make an O? We have only learned the basics so far in class (i.e. events, inheritance, client-supplier, etc.)
These are our instructions:
Write a controller that controls the game. There is one human player (the X player) and the computer player (the O player). The name of the class must be TicTacToeController. In a sense, the controller is the game since the controller will 1) create a TicTacToeModel 2) create a TicTacToeView and 3) create a TicTacToeButton (you must write this class following the design pattern covered in class lectures), a label, and text field such that when the button is pushed, the player moves into the cell selected by the text field. After every player move, the computer moves into a randomly selected empty cell. When the game is over, a text message must be displayed somewhere on the screen the gives the status of the game. While you are free to change the appearance of the controller, the basic elements must be provided (a view of the game, a button, and a text field to enter the cell). A sample screenshot is displayed below.And this is the code i have thus far:
[import java.awt.*; import javax.swing.JFrame; public class TicTacToeView extends Rectangle public TicTacToeView(int x, int y, int w, int h) { super(50,60,w,h); this.setBackground(Color.red); JFrame win = new JFrame("Tic Tac Toe"); win.setBounds(10,10,w+100, h+100); win.setLayout(null); win.setVisible(true); win.setBackground(Color.gray);
So, I'm working on a Tip Calculator for fun. I have everything worked out for the most part. The feature I am trying to add is to give the user the amount of change they'll get back from their transaction. I do not ask the user any information other than what is their bill.
Please enter your bill total : 5.50 Please enter your name : some name Total : 5.50 Tax : 0.48 -- Based off a tax rate of 8.75% Total with Tax : 5.98 -- 20 percent Tip : 1.10 Total with Tax and 20 percent Tip : 7.08 Total change : <-- I believe this will be about $2.92
I want the total bill to round to the nearest $10 value (e.g. for $5.50, calculate bill to $10. For $13.00, calculate bill to the $20.00, and so on... This way I get an accurate amount of change to give back to the user.
I made a MVC calculator. I was wondering if you could take a look at my design and if I was on track. I am still working on getting it to actually calculate something. All the buttons respond and print text on the JTextField but it is not calculating.
package calculator.MVC; import javax.swing.JButton; import javax.swing.JTextField; public class CalculatorModel { private int sum; private int number; private char opt;
I am making a calculator in Java. However, when I press the "=" button on my calculator, it always returns 0.0, no matter what. I don't understand why? The code is below:
import javax.swing.*;//import the packages needed for gui import java.awt.*; import java.awt.event.*; import java.util.Arrays; import java.util.List; import static java.lang.Math.*; public class CalculatorCopy {
import javax.swing.*; public class BasicCalculator{ public static void main (String[] args) { String output=""; double n1,n2; String operation; operation= JOptionPane.showInputDialog("Please enter your operation");
I have the basic workings of this program just doing all the printing at the end is throwing me off. Anyway here are some of the requirements and my code:
1. Allow the user to input an individual's hours worked. 2. Accumulate the total regular hours worked, the overtime hours worked, the amount of the regular pay, the amount of overtime pay, and the total payroll, for all of the employees entered. 3. Allow the user to enter a zero (0) as the Boolean expression to end the loop. 4. When the loop ends print out all of the accumulated totals with appropriate labels, each on a separate line.
import java.util.Scanner; public class WhileLoopPayCalc { public static void main(String [] args) { final double REGULAR_HOURS = 40f; final double HOURLY_WAGE = 12.5f; final double OVERTIME = 1.5f;
I am facing problem during calculation in my awt calculator. It is showing NumberFormatException. Below is part of code.
if(e.getSource()==b14)//On button 14, label is "/" { String v1=tf.getText().toString(); num1=num1+Integer.parseInt(v1);//num1 is as integer variable tf.setText("");//tf means textField }else