Answer to Question #247509 in Java | JSP | JSF for Memo

Question #247509
Write a Java program to display the three highest monthly speeding fines recorded for four
different cities. The following rows and columns represent the monthly speeding fines of each
city.
JAN FEB MAR
JHB 128km 135km 139km
DBN 155km 129km 175km
CTN 129km 130km 185km
PE 195km 155km 221km
Using a Two Dimensional array, produce the speeding fines report including the speeding fines
statistics. The speeding fines statistics must display the highest and lowest speeding fines in the
two dimensional array.
1
Expert's answer
2021-10-07T10:06:56-0400
public class Main {


	public static void main(String[] args) {
		String[] monthsArray = { "City/Month", "JAN", "FEB", "MAR" };
		String[] citiesArray = { "JHB", "DBN", "CTN", "PE" };
		int[][] speedsArray = { { 128, 135, 139 }, { 155, 129, 175 }, { 129, 130, 185 }, { 195, 155, 221 } };


		for (int j = 0; j < monthsArray.length; j++) {
			System.out.print(String.format("%-20s", monthsArray[j]));
		}
		System.out.println();
		for (int i = 0; i < speedsArray.length; i++) {
			System.out.print(String.format("%-20s", citiesArray[i]));
			for (int j = 0; j < speedsArray[i].length; j++) {
				System.out.print(String.format("%-20d", speedsArray[i][j]));
			}
			System.out.println();
		}
		System.out.println();


		int lowest = Integer.MAX_VALUE;
		int lowestRow = 0;
		int lowestColumn = 0;


		int highest = 0;
		int highestRow = 0;
		int highestColumn = 0;
		for (int i = 0; i < speedsArray.length; i++) {
			for (int j = 0; j < speedsArray[i].length; j++) {
				if (speedsArray[i][j] > highest) {
					highest = speedsArray[i][j];
					highestRow = i;
					highestColumn = j;
				}
				if (speedsArray[i][j] < lowest) {
					lowest = speedsArray[i][j];
					lowestRow = i;
					lowestColumn = j;
				}
			}
		}


		System.out.println("The highest speeding is " + highest + "Km on month " + monthsArray[highestColumn + 1]
				+ " from " + citiesArray[highestRow] + " city");
		System.out.println("The lowest is " + lowest + "Km on month " + monthsArray[lowestColumn + 1] + " from "
				+ citiesArray[lowestRow] + " city");


	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS