Magic Square Won't Pass The Tests
Feb 11, 2015public class MagicSquare {
public boolean isSquare(int[][] arr) {
if(arr.length == arr[0].length)
return true;
else
return false;
[Code] ....
public class MagicSquare {
public boolean isSquare(int[][] arr) {
if(arr.length == arr[0].length)
return true;
else
return false;
[Code] ....
I'm having a small issue with my output on my code. here is what my output is: The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.
Please enter the 4 values for row 0, separated by spaces: 1 2 15 16
Please enter the 4 values for row 1, separated by spaces: 13 14 3 4
Please enter the 4 values for row 2, separated by spaces: 12 7 10 5
Please enter the 4 values for row 3, separated by spaces: 8 11 6 9
Checking square for problems:
DIAG: VALID
ROWS: VALID
COLS: VALID
RANG: VALID
MAGIC: No
MAGIC should be YES. However i keep getting it saying no.This is the correct output..The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.
Please enter the 4 values for row 0, separated by spaces: 1 2 15 16
Please enter the 4 values for row 1, separated by spaces: 13 14 3 4
Please enter the 4 values for row 2, separated by spaces: 12 7 10 5
Please enter the 4 values for row 3, separated by spaces: 8 11 6 9
Checking square for problems:
DIAG: VALID
ROWS: VALID
COLS: VALID
RANG: VALID
MAGIC: YES
Here is my code..
import java.util.Scanner;
public class pdonahue_Magic {
public static void main(String args[]) {
int[][] theSquare = new int[4][4];
Scanner s = new Scanner(System.in);
System.out.println.
("The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.");
System.out.print("Please enter the 4 values for row 0, separated by spaces: ");
theSquare[0][0] = s.nextInt();
theSquare[1][0] = s.nextInt();
theSquare[2][0] = s.nextInt();
theSquare[3][0] = s.nextInt();
[code]....
I am making a program that reads a set of numbers (e.g.: 8 1 6 3 5 7 9 4 2) and prints them into a matrix.For example:
8 1 6 3 5 7 9 4 2, will produce:
8 1 6
3 5 7
9 4 2
When taking in these numbers from the user, I am not allowed to let the user state what size the matrix is (e.g.;3 x 3). Instead the program needs to determine that itself. I have written the code below to count the number of numbers inputted, but now I am stuck as to how to get them into the array. I have what is suppose to be my array written, but it is not function yet.
import java.util.*;
public class MagicSquare {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
[code]....
How to create a MAGIC SQUARE, i just wanted to learn the logic of it .. with 2d array and looping..
View Replies View RelatedI am working on a magic square program. My program compiles. However, when I enter the square dimension it does not select the correct file. The error says "java.io.FileNotFoundException." It looks like it inserts 0 instead of the entered dimension.
import java.util.*;
import java.io.*;
public class Trial2
{
public static int size, row, col;
public static void main(String[]args)throws java.io.IOException
[Code] ....
I'm trying to create a cursor for a game that moves square by square. While it will move to the next square, though, it leaves the image of the previous cursor on the last square it was on.
As a visual explanation, this is what the program looks like on launch:
This is what it's suppose to look like after you press the right arrow key once (made by forcibly changing launch coordinates):
And this is what it actually looks like after you press the right arrow key once:
Here is the code for the program:
package cursortest;
import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import java.awt.event.*;
public class CursorTest extends JPanel implements KeyListener{
[Code] ......
I'm fully aware that I could just use g.clearRect on the area and remove it for sure, but I know for a fact I shouldn't have to as I have another program I made a long time ago that tried to do something similar without needing to resort to that.
How would I compare the square of the square root of any number. The difference in these values is due to the rounding error in Java.
Example: Enter number 2
The answer I should get is: 2.000000000000004
Round off error: -4.440892
First I was using this way how to get connection and all was fine.
con = DriverManager.getConnection("jdbc:derby:memory:datab;create=true");
But now I have to change it to DataSource and how I find out derby had class ClientDataSource for this but for the hell I can't find out how to setup that virtual DB.
ClientDataSource ds = new ClientDataSource();
con = ds.getConnection();
I have two classes Person and Group. Group object is suppose to contain many Persons, How can I write unit tests when some other classes are involved and abstraction is much higher?
View Replies View RelatedI am writing a three part program that essentially works like a magic 8 ball. I have the switch/case part which gives the answers from a random number generator, then I also have a graphics file to create the ball as a visual. Lastly I have the frame to display the ball. The problem I am having is that, I am trying to make the program so that when you type in a question and press return the ball will show the answer and then you can type another question and press return and the ball will show a different answer. To quit you would simply type "quit". Right now however the ball is not showing up in my frame until I type quit, then to ask another question I have to end the run and start over again. Here is my program so far:
Case/Switch:
public class MagicEightBall
{
private int number;
private String answer;
[code]....
Do I want to make something like this:
*****
*.....*
*.....*
*.....*
*****
I am stuck, I have made a code that creates this and don't know how to complete it.
*****
* *
* *
* *
*****
Code:
class Main {
public static void main( String args[] ) {
System.out.print("#Enter number of stars :");
int stars = BIO.getInt();
[Code] ....
I have a program i m not sure how to implement :
(Square numbers) Find the first ten square numbers that are greater than Long.MAX_VALUE . A square number is a number in the form of n 2 . For example, 4, 9, and 16 are square numbers. Find an efficient approach to run your program fast.
I found two ways of solving this but i think both are way inefficient :
-A square number can be divided in lesser square numbers :
what's the square of 36 ? 36 is 2 * 3 * 2 * 3 => 4 * 9 => square is 2 * 3
-second option is to estimate a number and increase it or decrease it based on how close that number * number is to the BigInteger starting number , as as it gets closer the delta gets smaller until it gets to 1
This is a mathematical problem. I got one of the equations I need to compute down to
n^(1/6)
Or the 6th root of n.
Now the Java code I have for this is as follows, since Java has no nth root function I'm raising it to a power of a 1/6.
System.out.println(Math.pow(64.0, 1/6));
Now, the only thing it will print is 1.0
However the 6th root of 64 is 2!!!!
I have to create a square matrix that has a min and max value as well as a size value which is given a integer value in the main method. The matrix has to be filled with random values. Also I have to add that matrix to another one in an addMatrix method and I have to subtract both in a subMatrix method. These are the requirements:
Methods:
Constructor() - receives the row and col size for myMatrix and a max and min values for range of random fill values for the matrix.
RandFill() - fills matrices with random numbers
addMatrix() - receives a matrix object. adds its myMatrix with the received object's myMatrix. The result is placed in this object's myResultMatrix.
subMatrix() - subtracts both matrices
I typed up this code but I'm not sure about some parts of it and I would creating min and max values in the Random method and in the main printing the separate 2 matrices and adding and subtracting them:
import java.util.*;
public class SquareMatrix2 {
public int size;
public int myMatrix [][];
public int myResultMatrix;
public int [][] ResultStatus;
[Code] ....
Why does my program write that i have problem in Timer?
public class Ball extends JPanel implements ActionListener {
Timer timer = new Timer (3, this);
int x = 0, y = 0, aX = 2, aY = 2;
public void PaintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
[Code] ....
I aim to create a solid square using this:
public class asteriskSquare {
public static void main(String[] args) {
for (int x = 1; x <= 4; x++){
for (int y = 1; y <= 4; y++){
System.out.println("+");
}
System.out.println("+");
}
}
}
All I'm getting is a single column of "+".
I come to the point: I just started to learn java through various manuals and in one of them I came across a declaration of an array that I do not understand:
int[][] multiArr = new int[2][];
the manual says that you can allocate the multidimensional array multiArr by defining size in only the first square bracket but I can't undestand how you can use this array. Seems to be no way to store data with it!
I am new to java and i am trying to make a Java application which prints a diamond in a square grid of dots whose side length is input to the application.When you run the code is should be like this:
..*..
.*.*.
*...*
.*.*.
..*..
My java code print this:
..*..
.***.
*****
.***.
..*..
Here is my code:
class Main {
public static void main(String args[]) {
System.out.println("#Enter size of Diamond :");
int longestRow = BIO.getInt();
for(int row=1 ; row<=longestRow ; ++row)
[Code] .....
So currently I'm trying to learn how to draw a square with asterisks based on the input I give for it's sideLength.
So if I have a Square(5) it should draw a 5x5 looking like this:
*****
*****
*****
*****
*****
but then in the main method, it should draw multiple squares of given sizes, so say 5, 6, and 7. Making a square for each of the given numbers. This is what I currently have so far ....
class Square {
int sideLength; Square( int size ) {
sideLength= size;
} int getArea() {
return sideLength * sideLength;
[Code] ....
import java.util.Scanner;
public class justin10a
{
public static void main(String [] args)
{
int n;
n = getSize();
[Code] .....
I am working on a number of utility functions for square matrices and arrays, and I keep having trouble with segmentation faults.
arrayUtils~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class ArrayUtils {
//This function takes an array a, and returns the sum between indices
//i and j, where i is the lower index and j is the upper index. int size makes
//sure that the function doesn't go out of bounds.
public static int subSum(int[] a,int i, int j) {
int sum=0;
[Code] .....
My code compiles and runs just fine, i'd just like to get creating a small square box that shows number of words used next to the "word count = ". i'd wanto to press the count words button to count and show the number in the square box. here is my code.
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class wordCount extends JFrame implements ActionListener
[Code] .....
I have to create 3 JLabel for Option, Square and Direction. Well, I did but its not in the right place. It should be in the right Panel but i did change the code so its in panelRight1 but its still not changing? Its somehow not working and no matter how i try to change the position of it, it doesnt change at all.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CBallMaze extends JFrame implements ActionListener
{
/**
*
*/
private JMenu[] menu = {
[Code] .....
Some algorithm in computing the square root of a number without using any loops?
View Replies View RelatedI am new to java and during my classes we built a basic calculator. I want to extend my calculator and added function to square numbers.Here is my program:
Java Code:
package calculator02;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
[code]...
So I need to create a square and make it rotate when the letter R is pressed and then make it stop when the letter R is pressed again. I need make it rotate using a maths equation that I can't figure it out myself right this moment.
Code for square:
package lab2;
public class Square {
public double [][] vertices = new double[4][2]; //not good to make this public
public Square(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
vertices[0][0]=x1;
vertices[0][1]=y1;
vertices[1][0]=x2;
[Code] ....
In the Square code there is a method to write an equation to rotate it but I can't figure that part out ....