SlideShare a Scribd company logo
1 of 2
Click here to get Solution on ProgAnswer.com
Converting a Number from Binary to Decimal, hexadecimal and octal:
Topics Covered: reading keyboard input, if statements, nested loops, and string manipulations
Your program will take as input a series of "binary digits" as a string and then will call methods to
convert and display the binary string converted into decimal, hexadecimal and octal.
Your main method is responsible for prompting the user for the binary number. It should then call a
method "isValid" to test if the string input consists of only zeros and ones.
If the string is valid your main program should call a method "printDecimal" that prints the value of the
binary number in decimal. It will then call methods "printHex" and "printOctal" to print out the
hexadecimal and octal representations of the string as well. If the string input was NOT a valid binary
number then the main program should print an error message.
Method: printDecimal
The specification for this method should be
public static void printDecimal(String binaryNum)
It is responsible for using loops to convert the binary number into decimal and printing out the result.
To convert a number from base 2 to base 10, we first find the weight of each bit in the binary number.
The weight of each bit in the binary number is assigned from right to left. The weight of the rightmost
bit is 0. The weight of the bit immediately to the left of the rightmost bit is 1, the weight of the next bit
to the left is 2 and so on. Consider the binary number 1001101. The weight of each bit is as follows:
Weight6 5 4 3 2 1 0
1 0 0 1 1 0 1
We use the weight of each bit to find the equivalent decimal number. For each bit, we multiply the bit
by 2 to the power of its weight and then add all of the numbers. For the above binary number, the
equivalent decimal number is:
= 1 * 26 + 0 * 25 + 0 * 24 + 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20
= 64 + 0 + 0 + 8 + 4 + 0 + 1
= 77
Remember: to convert a binary number to an equivalent decimal number:
1. The weight of each bit in the binary number must be determined.
2. The weight is assigned from right to left.
Method :printHex
The specification for this method is:
public static void printHex(String binaryNum)
To convert a number from binary into Hexadecimal you group together sets of 4 bits from right to left,
padding on the left with zeros if needed. (see lecture 1 for details). This method will need to:
If the length of the binary string is not evenly divisible by 4, then concatenate zeros on the left.
Take groups of 4 bits (4 characters from the string) and convert the 4 binary digits in decimal and print
out the correct hexadecimal digit remembering the values 10-15 are A-F
Method :printOctal
The specification for this method is:
public static void printOctal(String binaryNum)
To convert a number from binary into Octal you group together sets of 3 bits from right to left, padding
on the left with zeros if needed. (see lecture 1 for details). This method will need to:
If the length of the binary string is not evenly divisible by 3, then concatenate zeros on the left.
Take groups of 3 bits (3 characters from the string) and convert the 3 binary digits in decimal and print
out the correct decimal value of the 3 digits. NOTE only the values 0-7 should be printed for each group
of 3 bits.
Tasks:
1. NOTE: your main program MUST allow the user to input more than one binary string.
* You will begin by asking the user if they want to enter a number or quit. IF they select "quit" then no
further processing should happen and your program should quit. If they input a number your program
should do the following with each iteration:
a. Your program must prompt the user for a binary # as a sequence of 1's and 0's.
b. Your program will read the user's input as a String and then must VALIDATE that the string
contains ONLY 1's and 0's. If it contains any other character an error should be displayed and execution
should stop, and you should ask the user if they want to enter another number or quit.
c. If the string is valid, main should display the number input and then call printDecimal, printHex
and printOctal.
2. Your program should ask the user if they want to enter another binary number or quit.

More Related Content

What's hot

Approximation and error
Approximation and errorApproximation and error
Approximation and errorrubenarismendi
 
Variable, constant, operators and control statement
Variable, constant, operators and control statementVariable, constant, operators and control statement
Variable, constant, operators and control statementEyelean xilef
 
Solving Equations And Inequalities For 6th
Solving Equations And Inequalities For 6thSolving Equations And Inequalities For 6th
Solving Equations And Inequalities For 6thLindsey Brown
 
Operators , Functions and Options in VB.NET
Operators , Functions and Options in VB.NETOperators , Functions and Options in VB.NET
Operators , Functions and Options in VB.NETShyam Sir
 
Class 6 computer part 3
Class 6 computer part 3Class 6 computer part 3
Class 6 computer part 3Jeevan Subedi
 
Data types and operators
Data types and operatorsData types and operators
Data types and operatorsamukthamalya
 
G6 m3-a-lesson 6-t
G6 m3-a-lesson 6-tG6 m3-a-lesson 6-t
G6 m3-a-lesson 6-tmlabuski
 
G6 m3-a-lesson 6-s
G6 m3-a-lesson 6-sG6 m3-a-lesson 6-s
G6 m3-a-lesson 6-smlabuski
 
Excel text functions
Excel text functionsExcel text functions
Excel text functionsKevin McLogan
 
Fractions keynote for explaining
Fractions keynote for explainingFractions keynote for explaining
Fractions keynote for explaininggrade5a
 
Libop Calculator for Android Features
Libop Calculator for Android FeaturesLibop Calculator for Android Features
Libop Calculator for Android FeaturesWoodgates E.I.R.L.
 
Advanced formula
Advanced formulaAdvanced formula
Advanced formulaMaymay027
 
Graphing sytems inequalities
Graphing sytems inequalitiesGraphing sytems inequalities
Graphing sytems inequalitiesJessica Garcia
 
Multiplication lesson
Multiplication lessonMultiplication lesson
Multiplication lessoncarovego
 
1.6 Equations & Inequalities Absolute Value
1.6 Equations & Inequalities   Absolute Value1.6 Equations & Inequalities   Absolute Value
1.6 Equations & Inequalities Absolute Valueguest620260
 
Etech. mitch. [autosaved]
Etech. mitch. [autosaved]Etech. mitch. [autosaved]
Etech. mitch. [autosaved]MaridelBajeta
 

What's hot (20)

Approximation and error
Approximation and errorApproximation and error
Approximation and error
 
Variable, constant, operators and control statement
Variable, constant, operators and control statementVariable, constant, operators and control statement
Variable, constant, operators and control statement
 
Solving Equations And Inequalities For 6th
Solving Equations And Inequalities For 6thSolving Equations And Inequalities For 6th
Solving Equations And Inequalities For 6th
 
Operators , Functions and Options in VB.NET
Operators , Functions and Options in VB.NETOperators , Functions and Options in VB.NET
Operators , Functions and Options in VB.NET
 
Class 6 computer part 3
Class 6 computer part 3Class 6 computer part 3
Class 6 computer part 3
 
Data types and operators
Data types and operatorsData types and operators
Data types and operators
 
Data Transformation
Data TransformationData Transformation
Data Transformation
 
G6 m3-a-lesson 6-t
G6 m3-a-lesson 6-tG6 m3-a-lesson 6-t
G6 m3-a-lesson 6-t
 
G6 m3-a-lesson 6-s
G6 m3-a-lesson 6-sG6 m3-a-lesson 6-s
G6 m3-a-lesson 6-s
 
Arrays in c++
Arrays in c++Arrays in c++
Arrays in c++
 
College algebra -REAL Numbers
College algebra -REAL NumbersCollege algebra -REAL Numbers
College algebra -REAL Numbers
 
Excel text functions
Excel text functionsExcel text functions
Excel text functions
 
Fractions keynote for explaining
Fractions keynote for explainingFractions keynote for explaining
Fractions keynote for explaining
 
Libop Calculator for Android Features
Libop Calculator for Android FeaturesLibop Calculator for Android Features
Libop Calculator for Android Features
 
Advanced formula
Advanced formulaAdvanced formula
Advanced formula
 
Graphing sytems inequalities
Graphing sytems inequalitiesGraphing sytems inequalities
Graphing sytems inequalities
 
Multiplication lesson
Multiplication lessonMultiplication lesson
Multiplication lesson
 
Proportion
ProportionProportion
Proportion
 
1.6 Equations & Inequalities Absolute Value
1.6 Equations & Inequalities   Absolute Value1.6 Equations & Inequalities   Absolute Value
1.6 Equations & Inequalities Absolute Value
 
Etech. mitch. [autosaved]
Etech. mitch. [autosaved]Etech. mitch. [autosaved]
Etech. mitch. [autosaved]
 

Similar to 1122230 question

Lesson 13. Pattern 5. Address arithmetic
Lesson 13. Pattern 5. Address arithmeticLesson 13. Pattern 5. Address arithmetic
Lesson 13. Pattern 5. Address arithmeticPVS-Studio
 
Manoch1raw 160512091436
Manoch1raw 160512091436Manoch1raw 160512091436
Manoch1raw 160512091436marangburu42
 
Data representation
Data representationData representation
Data representationManish Kumar
 
Number Systems.ppt
Number Systems.pptNumber Systems.ppt
Number Systems.pptzorogoh2
 
chapter one && two.pdf
chapter one && two.pdfchapter one && two.pdf
chapter one && two.pdfmiftah88
 
Lec 02 data representation part 1
Lec 02 data representation part 1Lec 02 data representation part 1
Lec 02 data representation part 1Abdul Khan
 
Topic 1 Data Representation
Topic 1 Data RepresentationTopic 1 Data Representation
Topic 1 Data Representationekul
 
Topic 1 Data Representation
Topic 1 Data RepresentationTopic 1 Data Representation
Topic 1 Data RepresentationKyle
 
Lab7: More Arrays, Strings, Vectors, and Pointers
Lab7: More Arrays, Strings, Vectors, and PointersLab7: More Arrays, Strings, Vectors, and Pointers
Lab7: More Arrays, Strings, Vectors, and Pointersenidcruz
 
Lecture 02 - Logic Design(Number Systems).pptx
Lecture 02 - Logic Design(Number Systems).pptxLecture 02 - Logic Design(Number Systems).pptx
Lecture 02 - Logic Design(Number Systems).pptxshwan it
 
IS 139 Lecture 4 - 2015
IS 139 Lecture 4 - 2015IS 139 Lecture 4 - 2015
IS 139 Lecture 4 - 2015Aron Kondoro
 
IS 139 Lecture 4
IS 139 Lecture 4IS 139 Lecture 4
IS 139 Lecture 4wajanga
 
Lab6: I/O and Arrays
Lab6: I/O and ArraysLab6: I/O and Arrays
Lab6: I/O and Arraysenidcruz
 
Oop lab assignment 01
Oop lab assignment 01Oop lab assignment 01
Oop lab assignment 01Drjilesh
 
data representation
 data representation data representation
data representationHaroon_007
 
Data repersentation.
Data repersentation.Data repersentation.
Data repersentation.Ritesh Saini
 
Week 4-Number Systems.pptx
Week 4-Number Systems.pptxWeek 4-Number Systems.pptx
Week 4-Number Systems.pptxHamnaKhalid25
 
04 chapter03 02_numbers_systems_student_version_fa16
04 chapter03 02_numbers_systems_student_version_fa1604 chapter03 02_numbers_systems_student_version_fa16
04 chapter03 02_numbers_systems_student_version_fa16John Todora
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAhmad Bashar Eter
 

Similar to 1122230 question (20)

Lesson 13. Pattern 5. Address arithmetic
Lesson 13. Pattern 5. Address arithmeticLesson 13. Pattern 5. Address arithmetic
Lesson 13. Pattern 5. Address arithmetic
 
Manoch1raw 160512091436
Manoch1raw 160512091436Manoch1raw 160512091436
Manoch1raw 160512091436
 
Data representation
Data representationData representation
Data representation
 
Number Systems.ppt
Number Systems.pptNumber Systems.ppt
Number Systems.ppt
 
C code examples
C code examplesC code examples
C code examples
 
chapter one && two.pdf
chapter one && two.pdfchapter one && two.pdf
chapter one && two.pdf
 
Lec 02 data representation part 1
Lec 02 data representation part 1Lec 02 data representation part 1
Lec 02 data representation part 1
 
Topic 1 Data Representation
Topic 1 Data RepresentationTopic 1 Data Representation
Topic 1 Data Representation
 
Topic 1 Data Representation
Topic 1 Data RepresentationTopic 1 Data Representation
Topic 1 Data Representation
 
Lab7: More Arrays, Strings, Vectors, and Pointers
Lab7: More Arrays, Strings, Vectors, and PointersLab7: More Arrays, Strings, Vectors, and Pointers
Lab7: More Arrays, Strings, Vectors, and Pointers
 
Lecture 02 - Logic Design(Number Systems).pptx
Lecture 02 - Logic Design(Number Systems).pptxLecture 02 - Logic Design(Number Systems).pptx
Lecture 02 - Logic Design(Number Systems).pptx
 
IS 139 Lecture 4 - 2015
IS 139 Lecture 4 - 2015IS 139 Lecture 4 - 2015
IS 139 Lecture 4 - 2015
 
IS 139 Lecture 4
IS 139 Lecture 4IS 139 Lecture 4
IS 139 Lecture 4
 
Lab6: I/O and Arrays
Lab6: I/O and ArraysLab6: I/O and Arrays
Lab6: I/O and Arrays
 
Oop lab assignment 01
Oop lab assignment 01Oop lab assignment 01
Oop lab assignment 01
 
data representation
 data representation data representation
data representation
 
Data repersentation.
Data repersentation.Data repersentation.
Data repersentation.
 
Week 4-Number Systems.pptx
Week 4-Number Systems.pptxWeek 4-Number Systems.pptx
Week 4-Number Systems.pptx
 
04 chapter03 02_numbers_systems_student_version_fa16
04 chapter03 02_numbers_systems_student_version_fa1604 chapter03 02_numbers_systems_student_version_fa16
04 chapter03 02_numbers_systems_student_version_fa16
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth session
 

1122230 question

  • 1. Click here to get Solution on ProgAnswer.com Converting a Number from Binary to Decimal, hexadecimal and octal: Topics Covered: reading keyboard input, if statements, nested loops, and string manipulations Your program will take as input a series of "binary digits" as a string and then will call methods to convert and display the binary string converted into decimal, hexadecimal and octal. Your main method is responsible for prompting the user for the binary number. It should then call a method "isValid" to test if the string input consists of only zeros and ones. If the string is valid your main program should call a method "printDecimal" that prints the value of the binary number in decimal. It will then call methods "printHex" and "printOctal" to print out the hexadecimal and octal representations of the string as well. If the string input was NOT a valid binary number then the main program should print an error message. Method: printDecimal The specification for this method should be public static void printDecimal(String binaryNum) It is responsible for using loops to convert the binary number into decimal and printing out the result. To convert a number from base 2 to base 10, we first find the weight of each bit in the binary number. The weight of each bit in the binary number is assigned from right to left. The weight of the rightmost bit is 0. The weight of the bit immediately to the left of the rightmost bit is 1, the weight of the next bit to the left is 2 and so on. Consider the binary number 1001101. The weight of each bit is as follows: Weight6 5 4 3 2 1 0 1 0 0 1 1 0 1 We use the weight of each bit to find the equivalent decimal number. For each bit, we multiply the bit by 2 to the power of its weight and then add all of the numbers. For the above binary number, the equivalent decimal number is: = 1 * 26 + 0 * 25 + 0 * 24 + 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20 = 64 + 0 + 0 + 8 + 4 + 0 + 1 = 77 Remember: to convert a binary number to an equivalent decimal number: 1. The weight of each bit in the binary number must be determined. 2. The weight is assigned from right to left. Method :printHex The specification for this method is: public static void printHex(String binaryNum) To convert a number from binary into Hexadecimal you group together sets of 4 bits from right to left, padding on the left with zeros if needed. (see lecture 1 for details). This method will need to: If the length of the binary string is not evenly divisible by 4, then concatenate zeros on the left.
  • 2. Take groups of 4 bits (4 characters from the string) and convert the 4 binary digits in decimal and print out the correct hexadecimal digit remembering the values 10-15 are A-F Method :printOctal The specification for this method is: public static void printOctal(String binaryNum) To convert a number from binary into Octal you group together sets of 3 bits from right to left, padding on the left with zeros if needed. (see lecture 1 for details). This method will need to: If the length of the binary string is not evenly divisible by 3, then concatenate zeros on the left. Take groups of 3 bits (3 characters from the string) and convert the 3 binary digits in decimal and print out the correct decimal value of the 3 digits. NOTE only the values 0-7 should be printed for each group of 3 bits. Tasks: 1. NOTE: your main program MUST allow the user to input more than one binary string. * You will begin by asking the user if they want to enter a number or quit. IF they select "quit" then no further processing should happen and your program should quit. If they input a number your program should do the following with each iteration: a. Your program must prompt the user for a binary # as a sequence of 1's and 0's. b. Your program will read the user's input as a String and then must VALIDATE that the string contains ONLY 1's and 0's. If it contains any other character an error should be displayed and execution should stop, and you should ask the user if they want to enter another number or quit. c. If the string is valid, main should display the number input and then call printDecimal, printHex and printOctal. 2. Your program should ask the user if they want to enter another binary number or quit.