Question

In: Computer Science

Design a class named Location for locating a maximal value and its location in a two-dimensional...

Design a class named Location for locating a maximal value and its location in a two-dimensional array. The class contains public data fields row, column and maxValue that stores the maximal value and its indices in a two-dimensional array with row and column as int types and maxValue as a double type.

Write the following method that returns the location of the largest element in a two dimensional array: public static location locate Largest(double[][] a)

The return value is an instance of location. Write a test program that prompts the user to enter a two-dimensional array and displays the location of the largest element in the array.

Complete the program "The Location Class" - 9.13. Page 366.  

Add to your class a public Class Level Variable named COUNT to keep counting how many objects have been created (instantiated); So, each time you are creating new object the Count var will be incrementing by 1

.In the main method create 3 objects, call all their methods, and print the final value of the Count var.  

This is java language

Solutions

Expert Solution

JAVA PROGRAM

///////////Location.java//////////////////

package test.location;

public class Location {
  
   //instance level variables
   public int row,column;
   public double maxValue;
  
   //class level COUNT variable
   public static int COUNT = 0;
  
   //constructor
   public Location(){
       COUNT = COUNT+1;//increase COUNT
   }
  
  
   /**
   * Find the largest value and its position from the 2D array
   * return ocation instance with the above values
   * @param a
   * @return
   */
   public static Location locateLargest(double[][] a){
       double max = a[0][0];
       int maxRowIndex = 0;
       int maxColIndex = 0;
       for(int i = 0 ; i < a.length ; i++){
           for(int j = 0; j < a[i].length; j++){
               if(a[i][j] > max){
                   max = a[i][j];
                   maxRowIndex = i;
                   maxColIndex = j;
               }
           }
       }
      
       //create the location instance
       Location l = new Location();
      
       l.maxValue = max;
       l.row = maxRowIndex;
       l.column = maxColIndex;
      
       return l;//return location instance
   }

   @Override
   /**
   * print current location instance
   */
   public String toString() {
       return "[rowIndex=" + row + ", columnIndex=" + column + ", maxValue=" + maxValue + "]";
   }

}

   ///////////TesstMain.java//////////////////

package test.location;

import java.util.Scanner;

public class TestMain {
  
   public static void main(String[] args){
       Scanner sc = new Scanner(System.in);
       TestMain tm = new TestMain();
      
       //create 3 2d arrays from user
       double[][] dblArr1= tm.get2DArrayFromUser(sc);
       double[][] dblArr2= tm.get2DArrayFromUser(sc);
       double[][] dblArr3= tm.get2DArrayFromUser(sc);
      
       //call the class level locateLargest method of location ckass to get location instance
       //to get the location of the max valued index
       Location l1 = Location.locateLargest(dblArr1);
       Location l2 = Location.locateLargest(dblArr2);
       Location l3 = Location.locateLargest(dblArr3);
      
       //print the location received
       //the location instance's toString
       System.out.println("Location1=>"+l1);
       System.out.println("Location2=>"+l2);
       System.out.println("Location3=>"+l3);
      
       System.out.println("Final count of the COUNT var: "+Location.COUNT); //print the class level COUNT variable
       sc.close();//close scanner
   }
  
   /**
   * The method get2DArrayFromUser will take number of ros and columns from
   * user and populate it will double values taken from user
   * @param sc
   * @return
   */
   private double[][] get2DArrayFromUser(Scanner sc){
       System.out.println("Enter number of rows:");
       int rows = sc.nextInt(); //read rows
       System.out.println("Enter number of columns:");
       int cols = sc.nextInt(); //read cols
      
       double[][] arr = new double[rows][cols];
       for(int i = 0; i < rows; i++){
           for(int j = 0; j < cols; j++){
               arr[i][j] = sc.nextDouble();//populate the 2D array
           }
       }
      
       return arr; //return array
   }

}

===============================================

OUTPUT

=================================================

Enter number of rows:
3
Enter number of columns:
1
45.2
25.6
2.36
Enter number of rows:
2
Enter number of columns:
2
12.6
36.21
21.25
45.65
Enter number of rows:
3
Enter number of columns:
2
12.56
56.12
18.56
65.18
17.56
65.71
Location1=>[rowIndex=0, columnIndex=0, maxValue=45.2]
Location2=>[rowIndex=1, columnIndex=1, maxValue=45.65]
Location3=>[rowIndex=2, columnIndex=1, maxValue=65.71]
Final count of the COUNT var: 3


Related Solutions

Design a class named Location for locating a maximal value andits location in a 2-dimensional...
Design a class named Location for locating a maximal value and its location in a 2-dimensional array. The class contains public data fields row, column andmaxValue that store the maximal value and its indices in a 2-dimensional array with row andcolumn as int types andmaxValue as a double typeWrite the following method that returns the location of the largest element in a 2-dimensional array : public static Location locateLargest(double[][] a)The return value is an instance of Location. Write a test...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Java Question Design a class named Person and its two subclasses named Student and Employee. Make...
Java Question Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Use the Date class to create an object for date hired. A faculty member has office hours and a rank. A...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan (the default is SLOW). ■ A private boolean data field named on that specifies whether the fan is on (the default is false). ■ A private double data field named radius that specifies...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to store game states and at least two member functions for adding players’ moves and printing the game board. Write a driver to test your class. (2). The data type of the two-dimensional array can be either int or char. As a passlevel program, the size of the board can be hardcoded to 10 or a constant with a pre-set value, anything between 4 and...
Design a class named Message to represent a sentence or phrase. The class will contain: •...
Design a class named Message to represent a sentence or phrase. The class will contain: • a private string data field to hold the sentence or phrase. • A no-arg constructor with an empty string message. • A constructor that create a message object with the specified string sentence or phrase. • Accessor and mutator (getter/setter) for string data field. • A method named getVowels ( ) that returns the number of vowels in a sentence or phrase. • A...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two team names and a final score for each team. Include methods to set and get the values for each data field. Create the class diagram and write the pseudocode that defines the class. b. Design an application that declares three BaseballGame objects and sets and displays their values. c. Design an application that declares an array of 12 BaseballGame objects. Prompt the user for...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan. ■ A private bool data field named on that specifies whether the fan is on (the default is False). ■ A private float data field named radius that...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT