Printing 8 Queens Using Stacks
Nov 16, 2014
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> ();
[Code] ......
View Replies
ADVERTISEMENT
Nov 17, 2014
Having some trouble coding this exercise in JAVA:
Build a backtracking algorithm to place n queens on a chess board of nxn, with no threat to any queen.
Using F=parameter, Print only the first result.
Using the same C=parameter, print every possible inlays.
This should work when running with a 4x4 board and an 8x8 board basically.
View Replies
View Related
Oct 16, 2014
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();
[Code] ....
View Replies
View Related
May 15, 2014
write a code using the stacks?i want the output to produce my name.
View Replies
View Related
Feb 5, 2014
I got to make a java code for my class. I must make a calculator using stacks. I have a code but i got some mistakes. Here is the code
Java Code:
package stackscalc;
import java.util.Scanner;
import java.util.Stack;
import java.util.EmptyStackException;
class Arithmetic {
int length;
Stack stk;
String exp,
[Code] .....
View Replies
View Related
Feb 4, 2014
i got a problem with this program.I am supposed to make a calculator using stacks but it seems that i m stack. i can t get the right recognition
for () and {} []
its not necessary to use all these symbols.just a simple () will do .here s the program
package stackscalc;
import java.util.Scanner;
import java.util.Stack;
import java.util.EmptyStackException;
class Arithmetic
{
int length;
Stack stk;
String exp,
postfix;
[code]....
View Replies
View Related
May 17, 2014
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
View Replies
View Related
May 11, 2014
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.
View Replies
View Related
Oct 23, 2014
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();
[code]...
View Replies
View Related
Feb 21, 2014
So I have 3 classes in this project, and When it compiles, I get a null pointer.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Project2 {
[Code] .....
View Replies
View Related
Apr 20, 2015
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;
[Code] .....
View Replies
View Related
Nov 23, 2014
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.
package comp10152.lab5;
import java.util.Scanner;
import java.util.Stack;
[Code].....
View Replies
View Related
May 12, 2014
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.
View Replies
View Related
Apr 7, 2014
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
View Replies
View Related
Nov 6, 2014
I'm making a program where the user enters an ID number and can submit the amount of sales that person made for a specific product and write to a file. One of the things I have to do is making a running sum of the sales for each product for all people combined as well as an accumulation of the a persons sales every time he enters data. Everything seems to be working execpt my values like person1Total, person2Total...etx and product2Total.
When I try to write them to the file, they always come out at 0.0 even though they should be the sum of the indexes of the array sales.
import java.io.*;
import java.util.Scanner;
public class SalesReport {
// private static double p1Total, p2Total, p3Total, p4Total;
private static double product1Total, product2Total, product3Total, product4Total, product5Total;
private static double person1Total, person2Total, person3Total, person4Total;
public static void main(String[] args) throws IOException
[code]....
View Replies
View Related
Mar 11, 2014
I am trying to print out a list of usernames for my chat program. It currently works except for the username portion. The Server sends a specific line that tells the client to print it as a username. That line is name//name. For example, if the name of the client was Eric, then the server would send Eric//Eric to the client. The client will then receive the line through a buffered reader. However, my current code keeps getting an error at the i=input.substring... line. Its an IndexOutOfBoundsException: String index out of range: -1.
class ChatThread extends Thread {
ChatServer cServer;
String[] sentence;
int i;
public void run() {
String input = "Eric";
try {
[code]....
View Replies
View Related
Apr 15, 2014
So for fun I've decided to write a program that can keep track of a simple card game my friends and I designed. I've built a class for an array, that stores the basic stats of each card. I need to be able to access this array from another class, that will print the array information to a file, so we can easily keep track of who's cards have leveled up and their stats. The main class will also be using a random number generator to determine how much damage the attacker/defender deals and takes respectively. So far I have the random number generator built as well. I'm just having issues creating a print to file class and what I need to change in my array class to make it accessible by the other class that prints it to a file.
public class StartingStats {
public static void main(String[] args)
{
//Variable to use for how many spots in a stat array there are
final int statArray = 2;
[code]...
what I need to change so that a print to file class can access EACH array in this array folder. Also if any cookie cutter printToFile class I could use/borrow/change that'd be really useful, as I've never done any printing to file before. Also, the levels of each stat and exp will be changing so that's why I need a separate class to print so that I can call it when it's been updated so we always have the most up to date stats saved.
View Replies
View Related
Nov 17, 2014
My array isn't printing. I have tried everything. I have tried putting a nested for loop in different methods but still no luck. I have also tried System.out.print but that prints random characters/numbers.
import java.util.Scanner;
public class Square
{
public static void main(String[] args)
[Code].....
^For some reason the site doesn't show the spaces between the *. It's supposed to be a 5x5 square.
View Replies
View Related
Mar 28, 2014
I have a hashmap of the form HashMap <String, Set<String>>I am trying to create a method with one argument. The argument is a key for the hashmap, if it exists it should print out the key and the associated values. I'm falling over at even getting it to print the key, it keeps printing all the keys from within the hashmap as I don't know how to load the argument into it. I have this so far
Java Code:
public void printValue(String club)
{
boolean result = clubMap.containsKey(club);
if (result)
{
String key = clubMap.keySet(club).toString();
System.out.println(key );
[code]....
View Replies
View Related
Sep 24, 2014
I've been working on the below code for a couple days in my spare time. Just a simple little text-based RPG. I'm brand new at programming and Java, so excuse the bad syntax. There's probably easier ways to go about what I'm trying to accomplish, but all we've learned so far in class is while loops and if statements. Anyways, the problem is that the statement "Really? Ten health points[...]yes or no?" in my code is printing twice.
int health = 0;
boolean enterHealth = false;
String verifyHealth = "";
boolean test6 = false;
boolean test7 = false;
boolean test8 = false;
[code]....
View Replies
View Related
May 3, 2015
Lets say in method 1 I want to print the numbers 1 - 10. Easy stuff. I then print that to the
PrintStream outFile = new PrintStream(new File ("output5_01")); I declared.
Now I'm in another methods, How do I print lets say 11- 20 without erasing the first methods printed results?
so my desired results would be
outFile
-------------------------------
(From 1st method) 1- 10
(From 2nd method) 11-20
As of now the second method always over writes the first. None of my books cover this and its very frustrating when the entire code is done but this won't let me write to the file
View Replies
View Related
Jun 12, 2014
Im trying to make a question game, much like a spin off from Trivial Pursuit. In this code, I call classes to get a random number. This number determines what category the question will be from. Coinciding with this number, the "if" statements go and pull the questions and answers from an alternate class. My problem is that when I try and output what should be the question and the 3 answers, its outputs "null" for each String?
This is my first class, which is just the class for the player.
Java Code: /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
[Code].....
View Replies
View Related
Jan 10, 2015
I finally got this to work but only by printing out the value of each number to debug as such, was there an easier way to do it that I missed?
public class Statistics {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// variables used
double num1;
double num2;
double num3;
double num4;
double num5;
double num6;
Scanner input = new Scanner(System.in);
//receive number and add it separately
[Code] ....
View Replies
View Related
Mar 10, 2014
I want to know how to print this type of pattern when a name is given ???
Example:
name given TEJA
Capture.PNG
View Replies
View Related
Mar 2, 2015
How do you replace negative zero value with a zero value when Printing results:
I want to remove the minus sign in -0.0000 and instead have 0.0000.
I am Printing coordinates and I do not want to have negative zero.
the coordinates are defined as double.
System.out.format(java.util.Locale.US," %.4f %.4f %.4f %.4f%n"xCur ,yCur,xNext,yNext);
0.0000 1.0000 -0.0000 0.0000
View Replies
View Related
Jan 24, 2014
I am writing a Java permutation program, it takes in N and k and gives k amount of combinations based on N length.
That portion works, but the portion I have that does not work is when k is greater then 1, the array is then then printing strings out of index.
The perm algorithm ends on index - 1 for moving characters but then I push all those characters into an Array List, I would think I could print them off however I want from there but that is not the case. I'll include the minimum amount of code possible.
//permString = 1234 or something, it doesn't matter
//Use Case N = 4 k = 3, prints out 123,124,132,134 ect
//Use Case N = 4 k = 2, error index out of range but only on the printing function; the perm function doesn't take k and
//is based on length
//the "" is just an empty string
permNow("", permString);
}
[code].....
View Replies
View Related