One interesting application of two dimensional arrays is magic squares. A magic square is a square matrix of numbers in which the sum of every row, every column, and both diagonals is the same. Magic squares have been studied for many years, and there are some particularly famous magic squares. In this exercise you will write code to determine whether a square is magic.
The file SquareTest.java contains a program that reads input for squares from text files and tells whether each is a magic square. The program uses the class in the file Square.java which represents a square matrix. It contains a constructor method that creates a new square from a text file. It also contains headers for methods to print the square, find the sum of a given row, find the sum of a given column, find the sum of the main (or other) diagonal, and determine whether the square is magic.
Download the two files and make sure you understand the code that is in them. Fill in the code for the unfinished methods in the Square class as described in the method headers. You can test your program using the test data in the tar file SquareData.tgz. The first three squares are magic, the others are not.
The file Fahrenheit.java is similar to the example program in Listings 6.5 & 6.6 of the text (pages 249 - 251). The program converts temperatures in Fahrenheit to the Celsius equivalent. The user enters a temperature in the text field and when the enter key is pressed the Celsius equivalent is computed and displayed. Pressing the enter key on a text field generates an action event so the program must implement the ActionListener interface. Save the program file to your lab 2 project directory and run it to see how it works. Study the code noting the following:
actionPerformed
method uses the getText
method to get the information out of the text field. That information is returned as a String.In this exercise you will write a similar program to compute a person's Body Mass Index. Body Mass Index (BMI) is a measure of weight that takes height into account. BMI is a simple way to determine if a person is over-weight or under-weight. BMI is calculated as follows for both men and women:
(703 * weight in pounds) / (height in inches)2
The file BMICalculator.java contains a skeleton for a GUI program to calculate BMI. Since there are two input values needed (the height and weight) this program will not respond to the user pressing the enter key on a text field as the Fahrenheit program does. Instead, it will have a button for the user to press to trigger the calculation. So, the user enters his or her height and weight and presses a "Calculate BMI" button and the program then displays the user's BMI. Much of the framework has been done for you, but you will need to fill in code. Follow the instructions in the comments of the program.
Run the BMI program to test it. The layout of the program looks a little strange with the calculate button on the same row as the output button. In order to put these onto separate rows, you can add the button to a panel that is too wide to fit anything else on the same row. In the panel constructor, create a new JPanel for the button. Set the button panel's preferred size to the width of the window panel and a height that will fit the button. Then add the button to the button panel, and the button panel to the window panel. Run your program to make sure that it looks good.
The file Dog.java contains a declaration for a Dog class. Save this file to your directory and study it -- notice what instance variables and methods are provided.
The file DogTest.java contains a simple driver program that creates a dog and makes it speak. Study DogTest.java, save it to your directory, and then run it to confirm what it does.
The file Yorkshire.java contain declarations for a class that extends Dog. Save and study this files as well. Notice that the constructor calls the constructor of the Dog class using the reserved word super. Modify DogTest.java to add statements to add and print a Yorkshire (also make it speak).
The file Labrador.java also extends the Dog class. However, note that the Labrador constructor takes two parameters: the name and color of the labrador, both strings. Notice that this class contains an error:
Implicit super constructor Dog() is undefined
Fix the problem and then modify DogTest.java to create and make the Dog, Labrador, and Yorkshire all speak.
Add code to DogTest.java to print the average breed weight for both your Labrador and your Yorkshire. Use the avgBreedWeight method for both. Look at the error that you get. Figure out what is wrong and fix the problem by adding the needed code to the Yorkshire class.
To submit your code: Tar the files in your lab2 directory and copy the tgz file to the directory /home/staff/bouchard/CPSC170A/lab2. Be sure to name the tar file with your names, not lab2.tgz.