// Flowers.java - This program reads names of flowers and whether they are grown
ID: 3913518 • Letter: #
Question
// Flowers.java - This program reads names of flowers and whether they are grown in shade or sun from an input // file and prints the information to the user's screen. // Input: flowers.dat. // Output: Names of flowers and the words sun or shade. import java.io.*; // Import class for file input. public class Flowers { public static void main(String args[]) throws Exception { // Declare variables here // Open input file. // Create BufferedReader object. // Write while loop that reads records from file. // Print flower name and the words sun or shade. br.close(); System.exit(0); } // End of main() method. } // End of Flowers class.
Astilbe
Shade
Marigold
Sun
Begonia
Sun
Primrose
Shade
Cosmos
Sun
Dahlia
Sun
Geranium
Sun
Foxglove
Shade
Trillium
Shade
Pansy
Sun
Petunia
Sun
Daisy
Sun
Aster
Sun
1-Make sure the source code file named Flowers.java is open.
2-Declare the variables you will need.
2-Write the Java statements that will open the input file, flowers.dat, for reading.
3-Write a while loop to read the input until EOF is reached.
4-In the body of the loop, print the name of each flower and where it can be grown (sun or shade) using a format of flowerName + " is grown in the " + sunOrShade).
Execute the program
Explanation / Answer
Below is the solution:
Flowers.java:
package chegg;
import java.io.*; // Import class for file input.
public class Flowers {
public static void main(String args[]) throws Exception {
// Declare variables here
String flowerName = ""; // Name of the flower.
String sunOrShade = ""; // Does the flower grow in the sun or shade?
// Open input file.
String desktop = System.getProperty ("user.home") + "/Desktop/"; //reads the file from desktop in linux if in windows give the directory path like "D://Flowers.dat"
FileReader in = new FileReader(desktop+"Flowers.dat");
BufferedReader br = new BufferedReader(in);
String line; //for line read
int lineNumber=0; //for count the line number
while ((line = br.readLine()) != null) { // Write while loop that reads records from file.
lineNumber++;
if(line.equals("Sun") || line.equals("Shade")){ //check if the line has sun or shade
sunOrShade=line; //store the sun or shade
}
else {
flowerName=line; //store the flowername
}
if((lineNumber%2)==0) { //check if the line number is even then
System.out.println(flowerName + " is grown in the " + sunOrShade);// Print flower name and the words sun or shade.
}
}
in.close();
System.exit(0);
} // End of main() method.
} // End of Flowers class.
sample output:
Astilbe is grown in the Shade
Marigold is grown in the Sun
Begonia is grown in the Sun
Primrose is grown in the Shade
Cosmos is grown in the Sun
Dahlia is grown in the Sun
Geranium is grown in the Sun
Foxglove is grown in the Shade
Trillium is grown in the Shade
Pansy is grown in the Sun
Petunia is grown in the Sun
Daisy is grown in the Sun
Aster is grown in the Sun
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.