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.

  • 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...

  • 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...

  • PLEASE I NEED C# PROGRAM WITH DETAILS Objectives Apply recursion to a more difficult problem. Background...

    PLEASE I NEED C# PROGRAM WITH DETAILS Objectives Apply recursion to a more difficult problem. Background There are many problems that loops simplify, such as displaying every pixel to a screen or receiving repetitive input. However, some situations that can be simplified with looping are not easily solvable using loops. This includes problems that require back tracking and being able to use information from previous iterations, which would normally be lost when using an iterative loop. In those cases, it...

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

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
ADVERTISEMENT