Java help (how to not to use if statement)

In summary, to avoid using if statements in Java, you can use the ternary operator or other alternatives such as switch statements, polymorphism, and design patterns. It is recommended to avoid excessive use of if statements as it can make code more complex and prone to errors. However, completely eliminating if statements may not always be possible or necessary. In terms of performance, there is usually no significant difference between using if statements and alternative solutions, so it is important to prioritize writing efficient and maintainable code.
  • #1
notorious9000
11
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
  • #2


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?
 
  • #3


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) ?
 
  • #4


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.
 
  • #5


Yes, you are correct. The task is to take an integer input from the user and convert it into a number in base N, where N can be any value from 2 to 36. The digits representing numbers 10 to 36 should be represented as A to Z, respectively.

The instruction is to not use an if-statement with 20+ different cases to determine which character should be appended to the current string. This means that instead of using multiple if-statements to check the value of the digit and then appending the corresponding character, you should use character arithmetic to determine which character to append.

In your code, you have successfully implemented this by using the modulus operator (%) to get the remainder and then converting it into a character using character arithmetic. This approach is more efficient and avoids the use of multiple if-statements.

I hope this helps clarify the task. Keep up the good work!
 

Related to Java help (how to not to use if statement)

1. How can I write code in Java without using if statements?

One way to avoid using if statements in Java is to use the ternary operator, which allows you to write conditional statements in a single line of code.

2. What are some alternatives to using if statements in Java?

Other alternatives to if statements in Java include switch statements, polymorphism, and design patterns such as the strategy pattern.

3. Why should I avoid using if statements in my code?

Using excessive if statements can make your code more complex and difficult to read and maintain. It can also lead to potential bugs and errors.

4. Can I completely eliminate if statements from my Java code?

While it is possible to write code without using if statements, they are a useful tool in many cases. It is important to use them judiciously and consider alternative solutions when possible.

5. Are there any performance benefits to not using if statements in Java?

In most cases, there is no significant difference in performance between using if statements and alternative solutions. It is more important to focus on writing efficient and maintainable code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top