Question

In: Computer Science

Your program should be in a new project named "Program 3: Number System Conversion" and should...

Your program should be in a new project named "Program 3: Number System Conversion" and should be in a package named "numberSystem". Your program must be in a file called "Program3NumberConversion.java". Your goal is to convert a number in some other number system (such as binary) to decimal. The base (also known as a radix) will have a maximum of 16. Before beginning your main method, write two other methods: public static int parseNumber (char letter) will basically be a big switch statement. There should be a case for each one of the digits (0-9) as well as a, A, b, B, c, C, d, D, e, E, f ,and F. Each of these should just return their numeric value: 0 is 0, 1 is 1, 2 is 2, ... a and A is 10, b and B is 11, etc. Note that none of these cases need a break statement since they are returning values. The heart of the matter is handled in public static int valueOfDigit(int place, int radix, int number). Remember that Math.pow returns a double, so the result will have to be cast to an integer before it is returned. The math is pretty simple, just consider the following example from the number 43205 . Remember to start with an index of 0 and to start from the right hand side of the number. If the 2nd place of the number in base 5 has value 3, it has a result of 3*(52) or 75. After you have computed the formula, perform the following output before returning the value for error checking: System.out.print(result+"+"); Your main method needs to follow this algorithm: Make a new Scanner named scanner bound to the keyboard Make a new String named response and set it equal to an empty String Do the following while response equals "y" (use equalsIgnoreCase) initialize an integer named sum and set it to 0 Prompt for a radix (or base) up to 16 and read it in Prompt for a number named inputNumber and read it in as a String (use scanner.next()) Start a for that run backwards through to characters of inputNumber. Do the following inside the for: Get the current character based on i Get the currentDigit based on the parseNumber of the result of the previous step Accumulate in sum the value of the digit. Make sure to compensate for the fact that your loop is running backwards. Now, just output " " + inputNumber+" in base "+radix+" converts to "+sum+" in base 10." Prompt the use to enter 'y' to enter another number to convert and read in their response close the scanner Here are some test cases for you to try: 1011 base 2 = 1+2+8 = 11

A54 base 16 = 4+80+2560 = 2644

5310 base 6 = 0+6+108+1080 = 1194

310 base 4 = 0+4+48 = 52

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

import java.util.*;

class Program3NumberConversion {

  public static int parseNumber(char x) {
    switch (x) {
      case '0':
        return 0;
      case '1':
        return 1;
      case '2':
        return 2;
      case '3':
        return 3;
      case '4':
        return 4;
      case '5':
        return 5;
      case '6':
        return 6;
      case '7':
        return 7;
      case '8':
        return 8;
      case '9':
        return 9;
      case 'a':
      case 'A':
        return 10;
      case 'b':
      case 'B':
        return 11;
      case 'c':
      case 'C':
        return 12;
      case 'd':
      case 'D':
        return 13;
      case 'e':
      case 'E':
        return 14;
      case 'f':
      case 'F':
        return 15;
    }
    //default return
    return -1;
  }

  public static int valueOfDigit(int place, int radix, int number) {
    int temp = (int) Math.pow(radix, place);
    int result = temp * number;
    System.out.print("\nfor " + (place + 1) + "th place result is : " + result);
    return result;
  }

  public static void main(String[] args) {
    String number;
    int radix;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter no and radix(max 16) of that number : ");
    number = sc.next();
    radix = sc.nextInt();
    int j = 0;
    //no in decimal
    int finalNumber = 0;
    /*for each character conversion
and add result to finalNumber */
    for (int i = number.length() - 1; i >= 0; i--) {
      finalNumber += valueOfDigit(j, radix, parseNumber(number.charAt(i)));
      j++;
    }
    System.out.println(
      "\nNumber in base " +
      radix +
      " is : " +
      number +
      " & number in decimal : " +
      finalNumber
    );
  }
}

Related Solutions

Write a program named MakeChange that calculates and displays the conversion of an entered number of...
Write a program named MakeChange that calculates and displays the conversion of an entered number of dollars into currency denominations—twenties, tens, fives, and ones. For example, if 113 dollars is entered, the output would be twenties: 5 tens: 1 fives: 0 ones: 3. Answer in C# (P.S i'm posting the incomplete code that i have so far below.) using System; using static System.Console; class MakeChange { static void Main() { int twenties, tens, fives, ones; WriteLine("Enter the number of dollars:");...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
2. Create a new project named named lab5_2. You will prompt the user for an amount...
2. Create a new project named named lab5_2. You will prompt the user for an amount of names to enter, and then ask for that amount of names. You’ll store these names in a vector of strings. Then you’ll sort the vector. Finally, you’ll print the results. How many names?: 4 Enter a name: Leonardo Enter a name: Donatello Enter a name: Michelangelo Enter a name: Raphael ==================== Alphabetized ==================== Donatello                  Leonardo Michelangelo                      Raphael
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should...
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should be only 3) in it and prints out the number of photos in the gallery.
Conversion of numeral to roman number: Create a FLOWCHART and C++ PROGRAM that prompts the user...
Conversion of numeral to roman number: Create a FLOWCHART and C++ PROGRAM that prompts the user to enter a number (from 1-3000 only) then output the number and its corresponding roman numeral number. ***validate the input (it must be 1-3000 only) SAMPLE OUTPUT: Enter a Number: 0 Number should be from 1-3000 only (Terminated) Enter a Number: -5 Number should be from 1-3000 only (Terminated) Enter a Number: 3500 Number should be from 1-3000 only (Terminated) Enter a Number: 1994...
Invent a recursive function and save the program in a file named " q3c.java” It should...
Invent a recursive function and save the program in a file named " q3c.java” It should not be any of the familiar ones (like fibonacci or binomial coefficients or any of those). Make one up. Make sure it is well-defined (no ambiguity), make sure it isn’t “infinitely recursive”. • Implement it in a Java program and demonstrate it with at least 7 test values. • Add necessary comments to the program to explain it. • In comments, describe your test...
Python Language: Similar to Project 3, write a program that loops a number from 1 to...
Python Language: Similar to Project 3, write a program that loops a number from 1 to 10 thousand and keeps updating a count variable according to these rules: if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above conditions match for the number, increase count by the number. Before the loop begins,...
1. The conversion ratio is:​ a. ​the number of new lower coupon rate bonds that the...
1. The conversion ratio is:​ a. ​the number of new lower coupon rate bonds that the bondholder receives for old bonds. b. ​the ratio of the face value of the bond to its market value. c. ​the number of shares of stock that the bondholder receives upon conversion. d. ​the ratio of the bond's old face value to its new face value. e. ​the number of bonds in the company's new project received upon expansion. 2. Which of the following...
1. Enhance Binary System Conversion program with Lab05.2 Addition Function Write a program that accepts two...
1. Enhance Binary System Conversion program with Lab05.2 Addition Function Write a program that accepts two positive binary number in string and perform the addition with Lab05.2 function enhance in a way can accept binary string in addition to decimal string (use input parameter to control the base2 or base10 addition) and output the as binary string. (Optional: Demonstrate 8 bits and 16 bits in length as inputs.) // the example function prototype for addition function below where accepting two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT