In: Computer Science
PLEASE USE ARRAYS IN JAVA TO ANSWER THIS
Write a 'main' method that examines its command-line arguments and
add should add the 2 numbers and print out the result.
subtract should subtract the 2 numbers and print out the
results.
Double should add the number to itself and then output the
result.
Command line arguments + 5 2 : should print 7
Command line arguments - 5 2 : should print 3
Command line arguments & 5 : should print 10
JAVA CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is
public. */
public class MyClass
{
public static int add(int x, int y){
return x+y;
}
public static int subtract(int x, int y){
return x+y;
}
public static int doubled(int x){
return x+x;
}
public static void main (String[] args)
{
char sign = args[0].charAt(0);
if(sign=='+'){
int first
=Integer.parseInt(args[1]);
int second =
Integer.parseInt(args[2]);
System.out.println(add(first,second));
}
else if(sign=='-'){
int first
=Integer.parseInt(args[1]);
int second =
Integer.parseInt(args[2]);
System.out.println(subtract(first,second));
}
if(sign=='&'){
int first
=Integer.parseInt(args[1]);
System.out.println(doubled(first));
}
}
}
OUTPUT:
CommandLine arguments
+12
result:
3