Question

Write a java program to create a student file which includes first name, last name, and...

Write a java program to create a student file which includes first name, last name, and GPA. Write another Java program to open the students file. Display the students list and calculate the average GPA of the class.  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Program:

(1) File: createStudentFile.java

import java.io.*;
import java.util.*;

class createStudentFile
{
   //Main method
   public static void main(String[] args) throws FileNotFoundException
   {
       String fName, lName;
       double GPA;
       String loop = "yes";
      
       //Scanner class object
       Scanner reader = new Scanner(System.in);
      
       //PrintWriter object
       PrintWriter writer = new PrintWriter(new File("studentGPA.txt"));
      
       //Reading from user
       while(loop.equalsIgnoreCase("yes"))
       {
           System.out.print("Enter First Name: ");
           fName = reader.nextLine();
           System.out.print("Enter Last Name: ");
           lName = reader.nextLine();
           System.out.print("Enter GPA: ");
           GPA = reader.nextDouble();
          
           //Storing in file
           writer.println(fName + " " + lName + " " + GPA);
           reader.nextLine();
          
           //Prompting user
           System.out.print("\n Do you want add more? (yes/no): ");
           loop = reader.nextLine();
       }
      
       //Closing files
       writer.close();
       reader.close();
   }
}

Sample Run:

_____________________________________________________________________________________________

(2) File: CalculateStudentGPA.java

import java.util.*;
import java.io.*;

class CalculateStudentGPA
{
   //Main method
   public static void main(String[] args) throws FileNotFoundException
   {
       //Scanner object
       Scanner reader = new Scanner(new File("studentGPA.txt"));
      
       String fName, lName;
       double GPA;
       int cnt=0;
      
       double sGPA = 0, aGPA;
      
       //Reading data from file
       while(reader.hasNext())
       {
           fName = reader.next();
           lName = reader.next();
           GPA = reader.nextDouble();
          
           //Printing values
           System.out.println("\n First Name: " + fName + ", Last Name: " + lName + ", GPA: " + GPA);
          
           //Accumulating GPA
           sGPA += GPA;
           cnt++;
       }
      
       //Finding average GPA
       aGPA = sGPA / (double)(cnt);
      
       //Printing average GPA
       System.out.printf("\n\n Average GPA of class: %.2f \n", aGPA);
   }
}

Sample Run:

Add a comment
Know the answer?
Add Answer to:
Write a java program to create a student file which includes first name, last name, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java - In NetBeans IDE: • Create a class called Student which stores: - the name...

    Java - In NetBeans IDE: • Create a class called Student which stores: - the name of the student - the grade of the student • Write a main method that asks the user for the name of the input file and the name of the output file. Main should open the input file for reading . It should read in the first and last name of each student into the Student’s name field. It should read the grade into...

  • PYTHON*************** Create a Person Class that: Accepts First Name, Last Name, Age, and Gender Has a...

    PYTHON*************** Create a Person Class that: Accepts First Name, Last Name, Age, and Gender Has a method that adds all persons Validate all input, not left empty for First Name, Last Name, Gender and numeric for age and throw an exception if it does not validate within the class. Create a Student Class that: Inherits the Person Class Accepts GPA Validate GPA (>= 0 and <= 4.0) and throw exception if it does not validate within the class. Has methods...

  • Question 1 (24 Marks] 1. (4 marks) Write an immutable Person class in Java which provides...

    Question 1 (24 Marks] 1. (4 marks) Write an immutable Person class in Java which provides the following. • Two attributes: lastName and first Name of type String. • Appropriate initialization, accessors/mutator methods. 2. (6 marks) Write a Student class that is a subclass of Person class (in the previous question) which provides the following. • Two attributes: A unique student ID of type String and GPA of type float; the student ID is initialized when a Student object gets...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

  • Create a class “Person” which includes first name, last name, age, gender, salary, and haveKids (Boolean)...

    Create a class “Person” which includes first name, last name, age, gender, salary, and haveKids (Boolean) variables. You have to create constructors and properties for the class. Create a “MatchingDemo” class. In the main function, the program reads the number of people in the database from a “PersonInfo.txt” file and creates a dynamic array of the object. It also reads the peoples information from “PersonInfo.txt” file and saves them into the array. In C# Give the user the ability to...

  • Write a Java program that reads a file until the end of the file is encountered....

    Write a Java program that reads a file until the end of the file is encountered. The file consists of an unknown number of students' last names and their corresponding test scores. The scores should be integer values and be within the range of 0 to 100. The first line of the file is the number of tests taken by each student. You may assume the data on the file is valid. The program should display the student's name, average...

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

  • SC 111 - BMCC Exam Student Name: Type # 4 - 20 Points write a program...

    SC 111 - BMCC Exam Student Name: Type # 4 - 20 Points write a program that uses a structure named Student to store the following information about a Student: Name ID GPA Address The program should create three Student instances, store values, and pass all of them to a function that displays the information about the Student in a clearly formatted manner. The program also uses another function to calculate and display the average GPA of all three Students.

  • In Java, write a program that asks the user for the name of a file. The...

    In Java, write a program that asks the user for the name of a file. The program should read all the numbers from the given file and display the total and average of all numbers in the following format (three decimal digits): Total: nnnnn.nnn Average: nnnnn.nnn Class name: FileTotalAndAverage

  • 1. Create a file that contains 3 rows of data of the form: First Name Middle...

    1. Create a file that contains 3 rows of data of the form: First Name Middle Name Last Name Month Day Year Month Day Year GPA. This file represents student data consisting of first name, middle name, last name, registration month, registration day, registration year, birth month, birth day, birth year, current gpa. ...or you may download the data file inPut.txt. inPut.txt reads:​​​​​​​ Tsia Brian Smith 9 1 2013 2 27 1994 4.0 Harper Willis Smith 9 2 2013 9...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT