1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
NARA [144]
3 years ago
13

Write and test a bin2Dec(String binaryString) method to convert a binary string into a decimal number. Implement the bin2Dec met

hod to throw a NumberFormatException if the string is not a binary string
Computers and Technology
1 answer:
ss7ja [257]3 years ago
7 0

Answer:

Following are the method to this question:

import java.util.*;//import package for user input

public class Main //defining a class  

{

  public static int bin2Dec(String binaryString) throws NumberFormatException//defining a method bin2Dec that accept string value  

  {

       int r=0,p= 1,i;

       Stack<Integer> s = new Stack<>();//creating Stack object

       for(i = binaryString.length()-1;i>=0;i--)//defining for loop to convert binary to decimal value

       {

           if(binaryString.charAt(i) != '0' && binaryString.charAt(i) != '1')//defining if block to check value

           {

               throw new NumberFormatException();//use throw block to check Exception

           }

           if(binaryString.charAt(i) == '1')//defining if block to check first input is value equal to 1

           {

               s.push(p);//using push method to add value in stack

           }

           p*= 2;//calculating the decimal value

       }

       while (!s.isEmpty())//defining while loop to stack is empty

       {

           r += s.pop();//use r variable to calculate value

       }

       return r;//return value

   }

   public static void main(String[] ax) //defining main method

   {

       Scanner oxb= new Scanner(System.in);//creating Scanner class object for input value

       System.out.print("Enter a binary number: ");//print message

       String b = oxb.nextLine();// defining String variable for input value

       try //defining try block

       {

           System.out.print(bin2Dec(b));//use print method to call bin2Dec method

       }

       catch (Exception e)//use catch block to catch Exception

       {

           System.out.print(b+ "Not a binary number: ");//print message

       }

   }

}

output:

Enter a binary number: 101

5

Explanation:

In the above-given code a method, "bin2Dec" is declared, that accept a string variable in its parameter, inside the method an integer variable and stack class object is created, and this method uses two loops, and in the for loop, it converts the binary to a decimal value, in the while loop it checks all value and adds its value in r variable.

In the main method, a scanner class object is created, that use a string variable to input value from the user-end, and use the try and catch block to call method.

You might be interested in
An exact-match query is:
Greeley [361]

Answer:

C. Where a record is returned if its unique identifier matches the search value

Explanation:

Exact match is a Google keyword match type that allows you show your adverts when searcher type the exact word or phrase you are bidding on. An exact match query will always return records that matches what is being searched on.

5 0
3 years ago
When using an online media source, students should ensure the information is
OLEGan [10]
Viable and true. hope this helps!
6 0
4 years ago
Read 2 more answers
Object-oriented development could potentially reduce the time and cost of writing software because: Group of answer choices a) i
spayn [35]

Answer:

b) objects are resuable

Explanation:

In OOP there's code reuse where a method or any other body of code is defined once and called or reused severally.

3 0
4 years ago
What is the missing line?
gregori [183]

Correct question:

What is the missing line?

>>> myDeque = deque('math')

>>> myDeque

deque(['m', 'a', 't'])

Answer:

myDeque.pop()

Explanation:

The double ended queue, deque found in the python collection module is very similar to a python list and can perform operations such as delete items, append and so on.

In the program written above, the missing line is the myDeque.pop() as the pop() method is used to delete items in the created list from the right end of the list. Hence, the 'h' at the right end is deleted and we have the output deque(['m', 'a', 't'])

myDeque.popleft () deletes items from the right.

4 0
3 years ago
Which command can be used to connect to a remote windows share called data on the server called fileserver?
DiKsa [7]

c. c.) smbclient //fileserver/data

7 0
3 years ago
Other questions:
  • List and describe the issues that lead to making a successful website?​
    9·1 answer
  • Which software application should be used to communicate in writing about an upcoming event?
    12·2 answers
  • Jamie posted all the journal entries into ledgers. After the postings, Jamie realizes that the debit side of the travel account
    10·1 answer
  • Universal Containers has a requirement to integrate Salesforce with an external system to control record access. What option sho
    11·1 answer
  • I thought the answer was senior manager see on google it says
    8·1 answer
  • Create an array using the makeRandomArray method, then take a start time using System.currentTimeMillis(). Next, run the array t
    13·1 answer
  • The compound known as butylated hydroxytoluene, abbreviated as BHT, contains carbon, hydrogen, and oxygen. A 1.501 g sample of B
    11·1 answer
  • Aarti, a museum employee, has created a table in access titled “Roman Achitecture”. She has included a field that links users im
    8·1 answer
  • Will give brainliest
    14·1 answer
  • Question 1 of 10
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!