Use Stars.java as a template you can modify and use it to perform the following
ID: 3635718 • Letter: U
Question
Use Stars.java as a template you can modify and use it to perform the following (incorporate Java files):1. The program should ask the user for a number input (integer). The program will then produce the stars ("*") as a square like the sample Stars.java. However while it is printing a line of stars("*"), it will also write each set (line) of stars on a text file vice displaying on screen.
2. The program will then open the same text file for reading. Read a line at a time and display on screen. The program will keep reading lines until no more data (end of file).
The program to adjust:
import java.util.Scanner;
public class Stars5 {
public static void main(String[] args)
{
//create keyboard
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome! This program will take your " +
"number input, and generate a square " +
"of stars. ");
System.out.print("Please enter a number: ");
int number = keyboard.nextInt();
for (int i=1; i<=number; i++)
{
for (int j=1; j<=number; j++)
System.out.print("*");
System.out.println();
}//end for
}//end main
}//end of Stars
Explanation / Answer
import java.io.*; import java.util.*; public class Stars { public static void main(String[] args) throws java.io.IOException, java.io.FileNotFoundException { //create keyboard Scanner keyboard = new Scanner(System.in); PrintWriter out = new PrintWriter("stars.txt"); System.out.println("Welcome! This program will take your " + "number input, and generate a square " + "of stars. "); System.out.print("Please enter a number: "); int number = keyboard.nextInt(); for (int i=1; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.