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...
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...
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...
Consider the following class and the main method below. Trace the code, then answer the questions...
Consider the following class and the main method below. Trace the code, then answer the questions on the right. public class SomeClass { private String aName; private int aNumber; private boolean amAwesome; public SomeClass(String name, int number){ aName = name; aNumber = number; amAwesome = true; } public SomeClass(String name, int number, boolean awesome){ aName = name; aNumber = number; amAwesome = awesome; } public void methodAwesome(int number){ if(amAwesome) aNumber += number - 5; amAwesome = !amAwesome; } public int...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT