JAVA - student program

Status
Not open for further replies.

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
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
Code:
import java.util.Scanner;
import java.io.PrintStream;
 
public class Person {
	protected String name;
	protected String address;
	
	public Person(){
		name = "NO NAME";
		address = "NO ADDRESS GIVEN";
	}
	
	public Person(String n, String a){
		name = n;
		address = a;
	}
	
	public String getName(){
		return name;
	}
	
	public void setName(String newName){
		name = newName;
	}
	
	public String getAddress(){
		return address;
	}
	
	public void setAddress(String newAddress){
		address = newAddress;
	}
	
	
    
    public static void main(String[] args) {
    	int choice;
    	String file;
    	
    	Person p = new Person();
    	Course c = new Course();
    	Student s = new Student();
    	
    	do
    		{
    			System.out.println("\n");
    			System.out.println("Student Grades Program\n");
    			System.out.println("--------------------\n");
    			System.out.println("1.  Load Grades From Console");
    			System.out.println("2.  Load Grades From File");
    			System.out.println("3.  Print Grades Report To Console");
    			System.out.println("4.  Print Grades Report To File");
    			System.out.println("99. End Program\n");
    			System.out.printf("Enter Choice: ");
    			
    			
    			
    			//User input
    			Scanner input = new Scanner(System.in);
    			choice = input.nextInt();
    			
    			System.out.println("\n");
    			
    			//validation for the input
    			while ((choice < 1 || choice > 4) && choice != 99)
    			{
    				System.out.println("Please enter a 1, 2, 3, or 4: " );
    				choice = input.nextInt();
    			}
    			
    	
    				//switch menu for the choices
    				switch (choice)
    				{
    					case 1: 
    						s.LoadFromConsole();
    						break;
    			
    					case 2:
    					
    			
    					case 3:
    						s.GradeReport();
    						break;
    						
    			
    					case 4:
    						System.out.printf("Please enter file to be written to: ");
    						file = input.nextLine();
    						//s.GradeReport(file);
    					
    				}
    	
    		}
    		
    		//When choice is 99 ends program
    		while (choice != 99);
    		System.out.println("Ending program...");
    	
    
    }
}

Course Class
Code:
class Course {
	private String course;
	private int credits;
	private double grade;
	
	public Course(){
		course = "NO COURSE NAME";
		credits = 0;
		grade = 0;
	}
	
	public Course(String cn, int cr, double g){
		course = cn;
		credits = cr;
		grade = g;
	}
	
	public String getCourse(){
		return course;
	}
	
	public void setCourse(String newCourse){
		course = newCourse;
	}
	
	public int getCredits(){
		return credits;
	} 
	
	public void setCredits(int newCredits){
		credits = newCredits;
	}
	
	public double getGrade(){
		return grade;
	}
	
	public void setGrade(double newGrade){
		grade = newGrade;
	}
}

Student Class
Code:
import java.util.Scanner;
import java.io.PrintStream;

class Student extends Person {
	private String major;
	private int taken;
	private Course[] courses = new Course[4];
	
	public Student(){
		super();
		major = "UNDECLARED";
		courses = new Course[100];
		
	}
	
	public void setCourses(int i, Course newCourses){
		courses[i] = newCourses;
		
	}
	
	public String getMajor(){
		return major;
	}
	public void setMajor(String newMajor){
		major = newMajor;
	}
	
	public int getTaken(){
		return taken;
	}
	public void setTaken(int newTaken){
		taken = newTaken;
	}
	
	public void LoadFromFile(String fileName){
		

	}
	
	public void LoadFromConsole(){
		
		Scanner input = new Scanner(System.in);
		
		Course c = new Course();
		
		System.out.printf("Please enter students name: ");
		String a = input.nextLine();
		setName(a);
		System.out.printf("Please enter students address: ");
		String b = input.nextLine();
		setAddress(b);
		System.out.printf("Please enter students major: ");
		String j = input.nextLine();
		setMajor(j);
		System.out.printf("Please enter the number of courses taken: ");
		taken = input.nextInt();
		
		
		for (int x = 0; x < taken; x++){
			
			System.out.printf("Please enter course name: ");
			String cn = input.nextLine();
			c.setCourse(cn);
			System.out.printf("Please enter credits: ");
			int f = input.nextInt();
			c.setCredits(f);
			System.out.printf("Please enter course grade: ");
			double g = input.nextDouble();
			c.setGrade(g);
			setCourses(x,c);
		}
	
	}
	
	public void GradeReport(String fileName){
		PrintStream ps;
		
		try{
			
			ps = new PrintStream(fileName);
		
			ps.printf("Student Name: %s\n",getName());
			ps.printf("Address: %s\n",getAddress());
			ps.printf("Major: %s\n",getMajor());
			ps.printf("# of Courses Taken: %d\n",taken);
			ps.printf("Course		Credits Grade\n");
			for (int x = 0; x < taken; x++){
				ps.println(courses[x]);
			}
			ps.printf("----------------\n");
			ps.printf("Total Credits	\n");	
			ps.printf("Avg(numeric)		\n");	
		}
		catch (Exception e)
		{
		System.out.println("ERROR. Could not open file!");
		}
		
	}
	
	public void GradeReport(){
		
		System.out.printf("Student Name: %s\n",getName());
		System.out.printf("Address: %s\n",getAddress());
		System.out.printf("Major: %s\n",getMajor());
		System.out.printf("# of Courses Taken: %d\n",taken);
		System.out.printf("Course		Credits Grade\n");
		for (int x = 0; x < taken; x++){
			System.out.println(courses[x]);
		}
		System.out.printf("-------------------------------\n");
		System.out.printf("Total Credits	\n");	
		System.out.printf("Avg(numeric)		\n");	
	}
}
 
Solution
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...

Rusting In Peace

Distinguished
Jul 2, 2009
312
0
19,060
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.
 
Solution

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
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
 
Status
Not open for further replies.