Question

In: Computer Science

PLEASE USE ARRAYS IN JAVA TO ANSWER THIS Write a 'main' method that examines its command-line...

PLEASE USE ARRAYS IN JAVA TO ANSWER THIS

Write a 'main' method that examines its command-line arguments and

  • calls the (add) method if the first parameter is a "+"
  • calls the (subtract) method if the first parameter is a "-"
  • calls the (doubled) method if the first parameter is a "&"

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

Solutions

Expert Solution

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


Related Solutions

Please use Java language to write an efficient function that compute the intersection of two arrays...
Please use Java language to write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command...
**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command that would find all lines that have an email address and place a label email = before the line in the file longfile output will multiple lines similar to this one : using a good awk command the output would be something like this email = From: "Linder, Jann/WDC" <[email protected]> email = To: Mr Arlington Hewes <[email protected]> email = > From: Mr Arlington Hewes...
answer in JAVA language please In this assignment you will use: one-dimensional arrays; strings and various...
answer in JAVA language please In this assignment you will use: one-dimensional arrays; strings and various string-handling methods; file input and output; console input and output; loops; and conditional program statements. Instruction You are asked to write a program that translates a Morse code message into English language. The program will ask the user to enter filenames for two input files, one for the code translation table data and the other for the coded message data, as well as an...
Write a Java application that accepts a bar code as a command line parameter and prints...
Write a Java application that accepts a bar code as a command line parameter and prints out the ZIP code. Assume that the bar code uses the symbols "|" and ":" for the long and short bars, respectively. Provide warnings on errors in the bar code specifying what exactly is wrong. The bar code input should be in the format specified in Problem 1, including the pair of the full bars at the beginning and at the end. Important: The...
USE JAVA. Write a very general sort method that can sort any type of data arrays/lists....
USE JAVA. Write a very general sort method that can sort any type of data arrays/lists. For example, can sort a list of integers in ascending order, a list of integers in descending order, a list of doubles, a list of student objects (with names and scores) in ascending order of names, or in descending order of scores, … You can use any pre-defined sort function or can code your own. Use your favorite language for implementation. If your language...
Please answer with a fully detailed solution with examples. In Java, write a method to determine...
Please answer with a fully detailed solution with examples. In Java, write a method to determine if a string contains only digit characters. In Java, write a method on how to find each occurrence of a letter given a string. In Java, given a list of numbers, write an algorithm to find all the triples and pairs of numbers in the list that equal exactly 13. In Java, write a function to find the total number of each character in...
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Please use JAVA to do this: Write a method that takes four strings as parameter. The...
Please use JAVA to do this: Write a method that takes four strings as parameter. The first string should be a pokemon name, the second a pokemon type(either fire, water, or leaf, where water beats fire, fire beats leaf and leaf beats water), the third a pokemon name, and the fourth a pokemon type. The method should print out which pokemon has the advantage over the other based on their type. Example: Pokemon X(which is the fire type) has the...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and returns a boolean. The method should return true if both arrays contain the same values (case sensitive), otherwise returns false. Note - the two arrays must be the same length and the values must be in the same order to return true. Part  2 Write a method named generateArray that accepts an int size as its parameter and returns an int[] array where each element...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT