Need to modify this file to print out information, any help is appreciated as I
ID: 3655283 • Letter: N
Question
Need to modify this file to print out information, any help is appreciated as I am new to printing out information to another file. This is the original file.
public class University {
// Properties
private String name;
private String currentTerm; // Fall/Spring/etc.
private int year; // 2011...
private Course[] courses;
private Student[] students;
private Instructor[] instructors;
// Constructors
public University(String name, String currentTerm, int year, Student[] students, Instructor[] instructors, Course[] courses) {
this.name = name;
this.currentTerm = currentTerm;
this.year = year;
this.students = students;
this.courses = courses;
this.instructors = instructors;
}
// Get/set
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCurrentTerm() {
return currentTerm;
}
public void setCurrentTerm(String currentTerm) {
this.currentTerm = currentTerm;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public Course[] getCourses() {
return courses;
}
public void setCourses(Course[] courses) {
this.courses = courses;
}
public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
this.students = students;
}
public Instructor[] getInstructors() {
return instructors;
}
public void setInstructors(Instructor[] instructors) {
this.instructors = instructors;
}
// toString
public String toString() {
String str = "University Name: " + name;
//str += " Current Semester: " + currentTerm + " " + year;
str += " Current Term: " + currentTerm;
str += " Current Year: " + year;
str += " Number of Students: " + students.length;
str += " Number of Courses: " + courses.length;
str += " Number of Instructors: " + instructors.length;
return str;
}
}
I need to modify it to print out this information.
This part involves writing out all the information to a file. Modify the University.java file by adding this method:
Explanation / Answer
Since you didn't post Student.java or Course.java, i have no idea what methods these class have. But i will show you the right way to do that. Basically, we use PrintWriter, replace the methods accordingly (i.e. student.getDOB instead of student.getDob) :D public boolean writeToFile(String filename) { try { PrintWriter pw = new PrintWriter(new FileWriter(filename)); StringBuffer sb = new StringBuffer(); sb.append("University Name: " + name); sb.append(" Current Term: " + currentTerm); sb.append(" Current Year: " + year); sb.append(" Number of Students: " + students.length); sb.append(" Number of Courses: " + courses.length); sb.append(" Number of Instructors: " + instructors.length); sb.append(" "); sb.append(" Undergrad/Grad Student:"); for (Student s : students) { sb.append(" Name: " + s.getName()); sb.append(" D.O.B: " + s.getDob()); sb.append(" UFID: " + s.getUFID()); sb.append(" GPA: " + s.getGPA()); sb.append(" Number of Courses: " + s.getCourses().length); } sb.append(" "); sb.append(" Instructor:"); for (Instructor i : instructors) { sb.append(" Name: " + i.getName()); sb.append(" D.O.B: " + i.getDob()); sb.append(" UFID: " + i.getUFID()); } sb.append(" "); sb.append(" Course:"); for (Course c : courses) { sb.append(" " + c.getTitle()); sb.append(" " + c.getType() + " " + c.getNumber()); sb.append(" Number of Credits: " + c.getNumOfCredits()); sb.append(" Instructor: " + c.getInstructor().getUFID()); sb.append(" Number of TAs: " + c.getTAs().length); sb.append(" TA UFIDs: "); for (TA ta : c.getTAs()) { sb.append(" " + ta.getUFID()); } sb.append(" Capacity: " + c.getCapacity() + ";Enrollement: " + c.getCurrentEnrollment()); sb.append(" Student UFIDs:"); for (Student st : c.getStudents()) { sb.append(" " + st.getUFID()); } } pw.print(sb.toString()); pw.close(); return true; } catch (IOException e) { e.printStackTrace(); return false; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.