Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your task is to write an application that prints stars (*) or dollar signs ($) d

ID: 3871325 • Letter: Y

Question

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 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.

javac PrintSymbols.java

Example command and output

>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

public class PrintSymbols {
public static void main(String args[]) {
String symbol=args[0];
if(!(symbol.equals("star")||symbol.equals("dollar"))){
System.out.println("Usage: java PrintSymbols [star|dollar] number_of_symbols number_of_symbols_per_line");
return ;
}
char sym='*';
if(symbol.equals("star"))sym='*';
if(symbol.equals("dollar"))sym='$';
int numTotals=0;
int numPerLine=0;
try{
numTotals=Integer.parseInt(args[1]);
numPerLine=Integer.parseInt(args[2]);
}
catch(Exception e){
System.out.println("Usage: java PrintSymbols [star|dollar] number_of_symbols number_of_symbols_per_line");
return;
}
int symperrow=numPerLine;
int numLines=numTotals/numPerLine;
int symlastrow=numTotals%numPerLine;
for(int i=0;i<numLines;i++){
for(int j=0;j<numPerLine;j++){
System.out.print(sym);
System.out.print(" ");
}
System.out.println();
}
for(int j=0;j<symlastrow;j++){
System.out.print(sym);
System.out.print(" ");
}

}
}