Question

In: Computer Science

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 type

Write 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 program that prompts the user to enter a two-dimensional array and displays the locations of the largest element in the array.

Algorithm:

Create the Location class with data fields row, column, maxValue.

In main method of your test program called “Inclass09A, get number of rows and columns in the array from the console.

Create the array.

Ask the user to enter the values for the array and populate the array.

Create/Invoke the locateLargest method, passing it the array as an argument.

In the locateLargest method, create a Location object, then save the first element in the 2-dim array as the largest. Then traverse the array saving any element larger than the previous largest element and the associated row and column.

The locateLargest method returns a Location object that contains the maxValue, row, and column of the maxValue. Assign the Location object into a reference variable of the type Location.

Print out a message that indicates the largest element, and it’s location (row and column) referencing the Location object’s instance variables.

Sample Run:

---------------------------------------------------

Enter the number of rows and columns of the array: 3 4

Enter the array:

23.5 35 2 10

4.5 3 45 3.5

35 44 5.5 9.6

The largest element, 45.0, is located at (1, 2)

Sample Run:

---------------------------------------------------

Enter the number of rows and columns of the array: 2 3

Enter the array:

1 2 3

4 4 3

The largest element, 4.0, is located at (1, 0)

Solutions

Expert Solution

import java.util.Scanner;

class Location{

public int row;

public int column;

public double maxValue;

}

public class Inclass09A{

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int r, c;

System.out.print("Enter the number of rows and columns of the array: ");

r = in.nextInt();

c = in.nextInt();

System.out.println("Enter the array:");

double arr[][] = new double[r][c];

for(int i=0; i

for(int j=0; j

arr[i][j] = in.nextDouble();

}

}

Location loc = locateLargest(arr);

System.out.println("The largest element, "+loc.maxValue+", is located at ("+loc.row+", "+loc.column+")");

}

public static Location locateLargest(double[][] a){

Location loc = new Location();

loc.row = 0;

loc.column = 0;

loc.maxValue = Double.MIN_VALUE;

for(int i=0; i

for(int j=0; j

if(loc.maxValue

loc.maxValue = a[i][j];

loc.row = i;

loc.column = j;

}

}

}

return loc;

}

}


Related Solutions

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...
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...
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...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
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...
Design a class named Account (put it in a package named accountspackages) with the following UML...
Design a class named Account (put it in a package named accountspackages) with the following UML diagram: Account -customerID: int -customerName: String -balance: double +setCustomerID(int): void +setCustomerName(String): void +setBalance(double):void +getCustomerID(): int +getCustomerName(): String +getBalance(): double +deposit(double): void +withdraw(double): void +printInformation():void The method withdraw(double) withdraws a specified amount from the account if the amount is less than or equal the balance, otherwise the method prints the message: Sorry! The account does not have sufficient funds. The method printInformation() prints:     the...
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...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT