Java Help Please. I am doing this assignment and in my program I get errors and
ID: 3673519 • Letter: J
Question
Java
Help Please.
I am doing this assignment and in my program I get errors and I cannot resolve them. This is the original assignment: https://www.chegg.com/homework-help/questions-and-answers/java-assignment-gives-opportunity-use-arrays-solve-problem-experiment-reading-writing-file-q10826055.
Here is my code so far:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication13;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.IOException;
import java.util.Scanner;
/**
*
*/
public class JavaApplication13 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
int input=0; // user selection
Scanner sc = new Scanner(System.in); // prepare for reading from keyboard
// display menu selection
System.out.println("Enter the type of input for the game: " +
"(1) Input magic values from file " +
"(2) Construct magic squares from input size and write to file "+
"(3) Exit the program");
// get user selection from keyboard
input = sc.nextInt();
// continue processing until user enters a 3
while (input !=3)
{
switch (input)
{
case 1:
inputMagicValue();
break;
case 2:
constructMagicSquare();
break;
case 3:
break;
default:
System.out.println("ERROR: Please input a valid number 1, 2 or 3.");
} // end switch
}
}
public static void inputMagicValue() throws FileNotFoundException, IOException {
Scanner user = new Scanner(System.in);
System.out.println("Enter input file name ");
String inputfile = user.nextLine().trim();
File file = new File(inputfile);
byte[] bytes;
//as the size of array is not known
try (FileInputStream fis = new FileInputStream(file)) {
//as the size of array is not known
bytes = new byte[(int)file.length()];
fis.read(bytes);
}
String[] valueStr = new String(bytes).trim().split("\s+");
int n = valueStr.length;
int[] arr = new int[n];
for(int i=0; i<n; i++)
arr[i] = Integer.parseInt(valueStr[i]);
final int nsquare = n*n;
final int M = (n*n*(n*n+1)/2)/n;
int sumRow=0, sumCol=0, sumPrimDiag=0, sumSecDiag=0;
for(int row=0; row<n; row++) {
sumRow = 0;
sumCol = 0;
for( int col=0; col<n; col++) {
if(arr[row][col]<1 || arr[row][col]>nsquare)
System.out.println("invalid array input");
else {
sumRow += arr[row][col];
sumCol += arr[col][row];
}
}
sumPrimDiag += arr[row][col];
sumSecDiag += arr[row][n-row-1];
if(sumRow != M || sumCol != M)
System.out.println("sum of row or column did not match, not magic square ");
}
if(sumPrimDiag != M || sumSecDiag != M)
System.out.println(" sum of diagonal did not match, not magic square ");
else {
System.out.println("it is magic square");
for(int row=0; row<n; row++) {
for(int col=0; col<n; col++) {
System.out.print(arr[row][col]);
}
System.out.println();
}
}
}
public static void constructMagicSquare() throws FileNotFoundException {
Scanner user = new Scanner(System.in);
System.out.println("Enter magicSquare size");
int n = user.nextInt();
System.out.println("Enter output file name");
String outputfile = user.nextLine().trim();
File output = new File(outputfile);
if(n%2 == 0)
System.out.println("size should be odd");
else {
int[][] magic = new int[n][n];
int row = n-1;
int col = n/2;
magic[row][col]=1;
for(int i=2; i<=n*n; i++){
if(magic[(row+1)%n][(col+1)%n] == 0) {
row = (row+1)%n;
col = (col+1)%n;
}
else
row = (row-1+n)%n;
magic[row][col] = i;
}
PrintStream out = new PrintStream(new FileOutputStream(outputfile));
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
System.out.print(magic[i][j]);
}
System.out.println();
}
out.close();
}
}
}
Explanation / Answer
import java.util.Scanner;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Magic_Squares
{
private static Scanner input = new Scanner(System.in);
private static int num=2;
public static void main(String[] args)
{
menu();
}
public static void menu()
{
int choice=0;
while(choice <=0 || choice > 3)//validates user choice
{
System.out.println("Enter the type of algorithm to execute: "
+" (1) Input magic values from file"
+" (2) Construct magic squares from input size and write to file"
+" (3) Exit");
choice= input.nextInt();
}
// if(choice == 1)
// inputMagicValues();
if(choice == 2)
constructMagicSquares();
else
{
System.out.println("Thank You!");
System.exit(0);
}
}
public static void constructMagicSquares()
{
try {
PrintWriter outStream = new PrintWriter("5x5.txt");
while(num %2==0)
{
System.out.println("Please enter an odd number");
num=input.nextInt();
}//end while
int[][] magic = new int[num][num];
int row = num-1;
int col = num/2;
magic[row][col] = 1;
for (int i = 2; i <= num*num; i++)
{
if (magic[(row + 1) % num][(col + 1) % num] == 0)
{
row = (row + 1) % num;
col = (col + 1) % num;
}//end if
else
{
row = (row - 1 + num) % num;
}//end else
magic[row][col] = i;
}//end for
// print results
for (int i = 0; i < num; i++)
{
for (int j = 0; j < num; j++)
{
if (magic[i][j] < 10) {
System.out.print(" "); // for alignment
outStream.print(" "); // for alignment
}
if (magic[i][j] < 100) {
System.out.print(" "); // for alignment
outStream.print(" "); // for alignment
}
System.out.print(magic[i][j] + " ");
outStream.print(magic[i][j] + " ");
}//end for
System.out.println();
outStream.println();
}//end for
//close the file stream
outStream.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Magic_Squares.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.