From 518bf4d7418a8c6a035ede79d69582e93e230702 Mon Sep 17 00:00:00 2001 From: Masayuki Matsuda Date: Tue, 8 Mar 2022 01:41:50 -0800 Subject: [PATCH] Complete assignment --- src/main/java/ca/ciccc/Assignment2.java | 74 ++++++++++++++++++++++--- src/main/java/ca/ciccc/MagicSquare.java | 59 ++++++++++++++++---- 2 files changed, 114 insertions(+), 19 deletions(-) diff --git a/src/main/java/ca/ciccc/Assignment2.java b/src/main/java/ca/ciccc/Assignment2.java index e51f3fd..eb316e7 100644 --- a/src/main/java/ca/ciccc/Assignment2.java +++ b/src/main/java/ca/ciccc/Assignment2.java @@ -23,8 +23,22 @@ public class Assignment2 { */ public String numberLoops(int n) { // Todo: your code goes here + String lines = ""; - return null; + for(int i = 1; i <= n; ++i){ + String line = ""; + for (int j = 0; j < n - i; ++j){ + line += "."; + } + for (int j = 0; j < i; ++j) { + line += String.format("%d", i); + } + lines += line; + if (i < n) { + lines += "\n"; + } + } + return lines; } /** @@ -34,8 +48,17 @@ public String numberLoops(int n) { */ public boolean countNumbers(int[] nums) { // Todo: your code goes here - - return false; + int numberOfOnes = 0; + int numberOfSevens = 0; + for (int i = 0; i < nums.length; ++i) { + if (nums[i] == 1) { + numberOfOnes += 1; + } + if (nums[i] == 7) { + numberOfSevens += 1; + } + } + return numberOfOnes > numberOfSevens; } /** @@ -52,8 +75,17 @@ public boolean countNumbers(int[] nums) { */ public int sumExcept13(int[] nums) { // Todo: your code goes here + int total = 0; + + for(int i = 0; i < nums.length; ++i) { + if (nums[i] == 13) { + ++i; + continue; + } + total += nums[i]; + } - return 0; + return total; } /** @@ -69,8 +101,16 @@ public int sumExcept13(int[] nums) { */ public int[] shiftArray(int[] nums) { // Todo: your code goes here + int[] shifted = new int[nums.length]; - return null; + for (int i = 0; i < nums.length; ++i) { + if (i == 0) { + shifted[shifted.length -1] = nums[i]; + } else { + shifted[i - 1] = nums[i]; + } + } + return shifted; } /** @@ -80,7 +120,18 @@ public int[] shiftArray(int[] nums) { */ public boolean tripleIncreasingOrder(int[] nums) { // Todo: your code goes here - + int increasingStreak = 1; + int lastNumber = nums[0]; + for (int i = 1; i < nums.length; ++i){ + if (nums[i] == lastNumber + 1 ) { + increasingStreak += 1; + } else { increasingStreak = 1; + } + lastNumber = nums[i]; + if (increasingStreak == 3) { + return true; + } + } return false; } @@ -91,8 +142,17 @@ public boolean tripleIncreasingOrder(int[] nums) { */ public boolean evenOrOdd(int[] nums){ // Todo: your code goes here + int evens = 0; + int odds = 0; - return false; + for (int i = 0; i < nums.length; ++i) { + if (nums[i] % 2 == 0) { + evens += 1; + } else { + odds += 1; + } + } + return evens == odds; } } diff --git a/src/main/java/ca/ciccc/MagicSquare.java b/src/main/java/ca/ciccc/MagicSquare.java index d146a6e..735de74 100644 --- a/src/main/java/ca/ciccc/MagicSquare.java +++ b/src/main/java/ca/ciccc/MagicSquare.java @@ -1,12 +1,11 @@ package ca.ciccc; /** - * * Magic Squares are square arrays of numbers that have the interesting property that * the numbers in each column, and in each row, all add up to the same total. - * + *

* Given an n x n square of numbers, determine if it is magic square. - * + *

* Reference: {@link java.lang.Integer} * Integer is a wrapper class for {@code int} primitive type. */ @@ -18,6 +17,7 @@ public class MagicSquare { /** * Constructor that takes n as the number of rows and cols. + * * @param n number of rows and cols. */ public MagicSquare(int n) { @@ -28,7 +28,8 @@ public MagicSquare(int n) { /** * Constructor that takes n as the number of rows and cols. * Initializes square 2-Dimensional array. - * @param n number of rows and cols. + * + * @param n number of rows and cols. * @param square */ public MagicSquare(int n, Integer[][] square) { @@ -38,6 +39,7 @@ public MagicSquare(int n, Integer[][] square) { /** * Getter for square + * * @return {@code this.square} */ public Integer[][] getSquare() { @@ -46,6 +48,7 @@ public Integer[][] getSquare() { /** * Setter for square. + * * @param square */ public void setSquare(Integer[][] square) { @@ -54,29 +57,61 @@ public void setSquare(Integer[][] square) { /** * Check if {@code square} is magic square or not. (static helper method) + * * @param square 2-Dimensional array * @return {@code true} if {@code square} is magic, otherwise {@code false} - * + *

* Sample Input * { - * {16, 3, 2, 13}, - * {5, 10, 11, 8}, - * {9, 6, 7, 12}, - * {4, 15, 14, 1} + * {16, 3, 2, 13}, + * {5, 10, 11, 8}, + * {9, 6, 7, 12}, + * {4, 15, 14, 1} * } - * + *

* Sample Output * true - * + *

* Explanation: The sums of each row and col are equal. */ public static boolean isMagicSquare(Integer[][] square) { // TODO: You need to implement this method. - return false; + int totalSupposedToBe = 0; + int squareHeight = square.length; + for (int c = 0; c < squareHeight; ++c) { + int firstRowTotal = 0; + + for (int r = 0; r < squareHeight; ++r) { + + firstRowTotal += square[r][c]; + } + totalSupposedToBe = firstRowTotal; + } + for (int r = 1; r < squareHeight; ++r) { + int rowTotal = 0; + for (int c = 0; c < squareHeight; ++c) { + rowTotal += square[r][c]; + } + + if (rowTotal != totalSupposedToBe) { + return false; + } + } + for (int c = 0; c < squareHeight; ++c) { + int columnTotal = 0; + for (int r = 0; r < squareHeight; ++r) { + columnTotal += square[r][c]; + } + if (columnTotal != totalSupposedToBe) { + return false; + } + } + return true; } /** * Check if {@code this.square} is magic square or not. + * * @return {@code true} if {@code square} is magic, otherwise {@code false} */ public boolean isMagicSquare() {