Given a two dimensional array of ints, return the value of the \"most valuable\"
ID: 3687345 • Letter: G
Question
Given a two dimensional array of ints, return the value of the "most valuable" contiguous sub-rectangle in the 20 array. Think of the array as a matrix called "city" representing the value of the property in each block of the rectangular city. Note that some blocks may have a negative value when the structures on the block need to be demolished. The most valuable sub-rectangle must be at least 1 by 1 in size but may be larger. Write JAVA code that will input the array and return the value of the most valuable sub-rectangle. To get credit, your code should be 0(nA3) or better. See next page for an example. Use the following outline for your code, public static int getValueOfMostValuablePlot(int[)[] city) {//check preconditions if (city == null 11 city.length == 0 11 city(0).length == 0 11 lisRectangular(city)) throw new HlegalArgumentExceptlon("Violation of precondition:" "getValueOfMostValuablePlot. The parameter may not be null," ? " must value at least one row and at least" + " one column, and must be rectangular.");Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package plotvalue1;
import java.*;
import java.util.IllegalFormatException;
import java.io.*;
import java.util.Scanner;
import java.lang.IllegalArgumentException;
import java.lang.*;
/**
*
* @author Admin
*/
public class PlotValue1 {
Scanner sc = new Scanner(System.in);
private static boolean isRectangular(int[][] city) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
// finding the most valuable sub rectangle of a 2d array
// example:
public void startUp() {
// say in block 0, there are 5 city valuied at $200,000, $ 2 million, $500,000 , $ 1 million
// and the 5th plot needs to be demolished and hence a negative value of $ - 40,000.- to cover the labor cost
// say in block 1, there are 5 city valuied at $250,000, $ 2.2 million, $560,000 , $ 1.1 million
// and the 5th plot needs to be demolished and hence a negative value of $ - 42,000.- to cover the labor cost
/*
array to represent the above 10 plots in 2 blocks will look like this:
city[0][0] = 200000
city[0][1] = 2000000
city[0][2] = 500000
city[0][3] = 1000000
city[0][4] = -40000
city[1][0] = 250000
city[1][1] = 2200000
city[1][2] = 560000
city[1][3] = 1100000
city[1][4] = -42000
city[2][0] = Value of 0th plot in the 2nd block
city[2][1] = Value of 1st plot in the 2nd block
city[2][2] = Value of 2nd plot in the 2nd block
city[2][3] = Value of 3rd plot in the 2nd block
city[2][4] = Value of 4th plot in the 2nd block
city[3][0] = etc similar above
city[3][1] =
city[3][2] =
city[3][3] =
city[3][4] =
city[4][0] =
city[4][1] =
city[4][2] =
city[4][3] =
city[4][4] =
city[blockNumber][PlotNumber]
first dimension is for the block number
second dimension is for the plot number
*/
int [][] city = new int[10][20];
int i=0,j=0;
int b,p;
// read in the array
System.out.println("Enter Array dimensions b, p = number of blocks, plots ");
b = sc.nextInt();
p = sc.nextInt();
for (i = 0; i<b; i++)
for (j=0; j<p; j++)
city[i][j] = sc.nextInt();
int mostValued=0;
for (i = 0; i<b; i++)
for (j=0; j<p; j++)
if ( city[i][j] > mostValued ) mostValued = city[i][j];
//city[1][]
} // end function start up
public int getValueOfMostValuablePlot(int[][] city) {
// pre validation
if (city == null || city.length == 0 || city[0].length == 0 || !isRectangular(city))
throw new IllegalArgumanetException("Violation of precondition:" +
"From Function: getValueOfMostValuablePlot. "
+ " Empty parameters are not allowed"
+ " a minimum value of 1 row and 1 column must be present"
+ "more over the shape must be rectangular please re enter the data thanks");
return 0;
} // end function getValueOfMostValuablePlot
/**
* @param args the command line arguments
*/
public void main(String[] args) {
// TODO code application logic here
startUp();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.