Using the puTTy Linux system and create the following Java command line iterator
ID: 3883584 • Letter: U
Question
Using the puTTy Linux system and create the following Java command line iterator application Lab3. Pass in four arguments from the Linux command line when the program starts. [ SHOULD BE DONE IN THE NANO EDITOR ( nano Lab3.java)
For example java Lab3 value1 value2 value3 value4
The four command line arguments get passed into the main method as a String array in the variable object named args. Remember they get passed in as strings so you need to parse them into integers using the Integer.parseInt(args[X]) before you can treat them as integers. Create four sections that perform the following functions.
for loop iteration structure: loop the number of times of value1 passed in, printing the counter value each time
while loop iteration structure: loop the number of times of value2 that was passed in, printing the counter value each time.
Create a do while loop iteration structure: loop the number of times of value3 that was passed in, printing the counter value each time
switch statement: Using the value4 passed in find the value in the switch and print the value one, two, three. Check only for 1-3 values.
THANKS
Joseph root@ist242:/home/JoeOakes# javac Lab3.javia root@ist242:/home/Joe0akes# java Lab3 3 4 5 2 for loop: 0 for loop: 1 for loop: 2 while loop: 0 while loop: 1 while loop: 2 while loop: 3 do while loop: 6 do while loop: 1 do while loop: 2 do while loop: 3 do while loop: 4 twoExplanation / Answer
//program
public class MyClass {
public static void main(String args[])
{
//declare arrays to hold values of command line args
int []value = new int[args.length];
//declare index of array
int count = 0;
for (String s: args)
{
value[count++] = Integer.parseInt(s) ;
}
/*for(int i = 0; i < count;i++)
{
System.out.printf("%d ",value[i]);
}*/
//for loop
for(int i = 0; i < value[0];i++)
{
System.out.printf("for loop: %d ",i);
}
//while loop
int i = 0;
while(value[1] > 0)
{
System.out.printf("while loop: %d ",i++);
value[1]--;
}
//do while loop
i = 0;
do
{
System.out.printf("do while loop: %d ",i++);
value[2]--;
}while(value[2] > 0);
//switch
switch(value[3])
{
case 1:
System.out.println("one ");
break;
case 2:
System.out.println("two ");
break;
case 3:
System.out.println("three ");
break;
}
}
}
----------------------------------------------------------------------------------------
//output
for loop: 0
for loop: 1
for loop: 2
while loop: 0
while loop: 1
while loop: 2
while loop: 3
do while loop: 0
do while loop: 1
do while loop: 2
do while loop: 3
do while loop: 4
two
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.