Question: import java.util.Scanner; public class Calculator { static int array [ ] ; public static void main ( String [ ] args ) { int min;

import java.util.Scanner;
public class Calculator {
static int array[];
public static void main(String[] args){
int min;
int max;
int sum;
double average;
Scanner sc = new Scanner(System.in);
char ch ='n';
do{
startEnteringBinaryNumbers(sc);
sum =0;
average =0.0;
min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;
for(int i =0; i < array.length;i++){
if(array[i]< min)
min = array[i];
if(array[i]> max)
sum = sum + array[i];
}
average =(double)sum/array.length;
System.out.println("Sum: "+ sum);
System.out.println("Minimum: "+ min);
System.out.println("Maximum: "+ max);
System.out.println("Average: "+ average);
System.out.println("For try again enter 'y' or 'Y' : ");
ch = sc.next().charAt(0);
System.out.println("");
} while(ch =='y'|| ch =='Y');
}
public static void startEnteringBinaryNumbers(Scanner sc){
System.out.print("How many binary numbers would you like to enter? ");
int n = sc.nextInt();
array = new int[n];
for(int i =0; i < n; i++){
String binaryNum = enterBinaryNumber(sc);
array[i]= convertBinaryToDecimal(binaryNum);
}
}
public static String enterBinaryNumber(Scanner scanner){
System.out.print("Enter binary number: ");
return scanner.next();
}
public static int convertBinaryToDecimal(String binaryNumber){
int dec =0;
int j =0;
for (int i = binaryNumber.length()-1;i >=0; i--){
dec =(int)(dec +(Integer.valueOf(binaryNumber.charAt(i))-48)* Math.pow(2, j));
j++;
}
return dec;
}
}
The following is a sample run of the program: . How many binary numbers would you like to enter? 2
Enter the binary number: 11010
The binary number you just entered has decimal value 26
Enter the binary number: 10101
The binary number you just entered has decimal value 21
The sum is 47 The minimum value is 21
The maximum value is 26
The average is 23.5
Would you like to try again [Y or y for yes]? y
How many binary numbers would you like to enter? 3
Enter the binary number: 1100
The binary number you just entered has decimal value 12
Enter the binary number: 11011
The binary number you just entered has decimal value 27
Enter the binary number: 101
The binary number you just entered has decimal value 5
The sum is 44
The minimum value is 5
The maximum value is 27
The average is 14.666666666666666
Would you like to try again [Y or y for yes]? n

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!