A Number Systems Calculator

I have assigned a single-day project for my Computer Science students to create a number systems calculator to wrap-up our discussions on numbering systems.

In this project, students have to create a converter that first asks which number system the original number is from (Decimal, Binary, Octal, or Hexadecimal). It will then ask for the number and will output the equivalent values in the other 3 number systems with each clearly labeled.

The students are starting with the following code from Github…

//Program Name: Number Converter
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Fall 2016

import java.util.*;
import java.lang.*;
import java.io.*;

public class converter{
    public static void main (String[] args) throws java.lang.Exception{
        Scanner menuChoice = new Scanner(System.in);
        System.out.println("Number Converter");
        System.out.println("");
        System.out.println("Which number system are you converting from?");
        System.out.println("1 - Decimal - Base 10");
        System.out.println("2 - Binary - Base 2");
        System.out.println("3 - Octal - Base 8");
        System.out.println("4 - Hexadecimal - Base 16");
        System.out.println("");
        System.out.print("Enter Your Selection_ ");
        int menu = menuChoice.nextInt();
        
        switch (menu) {
            case 1:
                {
                    Scanner myNumber = new Scanner(System.in);
                    System.out.print("Enter Your Decimal (Base 10) Number_ ");
                    int myDecimalNumber = myNumber.nextInt();
                    String myDecimalNumberBin = (Integer.toString(myDecimalNumber, 2));
                    String myDecimalNumberOct = (Integer.toString(myDecimalNumber, 8));
                    String myDecimalNumberHex = (Integer.toString(myDecimalNumber, 16));
                    System.out.println("The Decimal Number " + myDecimalNumber + " is equal to the following:");
                    System.out.println("");
                    System.out.println("Binary (Base 2) = " + myDecimalNumberBin);
                    System.out.println("Octal (Base 8) = " + myDecimalNumberOct);
                    System.out.println("Hexadecimal (Base 16) = " + myDecimalNumberHex);
                    break;
                }
            case 2:
                {
                    Scanner myNumber = new Scanner(System.in);
                    System.out.print("Enter Your Binary (Base 2) Number_ ");
                    String myBinaryNum = myNumber.nextLine();
                    int myBinaryNumber = (Integer.parseInt(myBinaryNum,2));
                    String myBinaryNumberDec = (Integer.toString(myBinaryNumber, 10));
                    String myBinaryNumberOct = (Integer.toString(myBinaryNumber, 8));
                    String myBinaryNumberHex = (Integer.toString(myBinaryNumber, 16));
                    System.out.println("The Binary Number " + myBinaryNumber + " is equal to the following:");
                    System.out.println("");
                    System.out.println("Decimal (Base 10) = " + myBinaryNumberDec);
                    System.out.println("Octal (Base 8) = " + myBinaryNumberOct);
                    System.out.println("Hexadecimal (Base 16) = " + myBinaryNumberHex);
                    break;
                }
            case 3:
                {
                    //Code for Menu Option 3
                    break;
                }
            case 4:
                {
                    //Code for Menu Option 4
                    break;
                }
            default:
                System.out.println("Invalid Selection");
                break;
        }
    }
}

With the given information, they should be able to easily code cases 3 and 4 of the problem.

Students are asked to test their solutions using the following test case:

Base 10 Base 2 Base 8 Base 16
42 101010 52 2A

As you can see, the test case all comes down to a matter of “Life, the Universe and Everything!”.

Here is a possible final solution:

//Program Name: Number Converter
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Fall 2016

import java.util.*;
import java.lang.*;
import java.io.*;

public class converter{
    public static void main (String[] args) throws java.lang.Exception{
        Scanner menuChoice = new Scanner(System.in);
        System.out.println("Number Converter");
        System.out.println("");
        System.out.println("Which number system are you converting from?");
        System.out.println("1 - Decimal - Base 10");
        System.out.println("2 - Binary - Base 2");
        System.out.println("3 - Octal - Base 8");
        System.out.println("4 - Hexadecimal - Base 16");
        System.out.println("");
        System.out.print("Enter Your Selection_ ");
        int menu = menuChoice.nextInt();
        
        switch (menu) {
            case 1:
                {
                    Scanner myNumber = new Scanner(System.in);
                    System.out.print("Enter Your Decimal (Base 10) Number_ ");
                    int myDecimalNumber = myNumber.nextInt();
                    String myDecimalNumberBin = (Integer.toString(myDecimalNumber, 2));
                    String myDecimalNumberOct = (Integer.toString(myDecimalNumber, 8));
                    String myDecimalNumberHex = (Integer.toString(myDecimalNumber, 16));
                    System.out.println("The Decimal Number " + myDecimalNumber + " is equal to the following:");
                    System.out.println("");
                    System.out.println("Binary (Base 2) = " + myDecimalNumberBin);
                    System.out.println("Octal (Base 8) = " + myDecimalNumberOct);
                    System.out.println("Hexadecimal (Base 16) = " + myDecimalNumberHex);
                    break;
                }
            case 2:
                {
                    Scanner myNumber = new Scanner(System.in);
                    System.out.print("Enter Your Binary (Base 2) Number_ ");
                    String myBinaryNum = myNumber.nextLine();
                    int myBinaryNumber = (Integer.parseInt(myBinaryNum,2));
                    String myBinaryNumberDec = (Integer.toString(myBinaryNumber, 10));
                    String myBinaryNumberOct = (Integer.toString(myBinaryNumber, 8));
                    String myBinaryNumberHex = (Integer.toString(myBinaryNumber, 16));
                    System.out.println("The Binary Number " + myBinaryNumber + " is equal to the following:");
                    System.out.println("");
                    System.out.println("Decimal (Base 10) = " + myBinaryNumberDec);
                    System.out.println("Octal (Base 8) = " + myBinaryNumberOct);
                    System.out.println("Hexadecimal (Base 16) = " + myBinaryNumberHex);
                    break;
                }
            case 3:
                {
                    Scanner myNumber = new Scanner(System.in);
                    System.out.print("Enter Your Octal (Base 8) Number_ ");
                    String myOctalNum = myNumber.nextLine();
                    int myOctalNumber = (Integer.parseInt(myOctalNum,8));
                    String myOctalNumberDec = (Integer.toString(myOctalNumber, 10));
                    String myOctalNumberBin = (Integer.toString(myOctalNumber, 2));
                    String myOctalNumberHex = (Integer.toString(myOctalNumber, 16));
                    System.out.println("The Octal Number " + myOctalNumber + " is equal to the following:");
                    System.out.println("");
                    System.out.println("Decimal (Base 10) = " + myOctalNumberDec);
                    System.out.println("Binary (Base 2) = " + myOctalNumberBin);
                    System.out.println("Hexadecimal (Base 16) = " + myOctalNumberHex);
                    break;
                }
            case 4:
                {
                    Scanner myNumber = new Scanner(System.in);
                    System.out.print("Enter Your Hexadecimal (Base 16) Number_ ");
                    String myHexNum = myNumber.nextLine();
                    int myHexNumber = (Integer.parseInt(myHexNum,16));
                    String myHexNumberDec = (Integer.toString(myHexNumber, 10));
                    String myHexNumberBin = (Integer.toString(myHexNumber, 2));
                    String myHexNumberOct = (Integer.toString(myHexNumber, 8));
                    System.out.println("The Hexadecimal Number " + myHexNumber + " is equal to the following:");
                    System.out.println("");
                    System.out.println("Decimal (Base 10) = " + myHexNumberDec);
                    System.out.println("Binary (Base 2) = " + myHexNumberBin);
                    System.out.println("Octal (Base 8) = " + myHexNumberOct);
                    break;
                }
            default:
                System.out.println("Invalid Selection");
                break;
        }
    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *