Can someone please help me keep the contents of this program but code it differe
ID: 3545513 • Letter: C
Question
Can someone please help me keep the contents of this program but code it differently? I prefer to have the main in the bottom and function up top...
import java.util.Scanner;
public class findFigure {
public static void main(String [] a)
{
Scanner input=new Scanner (System.in);
double [][] picture ={{1.2,1.3,4.5,6.0,2.7}
,{1.7,3.3,4.4,10.5,17.0},
{1.1,4.5,2.1,25.3,9.2},
{1.0,9.5,8.3,2.9,2.1}};
System.out.println("Please enter threshold number: ");
double threshold;
threshold= input.nextDouble();
display(picture,threshold);
}
public static double average(double a[][]){
double total=0;
double ave;
int count=0;
for(int i =0;i<4;i++){
for(int j=0;j<5;j++){
total= a[i][j]+total;
count++;
}
}
ave = total/count;
return ave;
}
public static void display(double a[][], double num){
double array [][] = new double [4][5];
double x = num*(average(a));
for(int i = 0; i<4;i++){
for(int j = 0;j<5;j++){
if(a[i][j]<x)
array[i][j]= 0.0;
else
array[i][j]= 1.0;
}
}
for(int i = 0;i<4;i++){
for(int j =0;j<5;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
}
Explanation / Answer
import java.util.Scanner;
public class findFigure {
public static double average(double a[][]){
double total=0;
double ave;
int count=0;
for(int i =0;i<4;i++){
for(int j=0;j<5;j++){
total= a[i][j]+total;
count++;
}
}
ave = total/count;
return ave;
}
public static void display(double a[][], double num){
double array [][] = new double [4][5];
double x = num*(average(a));
for(int i = 0; i<4;i++){
for(int j = 0;j<5;j++){
if(a[i][j]<x)
array[i][j]= 0.0;
else
array[i][j]= 1.0;
}
}
for(int i = 0;i<4;i++){
for(int j =0;j<5;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
public static void main(String [] a)
{
Scanner input=new Scanner (System.in);
double [][] picture ={{1.2,1.3,4.5,6.0,2.7}
,{1.7,3.3,4.4,10.5,17.0},
{1.1,4.5,2.1,25.3,9.2},
{1.0,9.5,8.3,2.9,2.1}};
System.out.println("Please enter threshold number: ");
double threshold;
threshold= input.nextDouble();
display(picture,threshold);
}
}