Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Let's use the String array[] parameter passed to our main() method, named args, to pass information into our main() method, from the command line ,

Let's use the String array[] parameter passed to our main() method, named args, to pass information into our main() method, from the command line, and use that String[] information contained in our args identifier to do some computations.

pseudocode:

pass in an operation and then perform that operation on all successive numbers passed into our main() method application.

Example 1 command line execution of our program:

>java MyCommandLine + 1 2 3 4 5

output would be:

1 + 2 + 3 + 4 + 5 = 15

example 2:

>java MyCommandLine - 15 2 3

output would be:

15 - 2 - 3 = 10

example 3:

>java MyCommandLine x 1 2 3

output would be:

1 x 2 x 3 = 6

Important

Use a Switch statement on the operation (first element in the array[]) + *

Modularize you add() , subtraction(), multiply() work

Topics that could help complete the lab

image text in transcribedimage text in transcribedimage text in transcribed
7.13.1 Passing Strings to the main Method You can pass strings to a main method from the command line when you run the program. The following command line, for example, starts the program TestMain with three strings: arg0, arg1, and arg2: java TestMain argo arg1 arg2 argo, arg1, and arg2 are strings, but they don't have to appear in double quotes on the com- mand line. The strings are separated by a space. A string that contains a space must be enclosed in double quotes. Consider the following command line: java TestMain "First num" alpha 53 It starts the program with three strings: First num, alpha, and 53. Since First num is a string, it is enclosed in double quotes. Note 53 is actually treated as a string. You can use "53" instead of 53 in the command line. 7.13 Command-Line Arguments 277 When the main method is invoked, the Java interpreter creates an array to hold the command-line arguments and pass the array reference to args. For example, if you invoke a program with n arguments, the Java interpreter creates an array such as the one that follows: args = new String[n]; The Java interpreter then passes args to invoke the main method. Note If you run the program with no strings passed, the array is created with new String [0]. In this case, the array is empty with length 0. args references to this empty array. Therefore, args is not null. but args . length is 0.7.9 Variable-Length Argument Lists A variable number of arguments of the same type can be passed to a method and treated as an array. Key Point You can pass a variable number of arguments of the same type to a method. The parameter in the method is declared as follows: typeName. . . parameterName In the method declaration, you specify the type followed by an ellipsis (. . .). Only one variable-length parameter may be specified in a method, and this parameter must be the last parameter. Any regular parameters must precede it. 7.10 Searching Arrays 269 Java treats a variable-length parameter as an array. You can pass an array or a variable number of arguments to a variable-length parameter. When invoking a method with a variable number of arguments, Java creates an array and passes the arguments to it. Listing 7.5 pres- ents a method that prints the maximum value in a list of an unspecified number of values. LISTING 7.5 VarArgsDemo . java public class VarArgsDemo { public static void main (String args) { printMax (34, 3, 3, 2, 56.5) ; pass variable-length arg list printMax (new double {1, 2, 3) ) ; pass an array arg public static void printMax (double. . . numbers) { a variable-length arg if (numbers . length == 0) { parameter System . out . printin("No argument passed") ; 10 return; 11 12 13 double result = numbers [0] ; 14 15 for (int i = 1; i result) 17 result = numbers [i ] ; 18 19 System . out . printIn("The max value is " + result); 20 21 Line 3 invokes the printMax method with a variable-length argument list passed to the array numbers. If no arguments are passed, the length of the array is 0 (line 8). Line 4 invokes the printMax method with an array. 7.9.1 What is wrong with each of the following method headers? Check a. public static void print (String. . . strings, double. . . numbers) Point b. public static void print (double. . . numbers, String name) c. public static double. . . print (double d1, double d2) 7.9.2 Can you invoke the printMax method in Listing 7.5 using the following statements? a. printMax ( 1, 2, 2, 1, 4) ; b. printMax (new double [ ] {1, 2, 3}) ; C. printMax (new int [ ] {1, 2, 3)) ;7.2.7 Foreach Loops Java supports a convenient for loop, known as a foreach loop, which enables you to traverse the array sequentially without using an index variable. For example, the following code dis- plays all the elements in the array myList: for (double e: myList) { System . out . printIn(e) ; You can read the code as "for each element e in myList, do the following." Note that the variable, e, must be declared as the same type as the elements in myList. In general, the syntax for a foreach loop is for (elementType element: arrayRefVar) { / / Process the element You still have to use an index variable if you wish to traverse the array in a different order or change the elements in the array. A Caution Accessing an array out of bounds is a common programming error that throws a runtime Array IndexOutOf BoundsException. To avoid it, make sure you do not use Array IndexOutOf Bounds- an index beyond arrayRefVar . length - 1 or simply using a foreach loop if Exception possible. Programmers often mistakenly reference the first element in an array with index 1. but it should be 0. This is called the off-by-one error. Another common off-by-one error in off-by-one error a loop is using

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

Highest peak as well as active volcano of North America?

Answered: 1 week ago

Question

Which is the largest fresh water leak in the world?

Answered: 1 week ago

Question

Which north american country known as the sugar bowl of the world?

Answered: 1 week ago

Question

South America is the continent located in which hemisphere?

Answered: 1 week ago