10A

Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, . . ., n² is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value.

magic square

Your task is to create a class MagicSquare whose constructor takes an n × n.array. It has the following methods:

isUnique() which returns true if all the numbers 1 to n² appear once and only once in the array. Otherwise, it returns false

isMagicSquare() which returns true if this array forms a magic square and false otherwise.

You need to test two features:

  1. Does each of the numbers 1, 2, ..., n² occur in the array?
  2. Are the sums of the rows, columns, and diagonals equal to each other?

A good design will use helper methods.

For the draft, supply the constructor and isUnique method. Make isMagicSquare a stub.

10A draft: http://cs12.cs.sjsu.edu:8080/netbrat/files?repo=ext&problem=13041618422815099690816155125
10A final: http://cs12.cs.sjsu.edu:8080/netbrat/files?repo=ext&problem=13041618422815099690816155125&level=2

10B

Reimplement the BankAccount class so that it is immutable. The deposit and withdraw methods need to return new BankAccount objects with the appropriate balance. You also need a getBalance method. Include javadoc for all the methods.

For the draft, provide the instance variable and a constructor that takes an inital balance as an argument. Write the javadoc for the constructor. It needs to compile. There will be no tester or output for this and you will not run it in NetBrat.

 

10B draft: No link
10B final: http://cs12.cs.sjsu.edu:8080/netbrat/files?repo=ext&problem=13041618536652773677238854935&level=2

10C

Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or
west.

Supply a constructor that specifies the initial x, y coordinates and the direction of the robot as ints.

Supply methods

    public void turnLeft()
    public void turnRight()
    public void move()
    public Point getLocation()
    public String getDirection()

The turnLeft and turnRight methods change the direction but not the location. The move method moves the robot by one unit in the direction it is facing. The getDirection method returns a string "N", "E", "S", or "W".

This is an example of describing the position of an objet. Define constants to represent the directions the robot can face. They will have his form

public static final int NORTH = ...;
public static final int SOUTH = ...;
public static final int EAST = ...;
public static final int WEST = ...;

You pick the values for the constants.

The getDirection() method will return the Strings N, S, E, and W to indicate the direction.

getLocation() returns a Point containing the x and y location of the Robot

For the draft, define the instance variables, the constants, and finish the constructor.

10C draft: http://cs12.cs.sjsu.edu:8080/netbrat/files?repo=ext&problem=13041619018013096523873730197
10C final: http://cs12.cs.sjsu.edu:8080/netbrat/files?repo=ext&problem=13041619018013096523873730197&level=2