Sign in with
Sign up | Sign in
Your question
Closed

JAVA - student program

Last response: in Applications
Share
April 13, 2011 1:24:52 AM

Hey all writing a program that takes a students name, address, major, number of course taken, course name, credits , course grade. The course name, credits and grade go into an array. There a 3 classes, Person, Course and Student. Student inheritance from Person. Once again THANK YOU ALL!!!!!!

PROBLEMS
1. In Person Class for choice 4, when I ask user for the file name, it doesn't let prompt a user input line.

2. In Student Class for the method of LoadFromConsole, the "Please enter course name: " part, it doesn't give me the prompt to input, instead the output looks like this: "Please enter course name: Please enter credits: " and only takes the value for credits.

3. In Student Class, both the GradeReport methods, I am not sure how to print my courses array. The way I have it it prints something like Course@2ef552.

4. In Student Class I am confused on how I would go about totalling the number of credits and the avg grade.

5. In Student Class I am confused on how I would read from a file, that part that is confusing is how to get the course name, credits, grade into the Course[] array.
FORMAT OF THE FILE:
Arthur
10 Broadway
Computer Systems
4
BCS101 3 82
BCS345 3 98
BIO120 4 53
MLG300 3 87





Person Class
  1. import java.util.Scanner;
  2. import java.io.PrintStream;
  3.  
  4. public class Person {
  5. protected String name;
  6. protected String address;
  7.  
  8. public Person(){
  9. name = "NO NAME";
  10. address = "NO ADDRESS GIVEN";
  11. }
  12.  
  13. public Person(String n, String a){
  14. name = n;
  15. address = a;
  16. }
  17.  
  18. public String getName(){
  19. return name;
  20. }
  21.  
  22. public void setName(String newName){
  23. name = newName;
  24. }
  25.  
  26. public String getAddress(){
  27. return address;
  28. }
  29.  
  30. public void setAddress(String newAddress){
  31. address = newAddress;
  32. }
  33.  
  34.  
  35.  
  36. public static void main(String[] args) {
  37. int choice;
  38. String file;
  39.  
  40. Person p = new Person();
  41. Course c = new Course();
  42. Student s = new Student();
  43.  
  44. do
  45. {
  46. System.out.println("\n");
  47. System.out.println("Student Grades Program\n");
  48. System.out.println("--------------------\n");
  49. System.out.println("1. Load Grades From Console");
  50. System.out.println("2. Load Grades From File");
  51. System.out.println("3. Print Grades Report To Console");
  52. System.out.println("4. Print Grades Report To File");
  53. System.out.println("99. End Program\n");
  54. System.out.printf("Enter Choice: ");
  55.  
  56.  
  57.  
  58. //User input
  59. Scanner input = new Scanner(System.in);
  60. choice = input.nextInt();
  61.  
  62. System.out.println("\n");
  63.  
  64. //validation for the input
  65. while ((choice < 1 || choice > 4) && choice != 99)
  66. {
  67. System.out.println("Please enter a 1, 2, 3, or 4: " );
  68. choice = input.nextInt();
  69. }
  70.  
  71.  
  72. //switch menu for the choices
  73. switch (choice)
  74. {
  75. case 1:
  76. s.LoadFromConsole();
  77. break;
  78.  
  79. case 2:
  80.  
  81.  
  82. case 3:
  83. s.GradeReport();
  84. break;
  85.  
  86.  
  87. case 4:
  88. System.out.printf("Please enter file to be written to: ");
  89. file = input.nextLine();
  90. //s.GradeReport(file);
  91.  
  92. }
  93.  
  94. }
  95.  
  96. //When choice is 99 ends program
  97. while (choice != 99);
  98. System.out.println("Ending program...");
  99.  
  100.  
  101. }
  102. }


Course Class
  1. class Course {
  2. private String course;
  3. private int credits;
  4. private double grade;
  5.  
  6. public Course(){
  7. course = "NO COURSE NAME";
  8. credits = 0;
  9. grade = 0;
  10. }
  11.  
  12. public Course(String cn, int cr, double g){
  13. course = cn;
  14. credits = cr;
  15. grade = g;
  16. }
  17.  
  18. public String getCourse(){
  19. return course;
  20. }
  21.  
  22. public void setCourse(String newCourse){
  23. course = newCourse;
  24. }
  25.  
  26. public int getCredits(){
  27. return credits;
  28. }
  29.  
  30. public void setCredits(int newCredits){
  31. credits = newCredits;
  32. }
  33.  
  34. public double getGrade(){
  35. return grade;
  36. }
  37.  
  38. public void setGrade(double newGrade){
  39. grade = newGrade;
  40. }
  41. }


Student Class
  1. import java.util.Scanner;
  2. import java.io.PrintStream;
  3.  
  4. class Student extends Person {
  5. private String major;
  6. private int taken;
  7. private Course[] courses = new Course[4];
  8.  
  9. public Student(){
  10. super();
  11. major = "UNDECLARED";
  12. courses = new Course[100];
  13.  
  14. }
  15.  
  16. public void setCourses(int i, Course newCourses){
  17. courses[i] = newCourses;
  18.  
  19. }
  20.  
  21. public String getMajor(){
  22. return major;
  23. }
  24. public void setMajor(String newMajor){
  25. major = newMajor;
  26. }
  27.  
  28. public int getTaken(){
  29. return taken;
  30. }
  31. public void setTaken(int newTaken){
  32. taken = newTaken;
  33. }
  34.  
  35. public void LoadFromFile(String fileName){
  36.  
  37.  
  38. }
  39.  
  40. public void LoadFromConsole(){
  41.  
  42. Scanner input = new Scanner(System.in);
  43.  
  44. Course c = new Course();
  45.  
  46. System.out.printf("Please enter students name: ");
  47. String a = input.nextLine();
  48. setName(a);
  49. System.out.printf("Please enter students address: ");
  50. String b = input.nextLine();
  51. setAddress(b);
  52. System.out.printf("Please enter students major: ");
  53. String j = input.nextLine();
  54. setMajor(j);
  55. System.out.printf("Please enter the number of courses taken: ");
  56. taken = input.nextInt();
  57.  
  58.  
  59. for (int x = 0; x < taken; x++){
  60.  
  61. System.out.printf("Please enter course name: ");
  62. String cn = input.nextLine();
  63. c.setCourse(cn);
  64. System.out.printf("Please enter credits: ");
  65. int f = input.nextInt();
  66. c.setCredits(f);
  67. System.out.printf("Please enter course grade: ");
  68. double g = input.nextDouble();
  69. c.setGrade(g);
  70. setCourses(x,c);
  71. }
  72.  
  73. }
  74.  
  75. public void GradeReport(String fileName){
  76. PrintStream ps;
  77.  
  78. try{
  79.  
  80. ps = new PrintStream(fileName);
  81.  
  82. ps.printf("Student Name: %s\n",getName());
  83. ps.printf("Address: %s\n",getAddress());
  84. ps.printf("Major: %s\n",getMajor());
  85. ps.printf("# of Courses Taken: %d\n",taken);
  86. ps.printf("Course Credits Grade\n");
  87. for (int x = 0; x < taken; x++){
  88. ps.println(courses[x]);
  89. }
  90. ps.printf("----------------\n");
  91. ps.printf("Total Credits \n");
  92. ps.printf("Avg(numeric) \n");
  93. }
  94. catch (Exception e)
  95. {
  96. System.out.println("ERROR. Could not open file!");
  97. }
  98.  
  99. }
  100.  
  101. public void GradeReport(){
  102.  
  103. System.out.printf("Student Name: %s\n",getName());
  104. System.out.printf("Address: %s\n",getAddress());
  105. System.out.printf("Major: %s\n",getMajor());
  106. System.out.printf("# of Courses Taken: %d\n",taken);
  107. System.out.printf("Course Credits Grade\n");
  108. for (int x = 0; x < taken; x++){
  109. System.out.println(courses[x]);
  110. }
  111. System.out.printf("-------------------------------\n");
  112. System.out.printf("Total Credits \n");
  113. System.out.printf("Avg(numeric) \n");
  114. }
  115. }

More about : java student program

Best solution

a b L Programming
April 13, 2011 10:06:05 AM

1) I can't tell you without debugging through this code. Do you know how to use your IDE to debug through your code?

2) Again I'd need to debug.

3) You need to iterate over the array elements. For example:

StringBuilder b = new StringBuilder();
for(int i=0; i<courses.length; i++){
Course c = courses;
b.append(c.toString());
}


You should implement a toString method on Course

4) Like 3 you need to iterate over the array and add up all the Course credits. Average grade will just be the sum of the Course grades divided by the size of the course array.

5) This is a tricky file format. Who has defined this file format? Can you assume that the address is always two lines? XML would be a better structure for this.

I've included some comments about your classes. They are intended to be useful not damning. I appreciate you're new to Java! I'm just pointing out design issues and coding convention mistakes to try and help you. They shouldn't take too long to do. Some of it might confuse so feel free to ask about anything - I'll try and explain it better.


Comments:

The Person class:

1) Get into the habit in calling super() in every constructor. Even if you aren't extending anything (you are actually extending Object)

2) I wouldn't default the name or address to anything other than null or "". Presumably you've set it as something else so it's printed to the screen or to file by another method. That method should handle what null or "" means in this context. Not the data class.

3) This class should not have the main method. Whilst it will work it's not good practice. It's good to think about what a Class' responsibilities are. Your program has nothing to do with People, Students or Courses. If you were to use this Person class in another program would it contain the main method? Not at all.

Conceptually your program is a fourth class, maybe it's called StudentRecords or simply Runner. Look at your practical sheet for a good class name. I'm going to refer to this fourth class as Runner for now.

The Course Class:

1) Call super in the constructor.

2) Don't have a property of the class the same as the Class name. It will compile and run fine it's just confusing. A Course has a name not a course. So change that course field to name.

The Student Class:

1) The courses field is somewhat confusing here. Firstly you are using an array rather than a Collection. Secondly, you are defaulting to an array of length 4 but then in your constructor you create an array of length 100?

Consider using the Vector class instead. It's probably a little advanced if you are just learning Java but the basics would be:
private Vector<Course> courses = new Vector<Courses>();

Then you just call the add method on it to add additional Course objects. You don't need to care about it's size - it will manage all that for you.

2) Avoid defaulting major in the constructor. Let it be null or an empty string.

3) Taken doesn't need to be stored. You can derive it's value from the size of the courses structure. So courses.length (or courses.size() if you change to using a Vector). Either add a getCourses method that people can get the size of or remove the setTaken method and make the getTaken method do "return courses.length". You may want to check if the courses object / array is null if doing the latter.

4) Class method names always start with lowercase. LoadFromFile, LoadFromConsole and GradeReport are badly named. It will work and compile but it's against Java programming conventions.

5) I'm not convinced LoadFromFile, LoadFromConsole and GradeReport are methods that should be on the Student class. Consider putting these on the Runner class instead.

6) Don't force the caller of setCourses to define the index in the array. Make a method called addCourse which takes a course. This method will add it to the end of the array. Using a Vector for your courses pays off here because you can just add the Course without fear of the array being too small.

The Runner Class

1) Contains the main method. This method creates a new instance of Runner and calls the run method. No reason to store it; doing new Runner().run() is acceptable.

2) The run method contains what you had in your previous main method.

3) The LoadFromFile, LoadFromConsole and GradeReport methods from the Student class are now on this class.

4) You've hardcoded some of these "magic" input values and used the values in the multiple places. For example you've made an input of 1 mean "load grades from console". I'd define this input as a static constant. I.e. in the runner class add:

public static const int LOAD_FROM_CONSOLE = 1;

Then use this constant in your main method:

i.e.
System.out.println(LOAD_FROM_CONSOLE+". Load Grades From Console" );

and

switch (choice){
case LOAD_FROM_CONSOLE:
s.LoadFromConsole();
break;
...
}


So why go to all that bother? Well it means that you are using the exact value everywhere. It's more readable and quite importantly if you suddenly decide the input needs to be 5 instead you just need to change the constant rather than the millions of places it's hardcoded.
Share
a b L Programming
April 17, 2011 12:56:30 PM

Is your code working now?
Score
0
Related resources
a b L Programming
April 18, 2011 10:08:45 AM

Yeah made any progress buddy?
Score
0
April 25, 2011 1:02:51 AM

Sorry for the lack of response, yea I got it to work, met up with my professor and was very simple after he explained it. Got a 100 on it =D.

BTW this is the best site I have ever found!!!!!!!!!!! Love all the help everyone gives.
THANK YOU to everyone
Score
0
April 25, 2011 1:03:01 AM

Best answer selected by laserpp.
Score
0
a b L Programming
April 25, 2011 11:29:54 AM

Well done buddy glad you got such an awesome grade - keep it up!
Score
0
June 27, 2011 12:14:34 AM

This topic has been closed by Area51reopened
Score
0
!