Question

JAVA HELP FOR COMP: Can someone please modify the code to create LowestGPA.java that prints out...

JAVA HELP FOR COMP:

Can someone please modify the code to create LowestGPA.java that prints out the name of the student with the lowestgpa.

In the attached zip file, you will find two files HighestGPA.java and students.dat. Students.dat file contains names of students and their gpa. Check if the code runs correctly in BlueJ or any other IDE by running the code. Modify the data to include few more students and their gpa.

import java.util.*; // for the Scanner class

import java.io.*; // for the File class

public class HighestGPA
{
public static void main (String[ ] args) throws FileNotFoundException
{
new HighestGPA().run();
} // method main   

public void run() throws FileNotFoundException
{
final double NEGATIVE_GPA = -1.0;

final String NO_VALID_INPUT =
"Error: the given file has no valid input.";

final String BEST_MESSAGE =
"\n\nThe student with the highest grade point average is ";

Scanner fileScanner = new Scanner (new File ("students.dat"));   

String name,
bestStudent = null;

double gpa,
highestGPA = NEGATIVE_GPA;

while (fileScanner.hasNextLine())
{   
Scanner lineScanner = new Scanner (fileScanner.nextLine());

name = lineScanner.next();
gpa = lineScanner.nextDouble();
if (gpa > highestGPA)
{
highestGPA = gpa;
bestStudent = name;
} // if
} // while
if (highestGPA == NEGATIVE_GPA)
System.out.println (NO_VALID_INPUT);
else
System.out.println (BEST_MESSAGE + bestStudent);
} // method run

} // class HighestGPA

Students.dat file:

Larry 3.3
Curly 3.7
Moe 3.2

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

import java.util.*; // for the Scanner class

import java.io.*; // for the File class

// Defines class LowestGPA

public class LowestGPA

{

// main method definition

public static void main (String[ ] args) throws FileNotFoundException

{

// Creates an anonymous object of the class LowestGPA and calls the run() method

new LowestGPA().run();

} // End of method main

// Method to find and display the lowest GPA student name

public void run() throws FileNotFoundException

{

// Creates a constant to store maximum double value

final double MAX_GPA = Double.MAX_VALUE;

// Creates a string constant to store error message for file  

final String NO_VALID_INPUT = "Error: the given file has no valid input.";

// Creates a string constant to store message for student

final String BEST_MESSAGE = "\n\nThe student with the lowest grade point average is ";

// Creates a string constant to store message for student GPA

final String GPA_MESSAGE = " having GAP: ";

// Creates an object of the Scanner class to open the file for reading

Scanner fileScanner = new Scanner (new File ("students.dat"));

// To store the name read from file and poor student name

String name, poorStudent = null;

// To store the GPA read from file and lowest GPA is assigned to maximum double value constant

double gpa, lowestGPA = MAX_GPA;

// To store the record read from file

Scanner lineScanner = null;

// Loops till end of the file

while (fileScanner.hasNextLine())

{

// Reads a record from file

lineScanner = new Scanner (fileScanner.nextLine());

// Extracts student name

name = lineScanner.next();

// Extracts student GPA

gpa = lineScanner.nextDouble();

// Checks if current GPA read from file is less than the earlier lowestGPA

if (gpa < lowestGPA)

{

// Update the GPA by assigning the current GPA read from the file to lowestGAP

lowestGPA = gpa;

// Update the poor student name by assigning the current student name

// read from the file to poorStudent

poorStudent = name;

} // End of if condition

} // End of while loop

fileScanner.close();

// Checks if lowestGPA is equals to constant MAX_GPA then display the constant error message

if (lowestGPA == MAX_GPA)

System.out.println (NO_VALID_INPUT);

// Otherwise

else

// Display the constant message for student with poor student name with

// GPA constant message with lowest GAP  

System.out.println (BEST_MESSAGE + poorStudent + GPA_MESSAGE + lowestGPA);

} // End of method run

} // End of class LowestGPA

Sample Output:

The student with the lowest grade point average is Rita having GAP: 3.1

Students.dat file contents:

Larry 3.3

Curly 3.7

Moe 3.2

Anil 8.2

Sunil 4.5

Rita 3.1

Gita 7.7

Add a comment
Know the answer?
Add Answer to:
JAVA HELP FOR COMP: Can someone please modify the code to create LowestGPA.java that prints out...
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
  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • How to write a Java file out that that reads from numbers.txt with these numbers 2...

    How to write a Java file out that that reads from numbers.txt with these numbers 2 6 7 9 5 4 3 8 0 1 6 8 2 3 and write to a file called totalSum.txt that looks like: 2+6+7+9=24 and so on using this code import java.io.*; import java.util.Scanner; public class FileScanner {    public static void main(String[] args)    {        /* For Homework! Tokens*/        Scanner file = null;        PrintWriter fout= null;   ...

  • Hello, Could you please input validate this code so that the code prints an error message...

    Hello, Could you please input validate this code so that the code prints an error message if the user enters in floating point numbers or characters or ANYTHING but VALID ints? And then could you please post a picture of the output testing it to make sure it works? * Write a method called evenNumbers that accepts a Scanner * reading input from a file with a series of integers, and * report various statistics about the integers to the...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac...

    Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac -g P4Program.java P4Program.java:94: error: package list does not exist Iterator i = new list.iterator(); ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. Note: Below there are two different classes that work together. Each class has it's own fuctions/methods. import java.util.*; import java.io.*; public class P4Program{ public void linkedStackFromFile(){ String content = new String(); int count = 1; File...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • 1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into...

    1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive. a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points) b) Write additional Java code that will show the average of all numbers in input.txt file (10 points) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

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