Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I keep getting the error fir my java codestudents.txt not found please help me fix import java.io . File; import java.io . FileNotFoundException; import java.util.ArrayList;

I keep getting the error fir my java code"students.txt not found" please help me fix import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
class Student {
private String name;
private int creditHours;
private int qualityPoints;
public Student(String name, int creditHours, int qualityPoints){
this.name = name;
this.creditHours = creditHours;
this.qualityPoints = qualityPoints;
}
public double gpa(){
return (double) qualityPoints / creditHours;
}
public boolean eligibleForHonorSociety(){
return gpa()> HonorSociety.getGpaThreshold();
}
@Override
public String toString(){
return "Name: "+ name +", GPA: "+ gpa();
}
public static void setGpaThreshold(double threshold){
HonorSociety.setGpaThreshold(threshold);
}
}
class Undergraduate extends Student {
private String year;
public Undergraduate(String name, int creditHours, int qualityPoints, String year){
super(name, creditHours, qualityPoints);
this.year = year;
}
@Override
public boolean eligibleForHonorSociety(){
return super.eligibleForHonorSociety() && (year.equals("Junior")|| year.equals("Senior"));
}
@Override
public String toString(){
return super.toString()+", Year: "+ year;
}
}
class Graduate extends Student {
private String degree;
public Graduate(String name, int creditHours, int qualityPoints, String degree){
super(name, creditHours, qualityPoints);
this.degree = degree;
}
@Override
public boolean eligibleForHonorSociety(){
return super.eligibleForHonorSociety() && degree.equals("Masters");
}
@Override
public String toString(){
return super.toString()+", Degree: "+ degree;
}
}
class HonorSociety {
private static double gpaThreshold;
public static double getGpaThreshold(){
return gpaThreshold;
}
public static void setGpaThreshold(double threshold){
gpaThreshold = threshold;
System.out.println("Honor Society GPA Threshold set to: "+ gpaThreshold);
}
}
public class Project2{
public static void main(String[] args){
ArrayList students = new ArrayList<>();
try {
Scanner scanner = new Scanner(new File("students.txt"));
double totalGpa =0;
int count =0;
while (scanner.hasNextLine()){
String line = scanner.nextLine();
String[] data = line.split("");
String name = data[0]+""+ data[1];
int creditHours = Integer.parseInt(data[2]);
int qualityPoints = Integer.parseInt(data[3]);
if (data.length ==5){
// Undergraduate
String year = data[4];
students.add(new Undergraduate(name, creditHours, qualityPoints, year));
} else if (data.length ==4){
// Graduate
String degree = data[3];
students.add(new Graduate(name, creditHours, qualityPoints, degree));
}
totalGpa += students.get(count).gpa();
count++;
}
if (count >0){
double averageGpa = totalGpa / count;
Student.setGpaThreshold((4.0+ averageGpa)/2);
}
scanner.close();
} catch (FileNotFoundException e){
System.out.println("Error: students.txt not found.");
return;
}
System.out.println("Honor Society Members:");
for (Student student : students){
if (student.eligibleForHonorSociety()){
System.out.println(student);
}
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Technology And Management Computers And Information Processing Systems For Business

Authors: Robert C. Goldstein

1st Edition

0471887374, 978-0471887379

More Books

Students also viewed these Databases questions

Question

2. What process will you put in place to address conflicts?

Answered: 1 week ago