1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java help (how to not to use if statement)

  1. Jul 8, 2010 #1
    Java help !!! (how to not to use if statement)

    My teacher asked the user for an integer and converted it into a number in base N, where N was a value between 2 and 9, inclusive. Expand this program to allow values of N between 2 and 36, inclusive, where digits representing the numbers 10, 11, 12, …, 36 are represented as A, B, C, …, Z, respectively. Do not use an if-statement with 20+ different cases to determine what character to append to the current string. Instead, use character arithmetic to determine which character should be output.

    Red lines are those that I don't understand.
    I don't know what he is talking about.
    Sorry English is not my first language......

    Is he saying that user's input should be in alphabets from 10[A] ~ 36[Z] ?

    And This is what I have.

    import java.util.Scanner;
    public class MinsooLab5d{

    public static void main(String[] args){
    int n, b;
    String r = "";
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter a number : "); //insert a number
    n = sc.nextInt();
    int c = n;

    System.out.print("Enter a base number [2~36] : ");
    b = sc.nextInt();

    if(b>=2 && b<=36){
    while(c != 0){
    int x = c % b;
    r=x+r;
    c=c/b;
    }
    System.out.println(n + "base" + b + " in binary is " + r); //print out result
    }

    else
    {
    System.out.println("Out of base number range !!!");
    }

    }
    }
     
  2. jcsd
  3. Jul 8, 2010 #2

    Mark44

    Staff: Mentor

    Re: Java help !!! (how to not to use if statement)

    In base 2 there are two digits: 0, 1. In base 8 there are 8 digits: 0, 1, 2, ..., 7. In base 10 there are 10 digits: 0, 1, 2, ..., 9. In base 16 there are 16 digits: 0, 1, 2, ..., 9, A, B, C, D, E, F. In base 36 there are 36 digits: 0, 1, 2, ..., 9, A, B, C, D, E, F, ..., Z.

    The program should ask the user to enter an integer and the base, and should convert the the given integer to a value in the given base.

    So for example, if the base N is 16, and the integer 47 is entered, the program should display 2F. (2F = 2*16 + 15*1)

    Similarly, if the base N is 36, and the integer 47 is entered, the program should display 1B. 1B = 1*36 + 11*1

    Does that help you get started?
     
  4. Jul 8, 2010 #3
    Re: Java help !!! (how to not to use if statement)

    1. I kinda get it. but how did you get these calculations ?
    (2F = 2*16 + 15*1) and (1B = 1*36 + 11*1).

    2. How do you convert decimal to character ?

    3. How did you get (2 and F) and (1 and B) ?
     
  5. Jul 8, 2010 #4

    Mark44

    Staff: Mentor

    Re: Java help !!! (how to not to use if statement)

    In the context of what I wrote, 2F is the base-16 representation of 47 (base-10). Similarly, 1B is the base-36 representation of 47 (base-10). In whatever base system you have, a digit is the multiplier for some power of the base. In base 10, 47 means 4*10 + 7*1. Likewise, 342 means 3*102 + 4*101 + 2*100.

    As a hexadecimal (base-16) number 21F means 2*162 + 1*161 + 15*160.
    That's the wrong question. The right question is: How do you convert a decimal integer into its representation in base N? Your teacher should have given you some examples of converting from one base to another.
    These are in two different bases, so you have taken them out of context. In hex, F is the "15" digit. In base-36, B is the "11" digit.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java help (how to not to use if statement)
  1. Java Help (Replies: 6)

Loading...