This site requires JavaScript, please enable it in your browser!
Greenfoot back
Bob000
Bob000 wrote ...

2014/3/18

Trouble understanding the conversion of different bases to base 10.

Bob000 Bob000

2014/3/18

#
import java.util.Scanner;

public class Conversion
{
    static Scanner input = new Scanner(System.in);
    static int base = input.nextInt();            
    static int number = input.nextInt();        
    static int result;
    
    /**
     * Your main method does the following steps repeatedly until the user enters 
     * 0 for the number base:
     * 
     * Obtain the number base for the next number using Scanner
     * Call the getNumberInBase method to get a number in that base from the user.
     * Call convertToDecimal using the number and base obtained.
     * Call the printResults method, passing the number, base and result of
     * calling the convertToDecimal method.
     * 
     */
    public static void main(String[] args)
    {
        while(base!=0){
            getNumberInBase(input,base);
            convertToDecimal(number,base);
            printResults(number,base,result);
        }
    }

    /**
     * Prompts the user to enter a number in the given base. Input comes from the
     * Scanner object. The input must be passed to the isValidNumber method to
     * determine if the number is a valid number for that base. Print an error
     * message if it is not valid and then prompt the user to enter a nother number.
     * This continues until the user provides a valid number for the given base.
     * 
     * Return only a number that is valid for the given base
     */
    public static int getNumberInBase(Scanner scanner, int base)
    {
        System.out.print("Enter a number base: ");
        while(!isValidNumber(number,base)){
            System.out.println("That is an invalid number for base " + base +". Try Again");
            getNumberInBase(input,base);
        }
        return base;
    }

    /**
     * returns true if the number is a valid number for the given base. Otherwise
     * the method returns false.
     */ 
    public static boolean isValidNumber(int number, int base)
    {
        return false;
    }

    /**
     * Returns the decimal equivalent of the number in the given base.
     * 
     * Pre condition: number is a valid number for the given base
     * Post condition: returns the decimal value of the number.
     */
    public static int convertToDecimal(int number,int base)
    {
        if(isValidNumber(number,base)){

        }
        return 0;
    }

    /**
     * Prints the statement:
     * 
     * The base <base> number <number> is <result> in base 10.
     * 
     */
    public static void printResults(int number, int base, int result)
    {

    }
}
I have created a layout of how my program should be, but I am greatly confused as how to write the boolean isValidNumber(int number, int base) method and the convertToDecimal(int number,int base). I don't have much background knowledge about bases, so I don't know why these methods are even necessary. I beg for assistance!
Bob000 Bob000

2014/3/18

#
I understand the importance of the Boolean method now, but as for the converToDecimal method, that I'm unsure of. I understand also the concept of bases now.
danpost danpost

2014/3/18

#
If you know what each position in the number represents (as far as its base is concerned), then you should have no problem converting the number back to decimal.
Bob000 Bob000

2014/3/18

#
Why do you have to convert it to a decimal is where its throwing me off.
danpost danpost

2014/3/18

#
I do not know, it is your project. Maybe because ... humans think in base 10 (which is the decimal based system). I hope you do not think that 'decimal' means ###.## format, I am quite sure that you want to convert the non base 10 number to a base ten (decimal in this sense) integer (not decimal in this sense).
Bob000 Bob000

2014/3/18

#
I was thinking decimal as in 0.8484. I understand now that it's referring to base 10 decimal
davmac davmac

2014/3/18

#
This looks wrong:
public static int convertToDecimal(int number,int base)  
The thing is, you're not converting to decimal at all. Your return type is 'int' so you are converting a string representation of some number, which is already an int, into an int. An 'int' doesn't have a base, it is just a number. This method would make more sense if the 'number' parameter was a String, and the return type was also String.
davmac davmac

2014/3/19

#
sorry, editt: you are converting a string representation of some number, which is already an int, into an int it looks like you are wanting to convert an int with some base into another int with base 10 (i.e. decimal), but: an int does not have any base associated with it.
danpost danpost

2014/3/19

#
davmac wrote...
sorry, editt: you are converting a string representation of some number, which is already an int, into an int it looks like you are wanting to convert an int with some base into another int with base 10 (i.e. decimal), but: an int does not have any base associated with it.
Yes, looks like there would be a maximum limit on the initial base and that limit is 10. Well... 9 if you want to restrict the initial base to a non-base 10 base.
davmac davmac

2014/3/19

#
danpost wrote...
Yes, looks like there would be a maximum limit on the initial base and that limit is 10. Well... 9 if you want to restrict the initial base to a non-base 10 base.
Right, but that assumes you want to interpret the digits of the 'number' parameter when it is expressed in decimal in some other base. That seems odd to me; surely 'number' should in fact be a String.
You need to login to post a reply.