subject

public class Exercise_07 { public static void main(String[] args) { System. out. println(bin2Dec("1100100")); // Purposely throwing an exception... System. out. println(bin2Dec("lafkja")); } public static int bin2Dec(String binary) throws NumberFormatException { if (!isBinary(binary)) { throw new NumberFormatException(binary + " is not a binary number."); } int power = 0; int decimal = 0; for (int i = binary. length() - 1; i >= 0; i--) { if (binary. charAt(i) == '1') { decimal += Math. pow(2, power); } power++; } return decimal; } public static boolean isBinary(String binary) { for (char ch : binary. toCharArray()) { if (ch != '1' && ch != '0') return false; } return true; } }

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 12:10
1. declare a constant named cents_per_pound and initialize with 25. 2. get the shipping weight from user input storing the weight into shipweightpounds. 3. using flat_fee_cents and cents_per_pound constants, assign shipcostcents with the cost of shipping a package weighing shipweightpounds.
Answers: 2
question
Computers and Technology, 23.06.2019 09:00
What provides an array of buttons for quick access to commonly used commands and tools
Answers: 1
question
Computers and Technology, 23.06.2019 19:30
You can apply several different worksheet themes from which tab?
Answers: 1
question
Computers and Technology, 23.06.2019 20:30
If chris has a car liability insurance, what damage would he be covered for
Answers: 1
You know the right answer?
public class Exercise_07 { public static void main(String[] args) { System. out. println(bin2Dec("11...
Questions
question
Mathematics, 13.10.2019 22:30
Questions on the website: 13722363