Java help (how to not to use if statement)

  • Context: Comp Sci 
  • Thread starter Thread starter notorious9000
  • Start date Start date
  • Tags Tags
    If statement Java
Click For Summary

Discussion Overview

The discussion revolves around converting an integer into a number in a specified base ranging from 2 to 36, with a focus on avoiding the use of multiple if-statements for character representation. Participants explore character arithmetic for representing digits beyond 9 using letters A to Z for values 10 to 36.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants express confusion regarding the teacher's instructions about inputting characters for values 10 to 36.
  • One participant provides a Java code snippet but includes an if-statement to check the base range, which some argue should be avoided.
  • There is a discussion about the representation of numbers in different bases, with examples given for bases 2, 8, 10, 16, and 36.
  • Participants inquire about the calculations for converting decimal numbers to their base representations, specifically how to derive values like 2F and 1B.
  • Questions arise about the method for converting decimal integers to characters, with a suggestion that the focus should be on base conversion rather than character conversion.
  • Clarifications are made regarding the significance of digits in different bases, particularly the meaning of letters in hexadecimal and base-36 systems.

Areas of Agreement / Disagreement

Participants generally agree on the need to convert integers to various bases but express differing levels of understanding about the conversion process and the use of character arithmetic. The discussion remains unresolved regarding the best approach to implement the conversion without if-statements.

Contextual Notes

Some participants note the limitations in understanding the teacher's instructions and the potential confusion around base representations and character conversions. There is also a lack of consensus on how to implement the character arithmetic effectively.

Who May Find This Useful

Readers interested in programming, particularly in Java, and those looking to understand number base conversions and character representations in computing may find this discussion beneficial.

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 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 18 ·
Replies
18
Views
3K