zu verwenden Ich versuche, meine Anwendung zu testen, indem Sie in einer output.txt-Datei drucken. Es gibt eine input.txt-Datei, die bereits vier Ehrenschüler und mindestens zwei mit dem gleichen GPA von 3.9 enthält, und drei, die keine Ehrenschüler sind. Die Ergebnisse sollten an die Datei output.txt gesendet werden. Die output.txt-Datei sollte enthalten: 1) Alle Schüler 2) Der beste Schüler 3) Anzahl der Auszeichnungen Studenten in der Liste 4) Honours Studenten Die input.txt-Datei, die ich erstellt enthält die folgenden (in Reihenfolge) Nachname, Vorname, ID, GPA und Jahr.Ich versuche, eine input.txt-Datei in eine output.txt-Datei in Java
Die Klasse TestStudents gibt die Datei input.txt aus. Allerdings muss ich die input.txt-Datei verwenden, um die oben erwähnte output.txt-Datei zu drucken. Vielen Dank.
Student-Klasse -
public class Student
{
String lastName, firstName, id;
double gpa;
int year;
public Student (String lastName, String firstName, String id,
double gpa, int year)
{
this.lastName = lastName;
this.firstName = firstName;
this.id = id;
this.gpa = gpa;
this.year = year;
}
public String toString()
{
return this.lastName + ", " + this.firstName + ": " + this.id + " "
+ this.gpa + " " + this.year;
}
public double getGPA()
{
return gpa;
}
public boolean isBetter (Student s)
{
return (this.gpa > ((Student)s).getGPA());
}
public boolean isHonors()
{
if (this.gpa >= 3.5)
{
return true;
}
else
{
return false;
}
}
}
CS152 Klasse -
import java.util.*;
import java.io.*;
public class CS152
{
public static final int MAXSIZE = 22;
private static int size = 0;
public static Student[] createList (Scanner scan) throws IOException
{
Student[] list = new Student [MAXSIZE];
return populateList (list, scan);
}
private static Student[] populateList (Student[] list, Scanner scan)
{
Student s;
if (size < MAXSIZE && scan.hasNext())
{
s = new Student (scan.next(), scan.next(), scan.next(),
scan.nextDouble(), scan.nextInt());
list[size] = s;
size++;
System.out.println (s);
return populateList (list, scan);
}
else
{
return list;
}
}
public static int getSize()
{
return size;
}
// Returns String of all students. Variable n is actual size of the list.
// Assume that n is positive. Recursive code.
public static String toString (Student[] list, int n)
{
String s = " ";
if (n == 1)
{
return s += list[0];
}
else
{
s += list[n].toString() + "\n";
s += "\n";
}
return s + toString (list, n - 1);
}
// Returns the best student. Must use method isBetter in the code.
// Variable n is actual size of the list. Assume that n is positive.
public static Student findBestStudent (Student[] list, int n)
{
if (n == 1)
{
return list[0];
}
else if (list[n].isBetter (list[n - 1]))
{
return list[n];
}
else
{
return findBestStudent (list, n - 1);
}
}
// Returns the number of honor students in the list.
// Must call the method isHonors(). Variable n is actual size of the list.
// Assume that n is positive.
public static int countHonors (Student[] list, int n)
{
if (n == 0)
{
return 0;
}
else if (list[n].isHonors())
{
return 1 + countHonors (list, n - 1);
}
else
{
return countHonors (list, n - 1);
}
}
static ArrayList<Student> studentsList = new ArrayList<Student>();
public static ArrayList <Student> honorsStuds (Student[] list, int n)
{
if (n == 0)
{
return studentsList;
}
else
{
boolean currentIsHonors = list[n - 1].isHonors();
if (currentIsHonors)
{
studentsList.add(list[n - 1]);
return honorsStuds (list, n - 1);
}
else
{
return honorsStuds (list, n - 1);
}
}
}
}
TestStudents Klasse -
import java.io.*;
import java.util.*;
public class TestStudents
{
public static void main (String[] args) throws IOException
{
File input = new File ("input.txt");
Scanner scan = new Scanner (input);
Student[] studentArray = CS152.createList (scan);
}
}
ich das Filewriter in die TestStudents Klasse eingebaut. Eine Liste aller Schüler wird angezeigt. Ich habe immer noch Schwierigkeiten, die Methoden findBestStudent, countHonors und ownsStuds aufzurufen und in TestStudents zu implementieren. Hier ist die überarbeitete TestStudents Klasse:
TestStudents Klasse -
import java.io.*;
import java.util.*;
public class TestStudents
{
public static void main (String[] args) throws IOException
{
File input = new File ("input.txt");
Scanner scan = new Scanner (input);
System.out.println ("All students: ");
Student[] studentArray = CS152.createList (scan);
File output = new File ("output.txt");
FileWriter fWriter = new FileWriter (output);
PrintWriter pWriter = new PrintWriter (fWriter);
pWriter.println (input);
pWriter.close();
}
}
'statische Student [] populateList (Student [] Liste, Scanner Scan) 'wird rekursiv aufgerufen - eine Stufe pro Schüler gelesen - für absolut keinen Grund. Du willst nicht 'if (/ * not done * /) {/ * ... */populateList()} ', du willst nur eine' while (/ * not done * /) {...} 'Schleife . – AJNeufeld
Sie haben den Code bereits geschrieben, um den besten Schüler usw. zu finden.Sie müssen nur die Funktionen aufrufen und das Ergebnis ausdrucken. – AJNeufeld
Das ist Teil, mit dem ich Probleme habe. Ich habe alle Schüler ausgedruckt. Ich muss nun die Methoden findBestStudent, countHonors und owersStuds ausdrucken. Leider kann ich sie nicht in der Datei output.txt drucken. – Voraptustenellus