javac PrintSymbols.java The goal of this assignment is • To learn how to use com
ID: 3871326 • Letter: J
Question
javac PrintSymbols.java
The goal of this assignment is
• To learn how to use command line arguments
Your task is to write 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. 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
class Main{
public static void main(String[] args){
String sym = args[0];
String nos1 = args[1];
String sline1 = args[2];
int nos = (int)nos1;
int sline = (int)sline1;
int count = 0;
if(sym == null || nos1 == null || sline1 == null){
System.out.println("Usage: java PrintSymbols [star|dollar] number_of_symbols number_of_symbols_per_line");
System.exit(0);
}
for(int j=0;j<((nos/sline)+1);j++){
for(int i=0;i<sline;i++){
if(count < nos){
if(sym == "star"){
System.out.print("*");
count += 1;
}else if(sym == "dollar"){
System.out.print("$");
count += 1;
}else{
System.out.println("Usage: java PrintSymbols [star|dollar] number_of_symbols number_of_symbols_per_line");
System.exit(0);
}
}else{
break;
}
}System.out.println();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.