Comp Sci Java help (how to not to use if statement)

  • Thread starter Thread starter notorious9000
  • Start date Start date
  • Tags Tags
    If statement Java
AI Thread Summary
The discussion focuses on expanding a Java program to convert integers into bases ranging from 2 to 36, using character arithmetic instead of multiple if-statements. Participants clarify that digits for bases above 10 should be represented as letters A to Z for values 10 to 36. The program should prompt users for an integer and a base, converting the integer accordingly, with examples provided for base-16 and base-36 conversions. Key questions arise about the calculations for converting decimal numbers to their respective base representations and the logic behind character conversions. The conversation emphasizes understanding base conversions without relying on extensive conditional statements.
notorious9000
Messages
11
Reaction score
0
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 !");
}

}
}
 
Physics news on Phys.org


notorious9000 said:
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 !");
}

}
}
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?
 


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) ?
 


notorious9000 said:
1. I kinda get it. but how did you get these calculations ?
(2F = 2*16 + 15*1) and (1B = 1*36 + 11*1).
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.
notorious9000 said:
2. How do you convert decimal to character ?
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.
notorious9000 said:
3. How did you get (2 and F) and (1 and B) ?
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.
 

Similar threads

Replies
7
Views
2K
Replies
1
Views
2K
Replies
12
Views
2K
Replies
7
Views
3K
Replies
5
Views
3K
Replies
2
Views
1K
Replies
3
Views
6K
Back
Top