Question

In: Computer Science

In java run through eclipse /**    *    * Given a square array, determines if...

In java run through eclipse

/**
   *
   * Given a square array, determines if it is diagonal
   * That is, returns true if all values in the array are 0
   * except the main diagonal. The main diagonal is entries of the form
   * data[i][j] where i == j. Elements on the main
   * diagonal can be 0 or any other number.
   *
   * Examples:
   * {{1,0,0},
   * {0,2,0}
   * {0,0,3}} yields true
   *
   * {{1,0,9},
   * {0,2,0},
   * {0,0,3}} yields false because 0,2 is nonzero
   *
   * {{0,0,0},
   * {0,0,0},
   * {0,0,3}} yields true because there can be 0
   * entries on the diagonal if desired
   * @param data input array
   * @return true if it is diagonal, false otherwise
   */
   public static boolean isDiagonal(int[][] data) {
      
       return false;
   }

Solutions

Expert Solution

public class IsDiagonal {


    /**
     * Given a square array, determines if it is diagonal
     * That is, returns true if all values in the array are 0
     * except the main diagonal. The main diagonal is entries of the form
     * data[i][j] where i == j. Elements on the main
     * diagonal can be 0 or any other number.
     * <p>
     * Examples:
     * {{1,0,0},
     * {0,2,0}
     * {0,0,3}} yields true
     * <p>
     * {{1,0,9},
     * {0,2,0},
     * {0,0,3}} yields false because 0,2 is nonzero
     * <p>
     * {{0,0,0},
     * {0,0,0},
     * {0,0,3}} yields true because there can be 0
     * entries on the diagonal if desired
     *
     * @param data input array
     * @return true if it is diagonal, false otherwise
     */
    public static boolean isDiagonal(int[][] data) {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (i != j && data[i][j] != 0) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isDiagonal(new int[][]{{1, 0, 0}, {0, 2, 0}, {0, 0, 3}}));
        System.out.println(isDiagonal(new int[][]{{1, 0, 9}, {0, 2, 0}, {0, 0, 3}}));
        System.out.println(isDiagonal(new int[][]{{0, 0, 0}, {0, 0, 0}, {0, 0, 3}}));
    }
}


Related Solutions

Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
THIS IS FOR JAVA Given an oversize array of size words and a word to remove,...
THIS IS FOR JAVA Given an oversize array of size words and a word to remove, write a method that returns the array with each occurrence of the given word removed. Shift the remaining words in the nonempty part of the array to the left so that each occurrence of the given word is overwritten. (Leave the words in the empty part of the array unchanged.) Hint: To understand the test cases, note that the size (but not the capacity)...
In JAVA Directions: You must trace through the code and determine the status of the array....
In JAVA Directions: You must trace through the code and determine the status of the array. The code can be found on the second page of this packet. Assume Array A = Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Value 33 12 39 6 -2 30 15 11 55 100 40 39 1 How many elements does A have? ______________ What is the start index? _______________ (assume this value is stored in a...
Write a program in Java with a Scanner. Given an array and a number k where...
Write a program in Java with a Scanner. Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array. It is given that all array elements are distinct. Example: Input: arr[] = {7,10,4,3,20,15} k = 3 Output: 7
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign4Test.java. Copy your code from Assignment 3 into the Student.java and StudentList.java. Assign4Test.java should contain the main method. In Student.java, add a new private variable of type Integer called graduationYear. In Student.java, create a new public setter method setGraduationYear to set the graduationYear. The method will have one parameter of type Integer. The method will set...
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Using Java please You are given an array of integers arr. Your task is to count...
Using Java please You are given an array of integers arr. Your task is to count the number of contiguous subarrays, such that each element of the subarray appears at least twice. E.g For arr = [0, 0, 0], the output should be duplicatesOnSegment(arr) = 3.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT