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
Pick a web browser (a leading one or perhaps a lesser-known one that you use) and examine the Privacy and Security features and
Veseljchak [2.6K]

Solution :

<u>Chrome web browser</u>

The Chrome web browser uses a range of privacy and security settings for its customers. They include several security indicators as well as malware protection. Chrome uses the sandboxing technology, which prevents the harmful viruses and Trojans from reaching the computers.

Chrome provides a safe browsing by giving us an alert whenever we try to browse some harmful web sites.  It also warns you if we use a username and password combination which has been compromised in any data leak.

Chrome also serves to protect the individuals :

It provides a features of Ad blocking.

When we want to browse safely without being recognized or without storing any credentials, Chrome provides an Incognito mode.

Many businesses can be done on the internet using Chrome platform that is safe and authenticate to gather and collect data and information.

3 0
3 years ago
What are the oop concept of java
mash [69]

Answer: OOP concepts in Java are the main ideas behind Java’s Object Oriented Programming. They are abstraction, encapsulation, inheritance, and polymorphism. Grasping them is key to understanding how Java works. Basically, Java OOP concepts let us create working methods and variables, then re-use all or part of them without compromising security.

HOPE THIS HELPED IS NOT SORRY.

4 0
3 years ago
Read 2 more answers
Which of the following is the correct style tag for having the web page's text be aligned to the right?
uysha [10]

Answer:

B. style=text-align: right

Explanation:

text-align is a CSS property having 5 types of values. Each of values define below.

left - Aligns content to left.

right - Aligns content to right.

center - Centers the content.

justify - Depends on the width of the content, it will align itself to both left and righ

inherit - It specifies the the value of text-align should be taken from parent element.

8 0
3 years ago
Read 2 more answers
Which group on the Home Ribbon allows you to add shapes to a PowerPoint slide?
skad [1K]
It is under the drawings group.  
8 0
3 years ago
Read 2 more answers
In Excel you insert a comment in a worksheet by using the ____ tab on the Ribbon.
White raven [17]
Review
(I was suprised too)
3 0
4 years ago
Other questions:
  • What will be the biased exponent of 1,100.1? <br> A:130<br> B:127<br> C:-127<br> D:2^3
    12·2 answers
  • In python,_______ are used to define the conditions necessary for a while loop to run.
    7·2 answers
  • Which of these is most closely associated with system control? (1 point) (Points : 1.5) boundary
    10·1 answer
  • What language must be used to display a bare-minimum webpage?
    8·2 answers
  • Computers help eliminate that repetitive of manual task. How can this benefit you in in your overall career
    7·2 answers
  • Which of the following does not represent the function of a Variable? Variables store data in memory; this data has limited use
    8·1 answer
  • If a filesystem has a block size of 4096 bytes, this means that a file comprised of only one byte will still use 4096 bytes of s
    12·1 answer
  • Page Setup options are important for printing a PowerPoint presentation a certain way. The button for the Page Setup dialog box
    15·1 answer
  • The use of a concept or product from one technology to solve a problem in an unrelated one
    11·1 answer
  • Define a function FindLargestNum() with no parameters that reads integers from input until a negative integer is read. The funct
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!