Question

*In JAVA please* Tasks This lab has two parts: Write a recursive method that converts a...

*In JAVA please*

Tasks

This lab has two parts:

  1. Write a recursive method that converts a decimal number to a different base number system.
  2. Print out the results after testing your method on a few different inputs.

Task 1 – Recursive Method

Create a recursive method that returns a given number converted from base ten to a given other base number system ranging from two to thirty-six. A decimal number, or base ten number, can be expressed in any other number system. The only difference is that different systems use different values for each digit. For instance, binary or base two, only has two possible digit values, zero (0) or one (1). Octal, base eight, has digits ranging from zero (0) to seven (7), and hexadecimal, base sixteen, has digits ranging from zero (0) to fifteen (which is shown as F). Notice that two, eight, ten, and sixteen are not part of their respective base number systems. Instead, those numbers are represented as (10) in each of their systems. So for base two 10 is two, for octal 10 is eight, for decimal 10 is ten, and for hexadecimal 10 is sixteen.

NOTE: Any number system greater than decimal uses capital letters as well as numbers. So hexadecimal has A which is ten, B which is eleven, C which is twelve, D which is thirteen, E which is fourteen, and F which is fifteen. This trend continues for number systems above hexadecimal as well.

HINT: The process to convert from decimal to any other base number system uses both division and modulus at each step, both of which will be done using the other base number. Also, the default calculator app on Windows is able to show the converted numbers for base two, eight, and sixteen from base ten, in case you’d like to check your output.

Task 2 – The Driver

            Now we just need to call our method from our Main method and test it with a few different inputs (they may be hardcoded or from user input). Print out the results of each of the method calls along with the tested input decimal number and which base number system it was converted to.

∃ Some Sample Output:

753 in decimal is 1361 in base 8.

753 in decimal is 2F1 in base 16.

9098 in decimal is 12EI in base 20.

692 in decimal is 1010110100 in base 2.

Tasks

This lab has two parts:

  1. Write a recursive method that converts a decimal number to a different base number system.
  2. Print out the results after testing your method on a few different inputs.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java program

public class Conversion {
   public static char getSymbol(int val) {
       if(val < 9)return (char) ('0' + val);
       else return (char) ('A' + (val-10));
   }
   static String result = "";
   public static String decimalToOther(int num , int base )
   {   
       if(num != 0)
    {
           int rem=num % base;
           result = getSymbol(rem) + result;
   num/=base;
   decimalToOther(num , base);
   }
    return result;
   }
  
   public static void main(String args[]) {
       System.out.println(decimalToOther(753 , 8));
       result = "";
       System.out.println(decimalToOther(753 , 16));
       result = "";
       System.out.println(decimalToOther(9098 , 20));
       result = "";
       System.out.println(decimalToOther(692 , 2));
       result = "";
       System.out.println(decimalToOther(692 , 25));
   }
}

Add a comment
Know the answer?
Add Answer to:
*In JAVA please* Tasks This lab has two parts: Write a recursive method that converts a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java, write a recursive method that converts an integer to hexadecimal. The function should print...

    In Java, write a recursive method that converts an integer to hexadecimal. The function should print out the hexadecimal character instead of returning the character. Write a test program that prompts the user to enter an integer and displays its hexadecimal equivalent.

  • JAVA PLEASE 4. Write a recursive program that converts a Hex base value to its decimal...

    JAVA PLEASE 4. Write a recursive program that converts a Hex base value to its decimal equivalent. This program must validate the ueser’s input. You program must have the main method, inputValidate method and hexToDecimal method. Must implement the last method recursively. Here is a sample output. Enter a valid hex number: 10 Your number in decimal is: 16 Do you have another number: yes Enter a valid hex number: 123 Your number in decimal is: 291 Do you have...

  • Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out...

    Write a program that reads in two hexadecimal numbers from a file, hex.dat, and prints out the sum of the two numbers in hexadecimal. (As noted in class, first do this without using a file and by reading using the cin > > command) From Wikipedia: "In mathematics and computer science, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0-9 to...

  • Using Java IDE Write a recursive method which takes an integer number and returns the sum...

    Using Java IDE Write a recursive method which takes an integer number and returns the sum of the numbers from 1 to that number. The method must solve the problem recursively.Then write an application which calls the method with a few different numbers and displays the return value of the method.

  • Step 1: design and code a program that uses a recursive method to convert a String...

    Step 1: design and code a program that uses a recursive method to convert a String of digit characters into an integer variable. For example, convert the character string “13531” to an integer with the value 13,531. Note that the comma is present only to distinguish the integer value from the character string (in quotes) . The program should be repetitive to allow the user to enter any number of number strings, convert them, display them, and sum them. When...

  • Question 1.1. (TCO 1) Which number system has a radix of two? (Points : 4) Hexadecimal...

    Question 1.1. (TCO 1) Which number system has a radix of two? (Points : 4) Hexadecimal Binary Decimal Octal Question 2.2. (TCO 1) Convert 24 base 10 to hexadecimal. (Points : 4) 1A 18 20 30 Question 3.3. (TCO 1) If FF h is converted to decimal, the result is _____. (Points : 4) 100 200 255 256 Question 4.4. (TCO 1) Convert decimal 103 to an 8-bit binary number. (Points : 4) 1110 0100 0100 0000 0110 0111 0110...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • RecursiveExponent.java Write a recursive function that accepts two arguments into parameters x and y, and which...

    RecursiveExponent.java Write a recursive function that accepts two arguments into parameters x and y, and which returns the value of x raised to the power y. Hint: exponentiation is equivalent to performing repetitions of a multiplication. For example, the base 4 raised to the 6th power = 4*4*4 * 4 * 4 * 4 = 4,096. The only file that you need to create is RecursiveExponent.java. Your main method should prompt the user to supply the base and exponent values...

  • Write a java recursive method digitMatch that takes two nonnegative integers as parameters and that returns...

    Write a java recursive method digitMatch that takes two nonnegative integers as parameters and that returns the number of digits that match between them. Two digits match if they are equal and have the same relative position starting from the end of the number (i.e., starting with the ones digit). In other words, the method should compare the last digits of each number, the second-to-last digits of each number, the third-to-last digits of each number, and so forth, counting how...

  • Create a new Java Application that has the following methods: A method that generate a binary...

    Create a new Java Application that has the following methods: A method that generate a binary search tree (BST) where the number of nodes and the range of the values are from a file. A recursive method to print the BST in preorder traversal A recursive method to print the BST in post-order traversal A recursive method to print the BST in in-order traversal A recursive method to count the number of all nodes in BST A method to find...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT