John Sing

Ranch Hand
+ Follow
since Dec 03, 2015
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Sing

Junilu Lacar wrote:Then let me be even more blunt with you. Not formatting your code, knowing that it's terrible, and not doing anything about it is very INCONSIDERATE of other people who would like to help you. That's like going into someone's house with dirty shoes, tracking mud all over the living room, and saying "I know my shoes are dirty but I have no time to think about you, just help me get this mud off." If I were the house owner, I would kick you out



Let me rephrase that. I'm going to change the way my code looks but I just need to fix this one small section before I do.
7 years ago
So I am trying a new approach. I know my code looks terrible but I need help on just one section. That section is



I have to try to get the entire magic square into one JOptionPane box but this makes it so each number has it's own box. How can I get them all into one box?
7 years ago
Hi I have to create a code that creates a magic square based on a number the user inputs. A magic square is a square where the numbers in the vertical, horizontal, and diagonal columns add up to the same number. I was able to do this in one file but now I have to make it work with a driver class. The thing is when I try to print the result it comes up blank. I know this is because the part where I assign the value "Print" is inside the main loop. Is there any way I can get the assigned value of Print out of the main loop and into the driver? Here is my Driver Class



and here is the code connected to the Driver

7 years ago
Hi I had to make a program where a user inputs values and I have to test if they are a magic square. I was able to do that with the following code



but I have to make it so the user inputs the value through JOptionPane and so that the program uses a driver class. So here is my driver class



and here is my new main code



So how can I convert my code to use the values entered from the JOptionPane? Would I have to redo the entire code?
7 years ago
Hi I have to create a program that checks if a user inputted values are what is known as a "magic box." The values are a magic box if the sum of the elements in each row, column, and the two diagonals are the same value and every element is unique. Here is the exact project requirements

"Write a class SquareMatrix that represents a matrix, and contains the following methods: - Relevant accessor/mutator methods - void add(int i, int row, int column): o Adds an integer to the matrix at the specified location. - boolean isMagic() o Determines whether the matrix illustrates a magic square. This means:  The user entered n 2 numbers for some number n  Each of the numbers occurs exactly once in the matrix  The sums of the elements in each row, column, and the two diagonals are equal Your class should handle user input through a full GUI, or pop -up windows. The user should specify: - The size of the matrix (the value of n) - The numbers to be added to the matrix"

I tried to start writing a basic code to do this (I'm just trying to make it work as of now, I will worry about the driver class later) and I already ran into a few problem with my code. Here is my code



The code compiles but when I try to run it Java gives me the following message

"java.lang.ArrayIndexOutOfBoundsException: 3
at SquareMatrix.main(SquareMatrix.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)"

The number I input for that particular example was 4 but that happens for every number I try inputting. So what can I do to fix this? I know my code is nowhere near finished but if I can't figure this step out I don't think I will be able to continue. Thank you in advanced.
7 years ago
Hi I have to create a program that plays an elimination game. The user has to input the number of players and the number of "cycles" or how much the program counts by. So for example if I input 8 players and 4 cycles then the first player out will be player 4 and then the next one would be player 8, and then it goes on and on until there is one winner. As of now my current plan to change any variables that the cycle lands on to 999 and keep going until there is one winner. I know I posted this a few times before but I made some major changes to my code (almost everything) and I think it should be working now. It gives me an output but it is not correct every time. How can I fix this?

7 years ago

Paul Clapham wrote:

John Sing wrote:and here is the error I am getting

"java.lang.ArrayIndexOutOfBoundsException: 998
at numberPicker.main(numberPicker.java:42)



That tells you several things:

1. You tried to access an array outside of its actual bounds.

2. You tried to access element 998.

3. You did that at line 42, which is in a method named "main".

So you should look at that line and find out why you are trying to access element 998 of an array which isn't that big.



Thank you for being clear and to the point. I was able to fix the error quite easily after you cleared it up for me.
7 years ago
Okay so I was able to figure out why my code only changes one number. It's because I told it to change the cycle number and not the number it lands on. I tried adding something to my code to do this. The code compiles but when I try to run it I get an error. Here is my new code



and here is the error I am getting

"java.lang.ArrayIndexOutOfBoundsException: 998
at numberPicker.main(numberPicker.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)"

any ideas on how to fix this?
7 years ago
Hi I have to create a program that plays an elimination game. The user has to input the number of players and the number of "cycles" or how much the program counts by. So for example if I input 8 players and 4 cycles then the first player out will be player 4 and then the next one would be player 8, and then it goes on and on until there is one winner. As of now my current plan to change any variables that the cycle lands on to 999 and keep going until there is one winner. But right now my code only changes one value to 999 and then stops. How can I get my code to change any value it lands on to 999 and keep going until a winner has been picked? Here is my code

7 years ago
Hi I have to create a program that plays an elimination game. The user has to input the number of players and the number of "cycles" or how much the program counts by. So for example if I input 8 players and 4 cycles then the first player out will be player 4 and then the next one would be player 8, and then it goes on and on until there is one winner. I thought I finished my program but for some reason it gets the wrong answer half the time. Then to make matters worse my teacher decided to tell us that we can't use array lists and we have to use arrays instead today... So I really need some help changing my code to have an array in it and making it work. Here is my code



7 years ago
Okay so after a little experimenting I was able to add the numbers of the players positions and I now know what is wrong with my code. My code picks player number 1 every time because of the remove from the array list part of the code I have on line 34. I have to so it removes the second character every time so no matter what it's going to land on the first player. How can I change my code so that it removes the player that the code lands on? Here is my new code.




7 years ago
I am sorry for not being clear but the program I am trying to create is one that will play an "elimination" game. The user inputs the number of players and the number of cycles or the amount it goes around in. Then whoever the program lands on is eliminated. So if I pick eight players and I have four cycles that means the program counts to four then the fourth person is eliminated, then it counts by four again and the eighth person is eliminated. Then it keeps going until there is one winner. I used "duck duck goose" because I could not figure out how to input numbers into the array list based off user input. The program I am trying to create is not like actual game of duck duck goose and I am just using those variables as an example. It would be the same thing if I use eney, meeney, miny, moe or anything else really. I'm sorry I did not specify this before.
7 years ago
Hi I have to create a code to play a game of Duck Duck Goose. Here is the exact project

"You are writing a program that simulates a game (such as Duck Duck Goose). The following are the requirements: 1. You must ask the user for the number of participants 2. You must ask the user for the length of the cycle (for example, Duck Duck Goose has a cycle of 3, whereas Eenie Meenie Miney Mo has a cycle of 4) 3. Your project must ONLY display the winner of the game (counting from 0). So, for example, if the winner is the 4 th participant, your program should display 3"

I have a code that I think might do this that technically works.



The code compiles and works but I don't think it is doing what I want it to do. When I input 4 for the number of cycles and 8 for the number of players I get duck as the output, but when I do it on paper I get Goose. It's very possible that I am doing it wrong so I just want to make sure my code is correct and if not what should I do to fix it?
7 years ago

Junilu Lacar wrote:Since you're dealing with integer values, you can just use % and / (integer division) to reverse the number. If the reverse of a number and the original number are the same then it's a palindromic number.

For example, the reverse of 89:

89 % 10 = 9
89 / 10 = 8
9 * 10 = 90
90 + 8 = 98

98 != 89 therefore 89 is not a palindrome

77 % 10 = 7
77 / 10 = 7
7 * 10 = 70

70 + 7 = 77

77 == 77 therefore 77 is a palindrome

---
Exercise for you is to generalize this and put it into a loop.

I would break the problem down into two parts:
1 - find the reverse of a number
2 - find the largest palindromic number that is the product of X-digit numbers



I looked up how to reverse a number online and I implemented it into my code. The thing the new number I am getting is not even a palindrome. Here is my new code



This code prints out the number "99700" so I think I am doing this wrong. How can I fix this?

EDIT- I just realized I am getting my error because I reversed the number but I never compared it to the original. How would I go about doing this?

ANOTHER EDIT- Okay I tried comparing them and my code compliles but then it does not print anything. Here is my new code

8 years ago
Hi I am trying to create a program to solve Project Euler Problem 4. The problem is "A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers."

Here is my code



The number I am getting from this code is "580085" which does fulfill the requirements but it's the wrong answer. How can I fix my code to get the right answer?
8 years ago