Write a java program to convert the user input based on the following specificat
ID: 3670158 • Letter: W
Question
Write a java program to convert the user input based on the following specifications. The user input has the following format
[variable-length number] [a space] [A|B|H|M]
If the input is a number followed by the character "A," output the ASCII representation of (number - 1), (number), and (number +1).
If the input is a number followed by the character "B," output the decimal equivalent.
If the input is a number followed by the character "H," output the binary equivalent.
If the input is a number followed by the character "M," output the multiplication of all digits as decimal numbers.
If none of the above conditions is met, output "Goodbye."
Please observe the following requirements:
1. There should be at least two (2) "fall through" switch statements in your submission. Use as many "fall through" constructs as possible to simplify your code.
2. Please use only else and else if statements not loops.
3. Please use simple math work not the advanced java math library.
If possible I would appreciate it if you could leave comments in your code.
Explanation / Answer
import java.util.*;
class input
{
public static void main(String args[])
{
String s;
int i,m=1;
Scanner scan = new Scanner(System.in);
System.out.println("Enter String as [variable-length number] [a
space] [A|B|H|M]");
s=scan.nextLine();
int n=s.length();
char c;
String myNumbers = (s+" ").split(" ")[0];
System.out.println(myNumbers);
int d=Integer.parseInt(myNumbers);
int e=0;
switch(s.charAt(n-1))
{
case 'A':
c=(char)(d-1);
System.out.println(c);
c=(char)d;
System.out.println(c);
c=(char)(d+1);
System.out.println(c);
break;
case 'B':
System.out.println("Decimal equivalent is
"+d);
break;
case 'H':
System.out.println
(Integer.toBinaryString(d));
break;
case 'M':
String s1=String.valueOf(d);
int n1=s1.length();
for(i=1;i<=n1;i++)
{
e=d%10;
m=m*e;
d=d/10;
}
System.out.println("Multiplication is
"+e);
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.