LAB
EXERCISE 23: Student Data 1
Goal:
In this exercise, you will learn how to:
1.
Insert the Main Method (Main Entry Point to the
Java Program)
2. Import the Scanner Library Class to accept
data from the console
3. Import the Library Classes which allow data
to be written to an external text file
a. import java.io.File
b. import java.io.FileWriter
c. import java.io.BufferedWriter
d import java.io.IOException
4. Use the tab
delimiter “\t” to separate the data fields(variables)
when writing the data to a file.
5. Create a Superclass
6. Create a Subclasses
7. Declare Public Variables
that can be accessed by a Subclass
8. Create Public Void Methods
9. Create Public Methods that will return values
10. Use a Try-Catch block statement for error
file handling and exceptions
11. Create the Object from the Class
12. Call the Object’s Method
Program Specifications:
The
program prompts the user to enter the student ID, first and last name, total
Lab exercises points, total Project points and Final Exam points. The program
then calculates the student total points earned and the student's letter
grade.
The data will be written to an external text file.
In the text file, each line is the student’s data which makes up a single
record (row).
The record will contain 6 fields separated by a “tab” delimiter: student ID, student first name, last name,
course, total points earned and letter grade
A. Pre-requisites:
1. Create a folder on your desktop Exercise-23
2. Launch Java EE- Eclipse
Note: You will need to use the Java
Perspective Workbench for this exercise
3. Setup your Eclipse Workspace to point to the Exercise-23
folder
a. Select File-> Switch Workspace
b. Browse and select your Exercie-23 folder as your Workspace.
B. Requirements:
1. Create
a Java Project and name it as StudentDataPart1
2. Create
the first Class that will have the Main Method
a.
Name the
Class as MainApp
b.
Choose
the main method to insert into the class
3. Create
a class called Student. This
will be the superclass (parent)
4. Create
a class called Grade. This
subclass that will inherit the methods and variables of the Student superclass
5. Create
a class called Data. This
subclass that will inherit the methods and variables of the Grade superclass
C.
Requirements for the Student Class:
1. Insert
the import java.util.Scanner class
which will allow data to be inputted from the console.
Insert the line @SuppressWarnings("resource")
2. Create
the public variables
public
String id, lastName, firstName,
letterGrade; |
3. Create
the public constant static variables and initialize
public
final static String COURSE= "COMSC-051 Java Programming Part 1"; |
4. Create
the public Void Method called getData() that
will prompt the user to input the data.
Below is the code for the method.
//
declare the scanner object used to input the student's ID from the console //
declare the scanner object used to input the student's last name from the
console //
declare the scanner object used to input the student's first name from the
console //
declare the scanner object used to input the lab exercises points earned from
the console //
declare the scanner object used to
input the project points earned from the console //
declare the scanner object used to input the final exam points earned from
the console |
D.
Requirements for the Grade Class:
1. The Grade class will be a subclass of the Student
parent class (superclass)
NOTE: A subclass will inherit the methods and variables from the superclass
Add the keyword "extends Students"
after the name of class
2. Create
the a public Method called calcTotalPoints () with
an integer returned data type that will
calculate the total points earned by the student
totalPoints = labPoints + projectPoints + finaExamPoints; |
3. Create
the a public Method called calcGrade() with
a String returned data type that will calculate
the student’s letter grade based on the total points earned
if (totalPoints>=900) { |
4. Create
the public Void Method
called displayData() that will display the required
output on the screen. Below is the code
for the method.
//
display the inputed data on the console //
display the returned results on the console |
E.
Requirements for the Data Class:
1. The Data class will be a subclass of the Student
parent class (superclass)
NOTE: A subclass will inherit the methods and variables from the superclass
Add the keyword "extends Grade" after the name of class
2. Insert
the following Library Classes which will write and store the data to an
external text file
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
3. Insert
the import java.util.Scanner class
which will allow data to be inputted from the console.
4. Create
a Void Method
called writeData () that will execute the code to write the data to an
external file – studentdata.txt
a. Declare the scanner object used to input the number of students in the class
Scanner
inputNum= new Scanner(System.in); |
b. Create instance of the
object filename from the File library class & pass the file name
“studentdata.txt” as a parameter
File
filename = new File("studentdata.txt"); |
c. Use Try-Catch
block to perform error handling if the program cannot write to the external
file
b1. Create the instance of the
object "fw"
from the FileWriter
library class
& pass value of "filename" object as the parameter
FileWriter fw
= new FileWriter(filename); |
b2. Create instance of the object "br" from the BufferredWriter library
class & pass the value of the "fw" object as the parameter
BufferedWriter bw
= new BufferedWriter(fw); |
b3. Create a For loop to perform the iteration of the code based on the
number of students in the class.
i.
Call the methods from the parent class
getData(); // call the
void method from the parent class- StudentGrade |
ii. Write the data to the studentdata.txt
file using "bw" object write method
Insert tabs “\t” as the tab delimiter between the data fields
(variables)
bw.write(id +"\t"+lastName+"\t"+firstName
+ "\t"+ COURSE + "\t"+ totalPoints
+ "\t" + letterGrade ); |
iii. Display the message that data was
written successfully to external file
System.out.println(); |
d. Close the BufferedWriter
object
bw.close(); // close the BufferedWriter object |
e. In the Catch-block
parameter, add (IOException e)
Insert the code below to display
the error handling message if cannot write to file
System.out.println("Unable to
write to the file: " + filename.toString()); |
f. Close the Scanner object
inputNum.close(); // close the Scanner object |
F. Requirements for the MainApp Class:
1. Add
comments (documentation)– Program Description, Author and Date
2. Create
an instance of the object - employeeInfo from the
Data class:
Data studentInfo = new Data()
3. Call the object's void
method- writeData:
studentInfo.writeData();
G.
Test:
1. Save your Java code
2. Compile and run your Java program.
3. Verify there is no syntax, logical or run-time
errors.
4. Use the following set of test data to determine
if the application is running properly
5. Expand
the Project Folder. Press F5 to
Refresh. Verify the studentdata.txt is in the root of the project folder.
H. Submit your exercise in the Canvas Lab Exercise
#23 Drop Box.
1. Submit the screen shot of the Eclipse
Workbench window showing the Console output screen.
You can use Paint (save as JPG) or Word to paste the screenshot.
2.
Submit the studentdata.txt data file.
3. Zip up and submit the compressed StudentDataPart1 subfolder that is in the Exercise-23
folder.
NOTE: Right click on the subfolder and select
Send to “Compress Folder”. The file will
have a file extension of .zip.
NOTE: You will need to upload the 3 files above
separately.