W$rite an application that prints #stars (*) or #dollar signs ($) depending on t
ID: 3872744 • Letter: W
Question
W$rite an application 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.
Your code must compile from the windows command line using the following commands.
javac PrintSymbols.java
> 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:
java PrintSymbols star 7 3
***
***
*
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.