Question

In: Computer Science

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)

    {

        // 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


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.print("Searching for " + desiredItem + " : ");
        
        
        for(int i=0,j=0;i<matrix.length ; j++) {
                
                if(j == matrix[0].length ) {
                        i++;
                        j=0;
                }
                
                if(i == matrix.length) {
                        break;
                }
                
                if(matrix[i][j] == desiredItem) {
                        System.out.println(i + " " + j);
                        return;
                }
                
        }
        System.out.println("not found!");
        return;

    }

    // 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();

      

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

        {

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

            {

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

            }

        }

      

        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 {     /**      * 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 "...
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...
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 Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature...
public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature { public void method1() { System.out.println("creature 1"); } public void method2() { System.out.println("creature 2"); } public String toString() { return "ocean-dwelling"; } } public class Whale extends Mammal { public void method1() { System.out.println("spout"); } public String toString() { return "BIG!"; } } public class Squid extends SeaCreature { public void method2() { System.out.println("tentacles"); } public String toString() { return "squid"; } } What...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT