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. 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
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 highest1 = 0;
int highestRow1 = 0;
int highestColumn1 = 0;
for (int i = 0; i < speedsArray.length; i++) {
for (int j = 0; j < speedsArray[i].length; j++) {
if (speedsArray[i][j] > highest1) {
highest1 = speedsArray[i][j];
highestRow1 = i;
highestColumn1 = j;
}
}
}
int highest2 = 0;
int highestRow2 = 0;
int highestColumn2 = 0;
for (int i = 0; i < speedsArray.length; i++) {
for (int j = 0; j < speedsArray[i].length; j++) {
if (speedsArray[i][j] > highest2 && speedsArray[i][j] != highest1) {
highest2 = speedsArray[i][j];
highestRow2 = i;
highestColumn2 = j;
}
}
}
int highest3 = 0;
int highestRow3 = 0;
int highestColumn3 = 0;
for (int i = 0; i < speedsArray.length; i++) {
for (int j = 0; j < speedsArray[i].length; j++) {
if (speedsArray[i][j] > highest3 && speedsArray[i][j] < highest1 && speedsArray[i][j] < highest2) {
highest3 = speedsArray[i][j];
highestRow3 = i;
highestColumn3 = j;
}
}
}
int lowest1 = Integer.MAX_VALUE;
int lowestRow1 = 0;
int lowestColumn1 = 0;
for (int i = 0; i < speedsArray.length; i++) {
for (int j = 0; j < speedsArray[i].length; j++) {
if (speedsArray[i][j] < lowest1) {
lowest1 = speedsArray[i][j];
lowestRow1 = i;
lowestColumn1 = j;
}
}
}
int lowest2 = Integer.MAX_VALUE;
int lowestRow2 = 0;
int lowestColumn2 = 0;
for (int i = 0; i < speedsArray.length; i++) {
for (int j = 0; j < speedsArray[i].length; j++) {
if (speedsArray[i][j] < lowest2 && speedsArray[i][j] != lowest1) {
lowest2 = speedsArray[i][j];
lowestRow2 = i;
lowestColumn2 = j;
}
}
}
int lowest3 = Integer.MAX_VALUE;
int lowestRow3 = 0;
int lowestColumn3 = 0;
for (int i = 0; i < speedsArray.length; i++) {
for (int j = 0; j < speedsArray[i].length; j++) {
if (speedsArray[i][j] < lowest3 && speedsArray[i][j] != lowest1 && speedsArray[i][j] != lowest2) {
lowest3 = speedsArray[i][j];
lowestRow3 = i;
lowestColumn3 = j;
}
}
}
System.out.println("The fist highest speeding is " + highest1 + "Km on month " + monthsArray[highestColumn1+1]
+ " from " + citiesArray[highestRow1] + " city");
System.out.println("The second highest is " + highest2 + "Km on month " + monthsArray[highestColumn2+1] + " from "
+ citiesArray[highestRow2] + " city");
System.out.println("The third highest is " + highest3 + "Km on month " + monthsArray[highestColumn3+1] + " from "
+ citiesArray[highestRow3] + " city");
System.out.println();
System.out.println("The fist lowest is " + lowest1 + "Km on month" + monthsArray[lowestColumn1+1] + " from "
+ citiesArray[lowestRow1] + " city");
System.out.println("The second lowest is " + lowest2 + "Km on month " + monthsArray[lowestColumn2+1] + " from "
+ citiesArray[lowestRow2] + " city");
System.out.println("The third lowest is " + lowest3 + "Km on month " + monthsArray[lowestColumn3+1] + " from "
+ citiesArray[lowestRow3] + " city");
}
}
Comments
Leave a comment