Question

I'm trying to write a java program that will read from two separate text files(course data...

I'm trying to write a java program that will read from two separate text files(course data and class data) and output a result. There will be 3 java files and two txt files. I'll list what I'm trying to do for each:

student.java - student object class

  • Attributes:
    • student name
    • major
    • class name
    • course id
    • grade
    • credits

course.java - course object class

  • Attributes:
    • course id
    • instructor id
    • room id

testStudent.java - class that will create the output

  • Create a report that has appropriate headings and 1 line of detail for each student record read in. Detail lines should include the following information:
    • student name,
    • class id,
    • Instructor id,
    • room id,
    • grade, - comment "good work if grade is A"
    • credits,
    • comment

-------------------------------------------------------------------------------------------

text files

--------------------------------------------------------------------------------------

classdata.txt

1001
Intro. to CompSci   
4
ALBERT, PETER A.    
Comp Info System    
A
1001
Intro. to CompSci   
4
ALLENSON, SHEILA M. 
Comp Info System    
B
1001
Intro. to CompSci   
4
ANDERSON, ALENE T.  
Comp Info System    
A
1001
Intro. to CompSci   
4
HENDRIX, JAMES D.   
Lib Arts - MIS      
C
1001
Intro. to CompSci   
4
CANNON, FREDDY B.B. 
Comp Info System    
B
1002
Visual Basic        
3
ALBERT, PETER A.    
Comp Info System    
C
1002
Visual Basic        
3
ALLENSON, SHEILA M. 
Comp Info System    
D
1002
Visual Basic        
3
ANDERSON, ALENE T.  
Comp Info System    
A
1002
Visual Basic        
3
HENDRIX, JAMES D.   
Lib Arts - MIS      
B
1002
Visual Basic        
3
CANNON, FREDDY B.B. 
Comp Info System    
B
1003
Cisco Networking I  
4
ALBERT, PETER A.    
Comp Info System    
C
1003
Cisco Networking I  
4
ALLENSON, SHEILA M. 
Comp Info System    
A
1003
Cisco Networking I  
4
ANDERSON, ALENE T.  
Comp Info System    
A
1003
Cisco Networking I  
4
HENDRIX, JAMES D.   
Lib Arts - MIS      
D
1004
Cisco Networking III
3
ALBERT, PETER A.    
Comp Info System    
B
1004
Cisco Networking III
3
ALLENSON, SHEILA M. 
Comp Info System    
C
1004
Cisco Networking III
3
ANDERSON, ALENE T.  
Comp Info System    
A
1004
Cisco Networking III
3
CANNON, FREDDY B.B. 
Comp Info System    
B
1004
Cisco Networking III
3
HELLER, HELEN H.    
Lib Arts - MIS      
A
1004
Cisco Networking III
3
HENDRIX, JAMES D.   
Lib Arts - MIS      
F

coursedata.txt

1001
WATNE          
BABCOCK134
1002
KROSHUS        
HORTON30
1003
SCHILLINGER    
LIBRARY118
1004
SCHWANTZ       
BABCOCK134
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution summary :

In total four classes created :

1)Course.java 2)Student.java 3)StudentTest.java 4)TestStudent.java

StudentTest.java is an additional POJO created in order to collect required information of a student in mentioned manner.

Please read comments for the explanation of each method and class

NOTE : Change the .txt file path as per you system

Sample output:

Source code :

--------------------------------------------------------------------------------------------------

Course.java

package org.bnymellon.students;

public class Course {
   private int courseId;
   private String instructorId;
   private String roomId;
   public Course() {
       super();
       // TODO Auto-generated constructor stub
   }
   public Course(int courseId, String instructorId, String roodId) {
       super();
       this.courseId = courseId;
       this.instructorId = instructorId;
       this.roomId = roodId;
   }
   public int getCourseId() {
       return courseId;
   }
   public void setCourseId(int courseId) {
       this.courseId = courseId;
   }
   public String getInstructorId() {
       return instructorId;
   }
   public void setInstructorId(String instructorId) {
       this.instructorId = instructorId;
   }
   public String getRoomId() {
       return roomId;
   }
   public void setRoomId(String roodId) {
       this.roomId = roodId;
   }
   @Override
   public String toString() {
       return "Course [courseId=" + courseId + ", instructorId=" + instructorId + ", roodId=" + roomId + "]";
   }
  

}

Student.java

package org.bnymellon.students;

public class Student {
   private String studentName;
   private String major;
   private String className;
   private int courseId;
   private String grade;
   private int credits;
   public Student() {
       super();
       // TODO Auto-generated constructor stub
   }
   public Student(String studentName, String major, String className, int courseId, String grade, int credits) {
       super();
       this.studentName = studentName;
       this.major = major;
       this.className = className;
       this.courseId = courseId;
       this.grade = grade;
       this.credits = credits;
   }
   public String getStudentName() {
       return studentName;
   }
   public void setStudentName(String studentName) {
       this.studentName = studentName;
   }
   public String getMajor() {
       return major;
   }
   public void setMajor(String major) {
       this.major = major;
   }
   public String getClassName() {
       return className;
   }
   public void setClassName(String className) {
       this.className = className;
   }
   public int getCourseId() {
       return courseId;
   }
   public void setCourseId(int courseId) {
       this.courseId = courseId;
   }
   public String getGrade() {
       return grade;
   }
   public void setGrade(String grade) {
       this.grade = grade;
   }
   public int getCredits() {
       return credits;
   }
   public void setCredits(int credits) {
       this.credits = credits;
   }
   @Override
   public String toString() {
       return "Student [studentName=" + studentName + ", major=" + major + ", className=" + className + ", courseId="
               + courseId + ", grade=" + grade + ", credits=" + credits + "]";
   }
  

}

StudentTest.java

package org.bnymellon.students;

public class StudentTest {
   private String studentName;
   private int courseId;
   private String className;
   private String roomId;
   private String instructorId;
   private String grade;
   private int credits;
   private String comment;
   public StudentTest() {
       super();
       // TODO Auto-generated constructor stub
   }
   public StudentTest(String studentName, int courseId, String className, String roomId, String instructorId,
           String grade, int credits, String comment) {
       super();
       this.studentName = studentName;
       this.courseId = courseId;
       this.className = className;
       this.roomId = roomId;
       this.instructorId = instructorId;
       this.grade = grade;
       this.credits = credits;
       this.comment = comment;
   }
   public String getStudentName() {
       return studentName;
   }
   public void setStudentName(String studentName) {
       this.studentName = studentName;
   }
   public int getCourseId() {
       return courseId;
   }
   public void setCourseId(int courseId) {
       this.courseId = courseId;
   }
   public String getClassName() {
       return className;
   }
   public void setClassName(String className) {
       this.className = className;
   }
   public String getRoomId() {
       return roomId;
   }
   public void setRoomId(String roomId) {
       this.roomId = roomId;
   }
   public String getInstructorId() {
       return instructorId;
   }
   public void setInstructorId(String instructorId) {
       this.instructorId = instructorId;
   }
   public String getGrade() {
       return grade;
   }
   public void setGrade(String grade) {
       this.grade = grade;
   }
   public int getCredits() {
       return credits;
   }
   public void setCredits(int credits) {
       this.credits = credits;
   }
   public String getComment() {
       return comment;
   }
   public void setComment(String comment) {
       this.comment = comment;
   }
   @Override
   public String toString() {
       return "StudentTest [studentName=" + studentName + ", courseId=" + courseId + ", className=" + className
               + ", roomId=" + roomId + ", instructorId=" + instructorId + ", grade=" + grade + ", credits=" + credits
               + ", comment=" + comment + "]";
   }
  
  
}

TestStudent.java

package org.bnymellon.students;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestStudent {
   public static void main(String[] args) {
       List<Course> courses=getCourseList();
       List<Student> students= getStudents();
       List<StudentTest> studentTests = getStudentTesting(courses,students);
       for(StudentTest st:studentTests) {
           System.out.println(st);
       }
      
   }
  
   //Processes lists of Course and Student and creates desired List of StudentTest
   public static List<StudentTest> getStudentTesting(List<Course> courses,List<Student> students){
       List<StudentTest> returnList = new ArrayList<StudentTest>();
       for(Student s:students) {
           for(Course c: courses) {
               if(s.getCourseId() == c.getCourseId()) {
                   StudentTest temp = new StudentTest();
                   temp.setStudentName(s.getStudentName());
                   temp.setGrade(s.getGrade());
                   temp.setCredits(s.getCredits());
                   temp.setClassName(s.getClassName());
                   temp.setCourseId(c.getCourseId());
                   temp.setInstructorId(c.getInstructorId());
                   temp.setRoomId(c.getRoomId());
                   if(s.getGrade().equals("A")) { //this condition is for creating comments according to the Grade
                       temp.setComment("Great job");
                   }else {
                       temp.setComment("-");
                   }
                   returnList.add(temp);
                   break;
               }
           }
       }
       return returnList;
   }

  
   //Reads the classdata.txt file and convers the info into List of Student and returns it to main method
   public static List<Student> getStudents(){
       List<Student> returnList = new ArrayList<Student>();
       BufferedReader reader;
       try {
           reader = new BufferedReader(new FileReader(
                   "C:\\Users\\pranav\\eclipse-workspace\\interview1\\secondHibernate\\src\\main\\java\\org\\bnymellon\\students\\classdata.txt"));
           String line = reader.readLine();
           while (line != null) {
               Student tempStudent = new Student();
               tempStudent.setCourseId(Integer.parseInt(line));
               line = reader.readLine();
               tempStudent.setClassName(line);
               line = reader.readLine();
               tempStudent.setCredits(Integer.parseInt(line));
               line = reader.readLine();
               tempStudent.setStudentName(line);
               line = reader.readLine();
               tempStudent.setMajor(line);
               line = reader.readLine();
               tempStudent.setGrade(line);
               line =reader.readLine();
               returnList.add(tempStudent);
           }
           reader.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
       return returnList;
   }
  
  
   //Reads the coursedata.txt file and convers the info into List of courses and returns it to main method
   public static List<Course> getCourseList(){
       List<Course> returnList = new ArrayList<Course>();
       BufferedReader reader;
       try {
           reader = new BufferedReader(new FileReader(
                   "C:\\Users\\pranav\\eclipse-workspace\\interview1\\secondHibernate\\src\\main\\java\\org\\bnymellon\\students\\coursedata.txt"));
           String line = reader.readLine();
           while (line != null) {
               Course tempCourse = new Course();
               tempCourse.setCourseId(Integer.parseInt(line));
               // read next line
               line = reader.readLine();
               tempCourse.setInstructorId(""+line);
               line = reader.readLine();
               tempCourse.setRoomId(""+line);
               returnList.add(tempCourse);
               line = reader.readLine();
           }
           reader.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
       return returnList;
   }

}

-----------------------------------------------------------------------------------------------------

TextFiles Used:

classdata.txt

1001
Intro. to CompSci   
4
ALBERT, PETER A.
Comp Info System
A
1001
Intro. to CompSci   
4
ALLENSON, SHEILA M.
Comp Info System
B
1001
Intro. to CompSci   
4
ANDERSON, ALENE T.
Comp Info System
A
1001
Intro. to CompSci   
4
HENDRIX, JAMES D.   
Lib Arts - MIS
C
1001
Intro. to CompSci   
4
CANNON, FREDDY B.B.
Comp Info System
B
1002
Visual Basic
3
ALBERT, PETER A.
Comp Info System
C
1002
Visual Basic
3
ALLENSON, SHEILA M.
Comp Info System
D
1002
Visual Basic
3
ANDERSON, ALENE T.
Comp Info System
A
1002
Visual Basic
3
HENDRIX, JAMES D.   
Lib Arts - MIS
B
1002
Visual Basic
3
CANNON, FREDDY B.B.
Comp Info System
B
1003
Cisco Networking I
4
ALBERT, PETER A.
Comp Info System
C
1003
Cisco Networking I
4
ALLENSON, SHEILA M.
Comp Info System
A
1003
Cisco Networking I
4
ANDERSON, ALENE T.
Comp Info System
A
1003
Cisco Networking I
4
HENDRIX, JAMES D.   
Lib Arts - MIS
D
1004
Cisco Networking III
3
ALBERT, PETER A.
Comp Info System
B
1004
Cisco Networking III
3
ALLENSON, SHEILA M.
Comp Info System
C
1004
Cisco Networking III
3
ANDERSON, ALENE T.
Comp Info System
A
1004
Cisco Networking III
3
CANNON, FREDDY B.B.
Comp Info System
B
1004
Cisco Networking III
3
HELLER, HELEN H.
Lib Arts - MIS
A
1004
Cisco Networking III
3
HENDRIX, JAMES D.   
Lib Arts - MIS
F

coursedata.txt

1001
WATNE
BABCOCK134
1002
KROSHUS
HORTON30
1003
SCHILLINGER
LIBRARY118
1004
SCHWANTZ   
BABCOCK134

Add a comment
Know the answer?
Add Answer to:
I'm trying to write a java program that will read from two separate text files(course data...
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
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