Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Scanner; public class Calculator { public static void main ( String [ ] args ) { Scanner scanner = new Scanner ( System .

import java.util.Scanner;
public class Calculator {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
char tryAgain;
do {
startEnteringBinaryNumbers(scanner);
System.out.print("Would you like to try again [Y or y for yes]?");
tryAgain = scanner.next().charAt(0);
} while (tryAgain =='Y'|| tryAgain =='y');
scanner.close();
System.out.println("Thank you for using the program");
}
public static void startEnteringBinaryNumbers(Scanner scanner){
int numBinaryNumbers;
int sum =0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
System.out.print("How many binary numbers would you like to enter? ");
numBinaryNumbers = scanner.nextInt();
for (int i =0; i numBinaryNumbers; i++){
System.out.print("Enter the binary number: ");
String binaryNumber = enterBinaryNumber(scanner);
int decimalValue = convertBinaryToDecimal(binaryNumber);
sum += decimalValue;
if (decimalValue min){
min = decimalValue;
}
if (decimalValue > max){
max = decimalValue;
}
System.out.println("The binary number you just entered has decimal value "+ decimalValue);
}
double average =(double) sum / numBinaryNumbers;
System.out.println("The sum is "+ sum);
System.out.println("The minimum value is "+ min);
System.out.println("The maximum value is "+ max);
System.out.println("The average is "+ average);
}
public static String enterBinaryNumber(Scanner scanner){
return scanner.next();
}
public static int convertBinaryToDecimal(String binaryNumber){
int decimalValue =0;
int power =0;
for (int i = binaryNumber.length()-1; i >=0; i--){
if (binaryNumber.charAt(i)=='1'){
decimalValue += Math.pow(2, power);
}
power++;
}
return decimalValue;
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions

Question

4-6 Is there a digital divide? If so, why does it matter?

Answered: 1 week ago