CS 2130 Computational structures Write Java programs that use command-line input
ID: 3908878 • Letter: C
Question
CS 2130 Computational structures
Write Java programs that use command-line input and call the following integer functions. All calculations for each function should be encapsulated within the function. Use a long datatype instead of int for all input and output variables. Do NOT use string variables in any of the algorithms. A sample main program will be provided as an example.
Turn in source code for each function, along with test output.For each of the above functions, state whether or not the function is one-to-one. Justify your answers. If a function is not one-to-one, show two inputs that have the same output.
4. BaseB: The function has two integer input values X and B, with variable X between 1 and 100,000 and base B between 2 and 10. The function should return as Y the value of X expressed as a base B number. Test your function with the following inputs:X = 359 B = 2X = 632 B = 3X = 757 B = 5X = 981 B = 8X = 8046 B = 10
Explanation / Answer
Given below is the code for the question. YOU NEED TO PASS 2 COMMAND LINE ARGUMENTS, THE VALUE OF NUMBER X AND BASE B
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
public class ConvertBase {
public static void main(String[] args) {
if(args.length != 2)
{
System.out.println("You need to pass 2 command line arguments (numbers)");
System.out.println("Usage ConvertBase <number X> <base B>");
System.exit(1);
}
long X = Long.parseLong(args[0]);
long B = Long.parseLong(args[1]);
long Y = BaseB(X, B);
System.out.println("X=" + X +", B=" + B +", Y=" + Y);
}
public static long BaseB(long X, long B){
long Y = 0;
long rem;
long powerOf10 = 1;
while(X > 0){
rem = X % B;
Y = rem * powerOf10 + Y;
X /= B;
powerOf10 *= 10;
}
return Y;
}
}
output (for command line args: 359 2)
====
X=359, B=2, Y=101100111
output (for command line args: 632 3)
====
X=632, B=3, Y=212102