T$he goal of this assignment is • To learn how to use command line arguments You
ID: 3872146 • Letter: T
Question
T$he goal of this assignment is • To learn how to use command line arguments Your task is to write an appli$cation that prints stars (*) or dollar signs ($) depending on the command line argument. The first command line argument must be a string of “star” or “dollar”. The second command line argument is the the number of the symbols to be printed in total. The third command line argument is the number of the symbols to be printed in a line. If the argument values are not valid, print the usage and quit the application. See Fig 7.21 to see how you can convert a string to an integer.
Your code must compile from the windows command line using the following commands.
> java PrintSymbols star 7 3
***
***
*
> java PrintSymbols dollar 9 5
$$$$$
$$$$
> java PrintSymbols comma 10 2 Usage: java PrintSymbols [star|dollar] number_of_symbols number_of_symbols_per_line
> java PrintSymbols star 10 Usage: java PrintSymbols [star|dollar] number_of_symbols number_of_symbols_per_line
Explanation / Answer
PrintSymbols.java
public class PrintSymbols {
public static void main(String[] args) {
if (args.length != 3) {
System.out
.println("Usage: java PrintSymbols [star|dollar] number_of_symbols number_of_symbols_per_line");
} else {
String symbol = args[0];
char ch;
if (symbol.equals("dollar")) {
ch = '$';
} else if (symbol.equals("star")) {
ch = '*';
} else {
System.out
.println("Usage: java PrintSymbols [star|dollar] number_of_symbols number_of_symbols_per_line");
return;
}
int n = Integer.parseInt(args[1]);
int inc = Integer.parseInt(args[2]);
for(int i=1;i<=n;i++) {
System.out.print(ch);
if(i%inc == 0){
System.out.println();
}
}
}
}
}
Output:
$$$$$
$$$$
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.