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...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public static void main (String args []) { System.out.println("File:../SmallAreaIncomePovertyEstData.text"); System.out.println("Id" + "\t" + "Population" + "\t" + "ChildPop" + "\t" + "CPovPop" + "\t" + "CPovPop%"); }// read the data try (FileReader fr = new FileReader("File: C:\\605.201/SmallAreaIncomePovertyEstData.text")) { int c; while (( c = fr.read())!= -1){ System.out.print((char) c); } } catch(IOException e) { System.out.println("I/O Error" + e); }    } Please help to fix
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...
Trace through the short-run, intermediate, and long-run effects of an increase in consumer wealth.  (Remember there are...
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 in gov’t spending,...
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...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT