xx
Content/activities/activity5/a5_v4.html
COMP 1020
Version 4: Reading from a file
Instead of using hard-coded arrays, it would be less awkward to read the potential magic squares from a text file. This text file could consist of a sequence of squares, where each square is defined by first its dimension (n) alone on a line, followed by one line for each row, with the values in the row separated by spaces. For example:
4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 3 1 1 1 5 5 5 9 9 9 3 8 1 6 3 5 7 4 9 2 5 11 24 7 20 3 4 12 25 8 16 17 5 13 21 9 10 18 1 14 22 23 6 19 2 15 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 5 5 5 5 5 5 5 5 5 1 1
The last square in this file is a 1x1 square containing the single value 1. To make the method that reads the input less complex, it will be divided into two methods. The first method will go through the entire file, reading the dimension, and calling a (as-yet-unwritten) method readSquare to read the lines from that square. It then calls print and check, as before.
public static void processSquaresFromFile(String filename) { BufferedReader inputFile; String firstLine; int[][] square; try { inputFile = new BufferedReader(new FileReader(filename)); firstLine = inputFile.readLine(); while (firstLine != null) { square = readSquare(inputFile, firstLine); printSquare(square); check(square); firstLine = inputFile.readLine(); } inputFile.close(); } catch (IOException ioe) { System.out.println(ioe.getMessage()); ioe.printStackTrace(); } }The readSquare method is passed the BufferedReader to read the contents of the square from the current position in the file. But there is a problem: reading from a BufferedReader can throw a checked exception, and Java is not smart enough to notice that readSquare is only called inside an existing try block. We can use a throws clause on the readSquare method, which is a declaration to Java that the method may throw an unchecked exception, and we are relying on the calling method to handle it. The throws clause consists of the keyword throws followed by the name of the unhandled exception at the end of the method header.
public static int[][] readSquare(BufferedReader inputFile, String firstLine) throws IOException { String[] split; String inputLine; int[][] square; int squareSize; squareSize = Integer.parseInt(firstLine); System.out.println("\nSize: " + squareSize); square = new int[squareSize][squareSize]; for (int row = 0; row < squareSize; row++) { inputLine = inputFile.readLine(); System.out.println(inputLine); split = inputLine.split("\\s+"); for (int col = 0; col < squareSize; col++) { square[row][col] = Integer.parseInt(split[col]); } } return square; } We could have done it without the throws clause, but we would have needed another try-catch block in readSquare and a return value (like null) to indicate there was an error, and a test of that return value when we call it in processSquaresFromFile. This is much easier.To test this new method, simply put the data in a text file (such as magic_squares.txt) and call processSquaresFromFile("magic_squares.txt"); in the main program.
Java file Version 4 link. Click on the link and save the Zip file to your computer.
Content/css/comp_1020_new.css
@charset "utf-8"; /* CSS Document */ body {height: auto;margin-top: 10px; margin-left: 25px; margin-bottom: 10px;} #container { width: 780px; height:468px; padding-left: 47px; padding-top: 50px; padding-right: 35px; margin: auto; background-image: url(unit1.jpg); left: 38px; } #containersmall { width: 780px; height:468px; padding-left: 47px; padding-top: 0px; padding-right: 35px; margin: auto; background-image: url(unit1.jpg); left: 38px; } #banner { width:800px; height:100px; } .h1 { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; color:#7eae37; margin-bottom:-10pt; } .h1b { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#000000; } .h1w { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#006699; } .h2 { font-family:Arial, Helvetica, sans-serif; font-size:13pt; font-weight:bold; color:#244062; font-style:normal; position: relative; background-color: #FFFFFF; } .h2b { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-weight:bold; font-variant:small-caps; color:#999966; font-style:normal; } .h3 { font-family:Arial, Helvetica, sans-serif; font-size:11.5pt; font-style:normal; font-weight:bold; color:#244062; margin-bottom:-7pt; } .h3b { font-family:Arial, Helvetica, sans-serif; font-size:16pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#336699 } #page { width:780px; height:994px; left: 7px; top: 166px; padding-left: 10px; padding-right: 10px; } .body { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; font-style:normal; font-weight:normal; color:#000000; line-height:14pt; } .body_box { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; font-style:normal; font-weight:normal; color:#000000; line-height:14pt; } .body_bold { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; font-style:normal; font-weight:bold; color:#006699 } .body_bold_box { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; font-style:normal; font-weight:bold; color:##006699 } .body_dr_green { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#336600 } .definition { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:italic; font-weight:normal; border-width:.25pt; border-style:solid; border-color: #CCCCCC; border-collapse:separate; empty-cells:hide; } .toc { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#669933 } a:link {color:#0000FF; text-decoration:underline; font-family:Arial, Helvetica, sans-serif;} a:visited {color:#0000FF; text-decoration:underline;font-family:Arial, Helvetica, sans-serif;} .top { font-family:Arial, Helvetica, sans-serif; font-size:8pt; font-style:normal; font-weight:normal; } p.ind { text-indent:-20px; margin-left:20px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000 } .quote { margin-left:40px; margin-right:40px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000 } .blackBox { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; background-color:#FFFFFF; border: 1.5px solid #000; padding-top: 10px; padding-left: 15px; padding-right: 5px; padding-bottom: 10px; margin-right: 200px; margin-left: 3px; } .blueBox { background-color: #daeef4; border: 1.5px solid #FFF; padding-top: 10px; padding-left: 15px; padding-right: 5px; padding-bottom: 10px; margin-left: -10px; margin-right: -15px; } .yellowBox { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; background-color: #FFFF99; border: 1.5px solid #FFF; padding-top: 10px; padding-left: 15px; padding-right: 5px; padding-bottom: 10px; margin-left: -10px; margin-right: -15px; } .blackBorder { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; background-color: #FFFFFF; border: 1.5px solid #00000; padding-top: 10px; padding-right: -17px; padding-bottom: 14px; margin-right: 200px; margin-left: 3px; } .blackBorderwy { font-family:Arial, Helvetica, sans-serif; font-size:10.5pt; background-color: #FFFFCC; border: 1.5px solid #00000; padding-top: 10px; padding-right: -17px; padding-bottom: 14px; margin-right: 200px; margin-left: 3px; } .blueBorder { background-color: #FFFFFF; border: 1.5px solid #669966; padding-top: 10px; padding-right: -17px; padding-bottom: 14px; margin-right: 200px; margin-left: 3px; } .biol_head { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-style:normal; font-weight:bold; color:#FFFFFF; } .body_white { font-family:Arial, Helvetica, sans-serif; font-size:9pt; font-style:normal; font-weight:normal; color:#FFFFFF } .body_white_bold { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:bold; color:#FFFFFF }
Content/css/COMP1020.css
@charset "utf-8"; /* CSS Document */ body {margin-top: 10px; margin-left: 25px; margin-bottom: 10px;} #container { width: 780px; height:591px; padding-left: 47px; padding-top: 50px; padding-right: 35px; margin: auto; } #banner { left:25px; top: 10px; width:800px; height:100px; padding-bottom: 30px; } .heading_banner { margin: 0px 0px 0px 0px; padding: 20px 0px 0px 30px; font-size: 34px; font-weight: 400; font-family: Arial, Helvetica, sans-serif; color: #FFF; height: 100px; width: 90%; line-height: 1.1em; opacity: 0.75; letter-spacing: 0.05em; position:absolute; } .h1 { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#336600; margin-bottom:-9pt; } .h1b { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#000000; } .h1w { font-family:Arial, Helvetica, sans-serif; font-size:18pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#FFFFFF; } .h2 { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-weight:bold; font-variant:small-caps; color:#336600; font-style:normal; position: relative; margin-bottom:3pt; } .h2b { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-weight:bold; font-variant:small-caps; color:#336699; font-style:normal; } .h3 { font-family:Arial, Helvetica, sans-serif; font-size:16pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#336600; margin-bottom:-7pt; } .h3b { font-family:Arial, Helvetica, sans-serif; font-size:16pt; font-style:normal; font-weight:bold; font-variant:small-caps; color:#336699 } .body { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000; } .body_bold { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:bold; color:#000000 } .definition { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:italic; font-weight:normal; border-width:.25pt; border-style:solid; border-color: #CCCCCC; border-collapse:separate; empty-cells:hide; } a:link {color:#336699; text-decoration:underline; font-family:Arial, Helvetica, sans-serif;} a:visited {color:#669933; text-decoration:underline;font-family:Arial, Helvetica, sans-serif;} .top { font-family:Arial, Helvetica, sans-serif; font-size:8pt; font-style:normal; font-weight:normal; } p.ind { text-indent:-20px; margin-left:20px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000 } .quote { margin-left:40px; margin-right:40px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:normal; color:#000000 } .blueBox { background-color: #336699; border: 1px solid #336699; padding-top: 10px; padding-right: 5px; padding-bottom: 10px; padding-left: 10px; } .blueBorder { background-color: #FFFFFF; border: 1px solid #336699; padding-top: 10px; padding-right: 5px; padding-bottom: 10px; padding-left: 10px; } .biol_head { font-family:Arial, Helvetica, sans-serif; font-size:12pt; font-style:normal; font-weight:bold; color:#FFFFFF; } .body_white { font-family:Arial, Helvetica, sans-serif; font-size:9pt; font-style:normal; font-weight:normal; color:#FFFFFF } .body_white_bold { font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-style:normal; font-weight:bold; color:#FFFFFF } .Highlight { background-color: #FFFF00; border: solid thin black; margin-top: .5em; margin-bottom: .5em; margin-right: 20px; margin-left: 20px; padding-top: .5em; padding-bottom: .5em; padding-left: 1em; padding-right: 1em; } .Aside { background-color: lightGrey; border: solid thin black; margin-top: .5em; margin-bottom: .5em; margin-right: 20px; margin-left: 20px; padding-top: .5em; padding-bottom: .5em; padding-left: 1em; padding-right: 1em; } .Action { background-color: #FFFFC0; border: solid thin black; margin-top: .5em; margin-bottom: .5em; margin-right: 20px; margin-left: 20px; padding-top: .5em; padding-bottom: .5em; padding-left: 1em; padding-right: 1em; } .key { background-color: #a9a9a9; color: white; border: solid thin black; font-style: italic; } .menu { background-color: #a9a9a9; color: white; border: solid thin black; } .version { color: olive; font-style: italic; } code { font-family: "Lucida Sans Typewriter", Courier, monospace; color: #222200; background-color: #FFFFE0; } .code { font-family: "Lucida Sans Typewriter", Courier, monospace; background-color: #FFFFB0; border-top: dotted thin black; border-bottom: dotted thin black; padding-top: 1em; padding-bottom: 1em; padding-left: .5em; padding-right: .5em; margin-right: 20px; margin-left: 20px; } .Aside .code { margin-right: 10px; margin-left: 10px; } pre.code { white-space: -moz-pre-wrap; /* Mozilla, supported since 1999 */ white-space: -pre-wrap; /* Opera 4 - 6 */ white-space: -o-pre-wrap; /* Opera 7 */ white-space: pre-wrap; /* CSS3 - Text module (Candidate Recommendation) http://www.w3.org/TR/css3-text/#white-space */ word-wrap: break-word; /* IE 5.5+ */ } dfn { font-weight: bold; color: brown; } .Example { background-color: aqua; border: solid thin black; margin-top: .5em; margin-bottom: .5em; margin-right: 20px; margin-left: 20px; padding-left: .5em; padding-right: .5em; } .ref { color: red; font-style: italic; } .filename { font-family: Arial, Helvetica, sans-serif; color: green; text-decoration: underline; } .output { font-family: "Lucida Sans Typewriter", Courier, monospace; color: purple; font-style: italic; } table.bordered { border-width: thin; border-style: solid; border-spacing: 0; } table.bordered td { border-width: thin; border-style: solid; border-spacing: 0; padding-left: 4px; padding-right: 4px; } table.bordered th { border-width: thin; border-style: solid; border-spacing: 0; padding-left: 8px; padding-right: 8px; } th.side { font-weight: bold; color: black; background-color: #ffd0d0; } th.right { font-weight: bold; color: black; background-color: #ffffd0; } th.top { font-weight: bold; color: black; background-color: #ffd0d0; text-alignment: center; } th.side code { background-color: inherit; } th.top code { background-color: inherit; } .InlineTitle { font-weight: bold; } .view { background-color: black; color: white; border: solid thin black; padding: 0.25em 1em; text-decoration: none; } a.view { background-color: black; color: white; border: solid thin black; } a.view:hover { background-color: blue; color: yellow; text-decoration: none; } /*added for IE as above (hover) does not work in IE*/ a.viewOn { background-color: blue; color: yellow; border: solid thin black; padding: 0.25em 1em; text-decoration: none; } ol li { margin-top: 0.3em; margin-bottom: 0.3em; } #footer {margin: 0px 0px 0px 0px; border: thin solid red;}
Content/images/comp1020_banner.jpg
Content/activities/java/Activity5D.zip
Activity5D.java
Activity5D.java
import
java
.
io
.
*
;
public
class
Activity5D
{
public
static
void
main
(
String
[]
args
)
{
processSquaresFromFile
(
"squares.txt"
);
System
.
out
.
println
(
"\nEnd of processing."
);
}
public
static
void
processSquaresFromFile
(
String
filename
)
{
BufferedReader
inputFile
;
String
firstLine
;
int
[][]
square
;
try
{
inputFile
=
new
BufferedReader
(
new
FileReader
(
filename
));
firstLine
=
inputFile
.
readLine
();
while
(
firstLine
!=
null
)
{
square
=
readSquare
(
inputFile
,
firstLine
);
printSquare
(
square
);
check
(
square
);
firstLine
=
inputFile
.
readLine
();
}
inputFile
.
close
();
}
catch
(
IOException
ioe
)
{
System
.
out
.
println
(
ioe
.
getMessage
());
ioe
.
printStackTrace
();
}
}
public
static
int
[][]
readSquare
(
BufferedReader
inputFile
,
String
firstLine
)
throws
IOException
{
String
[]
split
;
String
inputLine
;
int
[][]
square
;
int
squareSize
;
squareSize
=
Integer
.
parseInt
(
firstLine
);
System
.
out
.
println
(
"\nSize: "
+
squareSize
);
square
=
new
int
[
squareSize
][
squareSize
];
for
(
int
row
=
0
;
row
<
squareSize
;
row
++
)
{
inputLine
=
inputFile
.
readLine
();
System
.
out
.
println
(
inputLine
);
split
=
inputLine
.
split
(
"\\s+"
);
for
(
int
col
=
0
;
col
<
squareSize
;
col
++
)
{
square
[
row
][
col
]
=
Integer
.
parseInt
(
split
[
col
]);
}
}
return
square
;
}
public
static
void
printSquare
(
int
[][]
magicSquare
)
{
for
(
int
row
=
0
;
row
<
magicSquare
.
length
;
row
++
)
{
for
(
int
col
=
0
;
col
<
magicSquare
[
row
].
length
;
col
++
)
{
System
.
out
.
print
(
magicSquare
[
row
][
col
]
+
"\t"
);
}
System
.
out
.
println
();
}
}
public
static
void
check
(
int
[][]
array
)
{
boolean
overall
;
boolean
rows
,
columns
;
boolean
values
;
int
magicConstant
;
if
(
!
isSquare
(
array
))
{
System
.
out
.
println
(
"The array is not square."
);
overall
=
false
;
}
else
{
magicConstant
=
magicConstant
(
array
);
System
.
out
.
println
(
"The magic constant is "
+
magicConstant
);
rows
=
checkRows
(
array
,
magicConstant
);
columns
=
checkColumns
(
array
,
magicConstant
);
values
=
checkValues
(
array
,
magicConstant
);
overall
=
rows
&&
columns
&&
values
;
}
if
(
overall
)
{
System
.
out
.
println
(
"This is a magic square."
);
}
else
{
System
.
out
.
println
(
"This is NOT a magic square."
);
}
}
public
static
boolean
isSquare
(
int
[][]
array
)
{
boolean
result
;
if
(
array
.
length
==
0
)
{
result
=
true
;
}
else
{
result
=
array
.
length
==
array
[
0
].
length
;
}
return
result
;
}
public
static
int
magicConstant
(
int
[][]
square
)
{
return
(
square
.
length
*
(
square
.
length
*
square
.
length
+
1
))
/
2
;
}
public
static
boolean
checkRows
(
int
[][]
square
,
int
magicConstant
)
{
int
length
;
int
wrong
;
int
sum
;
length
=
square
.
length
;
wrong
=
0
;
for
(
int
row
=
0
;
row
<
length
;
row
++
)
{
sum
=
0
;
for
(
int
col
=
0
;
col
<
length
;
col
++
)
{
sum
+=
square
[
row
][
col
];
}
if
(
sum
!=
magicConstant
)
{
wrong
++
;
}
}
System
.
out
.
println
(
"There were "
+
wrong
+
" invalid rows."
);
return
wrong
==
0
;
}
public
static
boolean
checkColumns
(
int
[][]
square
,
int
magicConstant
)
{
int
length
;
int
wrong
;
int
sum
;
length
=
square
.
length
;
wrong
=
0
;
for
(
int
col
=
0
;
col
<
length
;
col
++
)
{
sum
=
0
;
for
(
int
row
=
0
;
row
<
length
;
row
++
)
{
sum
+=
square
[
row
][
col
];
}
if
(
sum
!=
magicConstant
)
{
wrong
++
;
}
}
System
.
out
.
println
(
"There were "
+
wrong
+
" invalid columns."
);
return
wrong
==
0
;
}
public
static
boolean
checkValues
(
int
[][]
square
,
int
magicConstant
)
{
int
[]
counts
;
int
length
;
int
wrong
;
length
=
square
.
length
;
counts
=
new
int
[
length
*
length
+
1
];
wrong
=
0
;
for
(
int
row
=
0
;
row
<
length
;
row
++
)
{
for
(
int
col
=
0
;
col
<
length
;
col
++
)
{
if
(
square
[
row
][
col
]
<
1
||
square
[
row
][
col
]
>
length
*
length
)
{
wrong
++
;
}
else
{
counts
[
square
[
row
][
col
]]
++
;
}
}
}
for
(
int
i
=
1
;
i
<=
length
*
length
;
i
++
)
{
if
(
counts
[
i
]
>
1
)
{
wrong
++
;
}
}
System
.
out
.
println
(
"There were "
+
wrong
+
" repeated or invalid values."
);
return
wrong
==
0
;
}
}