Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › To learn how to process command line arguments, to write a method and call it by sending an argument.
New Posts  All Forums:Forum Nav:

To learn how to process command line arguments, to write a method and call it by sending an argument.

post #1 of 4
Thread Starter 
Objective: To learn how to process command line arguments, to write a method and call it by sending an argument.
Steps:

1. Create a new folder called 'assign51' under C:\Java\Lesson5. Copy HelloWorld.java from Lesson 1 into it, and rename the file to "Max.java". Replace the word "HelloWorld" with "Max". Replace the System.out line with instructions to declare variables, and display output as described next.
2. First check if at least one run-time argument is supplied.
• If not, show an appropriate error message and exit the program.
• If so, add instructions to find the maximum out of the set of integers supplied at the command prompt. You can assume the user supplies only whole numbers and not decimal fractions. The program needs to display the output as shown below:

(a) The list of values supplied, with a label: 'The input values are: ', followed by the values separated by commas,
and, on the next line, the following:
(b) The maximum of these values, with a label: 'The maximum value is: ', followed by the maximum value.
So, as an example, if you entered the command line arguments: 4 8 2 6 at run time as:


java Max 4 8 2 6

it should show the following as output:

The input values are: 4, 8, 2, 6
The maximum value is: 8

3. To find the max value, write a separate method called findMax that takes the array of Strings as input, finds the max value and returns it back to the caller. DO NOT put that part of the code inside main(). Instead, simply call the new function you wrote from inside main(), passing it the 'args' String array as input argument.
4. To display the results, write another method called displayResult that takes the array of Strings as input and the max value (as an int), and displays the output as described in (2) above. Call displayResult() from inside main().
5. Submit Max.java at the end of (4) above.
post #2 of 4
Please do your own homework...
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
post #3 of 4
Thread Starter 
class Max
{
public static void main(String[] args)
{
if (args.length > 0)
{
int max = findMax(args);
displayResult(args, max);
}
else
{
System.out.println("Please supply values to find max");
System.exit(0);
}
}

static int findMax(String[] values)
{
int maxVal = Integer.parseInt(values[0]);
for (int i = 1; i < values.length; i++)
{
int val = Integer.parseInt(values);
if (val > maxVal) maxVal = val;
}
return maxVal;
}

static void displayResult(String[] values, int max)
{
String tmp = values[0];
for (int i = 1; i < values.length; i++)
{
tmp += ", " + values
;
}
System.out.println("The input values are: " + tmp);
System.out.println("The maximum value is " + max);
}
}
post #4 of 4
yes, this shows that you did something. But you have not asked a question. Again, please do your own homework. If you have questions, then you need to ask. Something like, "I am having trouble figuring out the logic for this if, can anyone spot a problem?"
Please don't post a question like, "It is broken, please help?"
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › To learn how to process command line arguments, to write a method and call it by sending an argument.