Question

In: Computer Science

Trace the sample run provided for Search2D class .................................................................. public class Search2D {     /**      * Searches...

Trace the sample run provided for Search2D class

..................................................................

public class Search2D

{

    /**

     * Searches for the desiredItem in a rectangular matrix[][] where

     * elements are sorted within each row and within each column

     * If the element is found, prints its position,

     * otherwise prints "not found"

     *

     * @author  YOUR NAME

     * @version 10/20/2020

     *

     */

    private void search(int[][] matrix, int desiredItem)

    {

        // TODO Project 4

        // TODO must implement with one loop only

        System.out.println("Searching for " + desiredItem);

    }

    // driver to test search method

    public static void main(String[] args)

    {

        int matrix[][] = {

                {9, 10, 20, 21, 40},

                {11, 15, 25, 26, 45},

                {13, 27, 29, 30, 48},

                {17, 32, 33, 34, 50}};

        Search2D search2D = new Search2D();

        System.out.println("\u001B[35m\u001B[1m*** These should be successful searches: ***\u001B[0m");

        for (int r = 0; r < matrix.length; r++)

        {

            for (int c = 0; c < matrix[r].length; c++)

            {

                search2D.search(matrix, matrix[r][c]);

            }

        }

        System.out.println("\n\u001B[35m\u001B[1m*** These should be unsuccessful searches: ***\u001B[0m");

        search2D.search(matrix,28);

        search2D.search(matrix,5);

        search2D.search(matrix,12);

    }

}

Solutions

Expert Solution

Theory:

Matrix

Matrix is a array which is rectangular. In this we store elements in the row and column form.

Solution:

Algorithm:

1. Start

2. Create class Search2D

3. write method search for searching

4. set index top right element

5. use while loop for searching till i<4 and j>=0

6. By using if condition check if the index element and desired element are equal then display element is found else display element is not found.

7. In main method create matrix also create class objects.

8.by using class objects call the search() method and pass matrix and search element.

9.end

Code:

class Search2D {
  
private void search(int[][] matrix, int desiredItem)
{
  
int i = 0, j = 4; // set indexes for top right element
System.out.println("\nSearching for " + desiredItem);
while (i < 4 && j >= 0) {
if (matrix[i][j] == desiredItem) {
System.out.print("Found at " + i + " " + j+"\n");
return;
}
if (matrix[i][j] > desiredItem)
j--;
else // if mat[i][j] < x
i++;
}
  
System.out.print("Element not found\n");
  
}
// driver to test search method
public static void main(String[] args)
{
int matrix[][] = {

{9, 10, 20, 21, 40},

{11, 15, 25, 26, 45},

{13, 27, 29, 30, 48},

{17, 32, 33, 34, 50}};

Search2D search2D = new Search2D();

System.out.println("\n***These should be successful searches: ***");

for (int r = 0; r < matrix.length; r++)

{

for (int c = 0; c < matrix[r].length; c++)

{

search2D.search(matrix, matrix[r][c]);

}

}

System.out.println("\n*** These should be unsuccessful searches: ***");

search2D.search(matrix,28);

search2D.search(matrix,5);

search2D.search(matrix,12);
}
}

Output:


Related Solutions

Trace the sample run provided for Search2D class .................................................................. public class Search2D {     /**     ...
Trace the sample run provided for Search2D class .................................................................. public class Search2D {     /**      * Searches for the desiredItem in a rectangular matrix[][] where      * elements are sorted within each row and within each column      * If the element is found, prints its position,      * otherwise prints "not found"      *      * @author YOUR NAME      * @version 10/20/2020      *      */     private void search(int[][] matrix, int desiredItem)     {         //...
implement please... ........................................... public class TernarySearch {     /** Task: Searches for a value in an array....
implement please... ........................................... public class TernarySearch {     /** Task: Searches for a value in an array.      * @param a an array of Comparable objects      * @param desiredItem an item to search for         * @param n an integer > 0      */     public static <T extends Comparable<? super T>>     boolean ternarySearch(T[] a, T desiredItem, int n)     {         // it is assumed that the data is already sorted         return ternarySearch(a, 0, n-1, desiredItem);     } // end ternarySearch     /** Task: recursive ternarySearch search through...
Fix the following java code package running; public class Run {    public double distance; //in...
Fix the following java code package running; public class Run {    public double distance; //in kms    public int time; //in seconds    public Run prev;    public Run next;    //DO NOT MODIFY - Parameterized constructor    public Run(double d, int t) {        distance = Math.max(0, d);        time = Math.max(1, t);    }       //DO NOT MODIFY - Copy Constructor to create an instance copy    //NOTE: Only the data section should be...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Searches related to Class, looking back over the Course Objectives for this course math 225N at...
Searches related to Class, looking back over the Course Objectives for this course math 225N at Chamberlain University, what are you looking forward to learning more about throughout your education and career? What are some things that you learned about that you will continue using in your work (be specific on topics and explain how they can be used)? What types of studies would you now be interested in researching and learning about? How might you carry out your research...
1.5 TRACE THROUGH THE SHORT-RUN, INTERMEDIATE, AND LONG-RUN EFFECTS OF AN INCREASE IN CONSUMER WEALTH. (REMEMBER...
1.5 TRACE THROUGH THE SHORT-RUN, INTERMEDIATE, AND LONG-RUN EFFECTS OF AN INCREASE IN CONSUMER WEALTH. (REMEMBER THERE ARE SEVERAL AD DETERMINANTS. BE ABLE TO DO THIS QUESTION FOR ANY OF THEM—THINGS LIKE CONSUMER OPTIMISM, CONSUMER PESSIMISM, DECREASES IN WEALTH, DECREASE IN HOUSEHOLD INDEBTEDNESS, INCREASE IN HOUSEHOLD TAXES, DECREASES IN HOUSEHOLD TAXES, INCREASED EXCESS CAPACITY OF CAPITAL, DECREASED EXCESS CAPACITY OF CAPITAL, INCREASES IN THE COST OF MAINTAINING CAPITAL, DECREASES IN THE COST OF MAINTAINING CAPITAL, INCREASES IN GOV’T SPENDING, DECREASES...
please run it and show a sample. please dont change the methods. public interface PositionalList extends...
please run it and show a sample. please dont change the methods. public interface PositionalList extends Iterable { /** * Returns the number of elements in the list. * @return number of elements in the list */ int size(); /** * Tests whether the list is empty. * @return true if the list is empty, false otherwise */ boolean isEmpty(); /** * Returns the first Position in the list. * * @return the first Position in the list (or null,...
Class Employee (All IN JAVA) public class Employee {public String strName, strSalary; public Employee(){strName = "...
Class Employee (All IN JAVA) public class Employee {public String strName, strSalary; public Employee(){strName = " ";strSalary = "$0";} public Employee(String Name, String Salary){strName = Name;strSalary = Salary;} public void setName(String Name){strName = Name;} public void setSalary(String Salary){strSalary = Salary;}public String getName(){return strName;} public String getSalary(){return strSalary;} public String toString(){return(strName + " has a salary of " + strSalary); Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You...
public class MyLinked {    static class Node {        public Node (double item, Node...
public class MyLinked {    static class Node {        public Node (double item, Node next) { this.item = item; this.next = next; }        public double item;        public Node next;    }    int N;    Node first;     // remove all occurrences of item from the list    public void remove (double item) {        // TODO    } Write the remove function. Do NOT add any fields to the node/list classes, do...
I can't figure out why this won't run. //CryptographyTest.java //package cryptography; import java.util.Scanner; public class CryptographyTest...
I can't figure out why this won't run. //CryptographyTest.java //package cryptography; import java.util.Scanner; public class CryptographyTest {    public static void main(String[] args) { // create a scanner object to read from user Scanner s = new Scanner(System.in); // prompt user for input option while(true) { // loop till user say exit System.out.println("Select Option:"); System.out.println("1. Encrypt"); System.out.println("2. Decrypt"); System.out.println("3. Exit"); String input = s.nextLine(); // get user input int option = 0; try { // check for valid input option...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT