Question

A company wants to invest into building a golf course. They could choose the land to...

A company wants to invest into building a golf course. They could choose the land to build the course from a rectangle area of size MxN. The rectangle area is divided into MxN cells. They measured the elevation of each cell and recorded it into an integer number. The company decided that the golf course should be a KxK square (K ≤ min(M,N)). Thus, there are (M-K+1)(NK+1) possible KxK squares that can be used to build the golf course. For each such square, the company wants to know the highest elevation of a cell inside the square. They also need to know how many cells that have such highest elevations. Write a program that help the company do the above task. Input You need to open a data file from the command-line argument and get the information from the file prior to perform the above task. This data file should be stored in the same directory (folder) as your java program. A sample data file, GolfCourseData, is for you to reference and this data file has the following format. You may acquire a copy ot this data file from the Blackboard. The first line contains t, the number of test cases (about 10). Followed by t test cases. Each test case has the following form: • The first line contains three integers M, N, K (1 ≤ M, N ≤ 500, 1 ≤ K ≤ min(M,N) ). • There are M lines follow. Each line contains N nonnegative integers not exceeding 10000. Each integer represents the elevation of a cell. There is a blank line after each test case. Output For each test case, in the first line print "Case d:" where d is the number of the test case. Then print M-K+1 lines and in each line print N-K+1 entries. Successive entries should be separated by spaces. The entry in the ith line and the jth column has the following form: • The first number is the highest elevation of a cell in the KxK square whose top left corner is (i,j). • If there is more than one cell in the square that has the highest elevation, print (c), where c is the number of cells inside the square that have the highest elevation. Print a blank line after each test case.

Language would be java

this is the .txt file

2
3 3 2
5 5 5
5 5 5
5 5 5

3 3 2
4 2 1
3 5 7
2 8 8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE :

import java.io.*;

class Main{
   public static void main(String [] args){
       String fileName = "input.txt"; //The file has the name "input" and is in the same directory as the program
      
       try{ //For exceptions regarding file not found and input / output exceptions
           File file = new File(fileName);
           FileInputStream fis = new FileInputStream(file);
           InputStreamReader isr = new InputStreamReader(fis);
           BufferedReader br = new BufferedReader(isr); //Reads file line by line
          
           String line=br.readLine();
           int testCases = Integer.parseInt(line); //First line of the input file is the number of test case
          
           for(int tt=1;tt<=testCases;tt++){
               line = br.readLine();
               String [] mnk = line.split(" "); //First line of each test case
              
               int m,n,k;
               m = Integer.parseInt(mnk[0]);
               n = Integer.parseInt(mnk[1]);
               k = Integer.parseInt(mnk[2]);
              
               int [][] rectangle = new int[m][n]; //Below for loop creates a grid where elevation level of every cell is stored
               for(int i=0;i<m;i++){
                   line = br.readLine();
                   String [] mLine = line.split(" ");
                   for(int j=0;j<n;j++){
                       rectangle[i][j] = Integer.parseInt(mLine[j]);
                   }
               }
              
               System.out.println("Case " + tt + ": ");
              
               for(int i=0;i<m-k+1;i++){
                   for(int j=0;j<n-k+1;j++){
                       int maxElevation=0;
                       int cnt=0;
                       for(int I=i;I<i+k;I++){
                           for(int J=j;J<j+k;J++){
                               if(rectangle[I][J] > maxElevation){
                                   maxElevation = rectangle[I][J];
                                   cnt = 1;
                               }
                               else if(rectangle[I][J] == maxElevation){
                                   cnt++;
                               }
                           }
                       }
                      
                       System.out.print(maxElevation);
                       if(cnt > 1){
                           System.out.print("(" + cnt + ")");
                       }
                       System.out.print("\t");
                   }
                   System.out.println();
               }
               System.out.println();
           }
       }
       catch(FileNotFoundException fnfe){
           System.out.println("fnfe");
       }
       catch(IOException ioe){
           System.out.println(ioe);
       }
   }
}

OUTPUT (for the sample file) :

File :

Add a comment
Know the answer?
Add Answer to:
A company wants to invest into building a golf course. They could choose the land to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Iwant answer this using number. Airthemitics. Varibles by python This using if else in python The...

    Iwant answer this using number. Airthemitics. Varibles by python This using if else in python The third picture using if. Elif. Else This using for loop in python A. Perimeter of a Rectangle time limit per test 1 second memory limit per test: 256 megabytes input standard input output standard output You are given two positive integers a and b which are the sides of the rectangle. Print the perimeter of this rectangle. The formula for perimeter is P =...

  • Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp...

    Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp takes a single argument, which is the path to a file containing a square (k × k) matrix M and a non-negative exponent n. It computes M and prints the result Note that the size of the matrix is not known statically. You ust use malloc to allocate space for the matrix once you obtain its size from the input file. Tocompute M". it...

  • [15 marks] Suppose that students enrolled in one course are required to take four tests, and...

    [15 marks] Suppose that students enrolled in one course are required to take four tests, and each student’s final grade for this course is the average of his/her grades of these four tests. This question asks you to write a program that can be used to compute the lowest final grade, highest final grade and the average final grade. General Requirements: Use a5q1.c as the name of your C source code file. We will use the following command on bluenose...

  • Write a C program to compute average grades for a course. The course records are in...

    Write a C program to compute average grades for a course. The course records are in a single file and are organized according to the following format: Each line contains a student’s first name, then one space, then the student’s last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program...

  • You're given a chess board with dimension n x n. There's a king at the bottom...

    You're given a chess board with dimension n x n. There's a king at the bottom right square of the board marked with s. The king needs to reach the top left square marked with e. The rest of the squares are labeled either with an integer p (marking a point) or with x marking an obstacle. Note that the king can move up, left and up-left (diagonal) only. Find the maximum points the king can collect and the number...

  • Please write code in java You’re given a chess board with dimension n x n. There’s...

    Please write code in java You’re given a chess board with dimension n x n. There’s a king at the bottom right square of the board marked with s. The king needs to reach the top left square marked with e. The rest of the squares are labeled either with an integer p (marking a point) or with x marking an obstacle. Note that the king can move up, left and up-left (diagonal) only. Find the maximum points the king...

  • DSC13152.21 Dr. M. Sedaghat Test#2 Nov.2018 r of a miniature golf course that is popular with...

    DSC13152.21 Dr. M. Sedaghat Test#2 Nov.2018 r of a miniature golf course that is popular with college students is trying to determine how many boxes of golf balls they should order at one time for their booming business. Due to the high loss rate of the golf balls they use, they calculated that they would need 8,000 boxes of balls each n and year. It costs them 20% of the cost per box to carry one quantities of boxes of...

  • C++ You're given the pointer to the head nodes of two linked lists. Compare the data...

    C++ You're given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty. Input Format You have to complete the int CompareLists (Node headA, Node* head B) method which takes...

  • C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an...

    C Programming Language Problem Title: Discount Jojo is browsing the internet while suddenly he sees an ad about the new cafe. The promotion is if the price of an item is N dollars, then you can buy the second item for half the price, the third item for a quarter of the original price, and so on, but if it becomes less than M dollars, then you have to pay M dollars. He wonders how much he has to pay...

  • Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin...

    Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin will be processed as follows and passed to the function In the first line, there is a single integer n. In the second line, there is a single integer m. In the ph of the next n lines there are m space-separated integers denoting the throw of the initial grid. In the next line, there is a single integer k. In the next line,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT