Question

I need assistance writing this code. I keep generating errors and not sure what to do...

I need assistance writing this code. I keep generating errors and not sure what to do now.

Design and implement a method that will take any value as String (even it's a number!) and convert that value to a number in any base. Use the method;

private static ArrayList convertToBase(String value, int currentBase, int targetBase){

// your algorithm

return result; //which is a String array

}

Examples:
convertToBase("10011", 2, 10) should return [1, 9], meaning, (10011)base2 = (19)base10

convertToBase("100", 10, 8) should return [6, 4]

convertToBase("E12B0", 16, 2) should return [1,1,1,0,0,0,0,1,0,0,1,0,1,0,1,1,0,0,0,0]

convertToBase("1250", 10, 16) should return [4, E, 2]

Write a tester, make sure your code produces correct results.

Homework Answers

Answer #1
import java.util.*;
public class Demo{
    public static ArrayList convertToBase(String value, int currentBase, int targetBase){
        String str=Integer.toString(Integer.parseInt(value, currentBase), targetBase);
        ArrayList<Character> result = new ArrayList<Character>();
        for (char c : str.toCharArray()) {
            result.add(Character.toUpperCase(c));
        }
        return result;
    }
    public static void main(String[] args){
        System.out.println(convertToBase("10011",2,10));
        System.out.println(convertToBase("100",8,10));
        System.out.println(convertToBase("100",10,8));
        System.out.println(convertToBase("E12B0",16,2));
        System.out.println(convertToBase("1250",10,16));
    }
}

Note:

->the output of convertBase("100",10,8) is [1,4,4]

->the output of convertBase("100",8,10) is [6,4]

It is given wrong in the question.

But i have printed both of the them

Below is the screenshot of the code for reference purpose:

output:

Know the answer?
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
Similar Questions
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
I'm generating a random number every 10 second and Storing and Updating the number every 10...
I'm generating a random number every 10 second and Storing and Updating the number every 10 second when I generate new number in UpdateInt method. However, In my main method when I called UpdateInt method it generate every 10 second new number but it doesn't update and store in the arraylist. ***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the arraylist that has the update number*** import java.util.ArrayList; import...
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have this much public class Main { int WIDTH = 0; double HEIGHT = 0; public static void drawSmileyFaces() { System.out.println(" :-):-):-):-):-):-)"); } public static void main (String[] args) { for (int WIDTH = 0; WIDTH < 3; WIDTH++ ) { for(double HEIGHT = 0; HEIGHT < 2; HEIGHT++) { drawSmileyFaces(); } } } } I am pretty sure that alot of this is wrong...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Take a look at the file GenericMethods.java. There are three methods you must implement: ·public static...
Take a look at the file GenericMethods.java. There are three methods you must implement: ·public static <T extends Comparable<T>> int findMin(T[] arr): Iterate through the array to find the index of the smallest element in the array. If there are two elements with the smallest value, the method should return the index of the first one. The method should run in O(n). ·public static <T extends Comparable<T>> int findMinRecursive(T[] arr): Should behave just like findMin, but should be implemented recursively....
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type. Implement the following generic linear search method and write a client program to display results: (Here is the header) public static <E extends Comparable<E>> int search(E[] list, E key)...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
In Java is there a way I can put a value in a linkList = array[...
In Java is there a way I can put a value in a linkList = array[ ] string (I don't know how to word it here what I have so far with my code) * Example object 51 I want to equal Array [0]; // Project code below public String getCardRank() { String values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; return "RRR"; } // this part is in another .java file private...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT