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
Course Class
Student Class
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");
}
}