-2

The program will compute the final grades of students. Each students holds the record of his/her Midterm grade, Second Quarter grade and Final exam. Final Grade will be 25% of Midterm grade, 30% of Second Quarter grade and 45% of the Final exam. The program will ask data for 10 student. Data for each Student consist of name, section, midterm grade, second quarter grade and final exam.

How will I display the grades of the student in a tabular form?

This is the code for gradesummary.java

public class gradesummary {
public static void main(String args[]){
    Scanner cin = new Scanner(System.in);
    Vector<Student> list = new Vector<>();

    for(int x=0; x<10 ;x++){
        Student s = new Student();
        System.out.print("Name: ");
        s.name = cin.nextLine();
        System.out.print("Section: ");
        s.section = cin.nextLine();
        System.out.print("Midterm Grade: ");
        s.midgrade = cin.nextDouble();
        System.out.print("Second Quarter: ");
        s.secondqtr = cin.nextDouble();
        System.out.print("Final Exam: ");
        s.finexam = cin.nextDouble();

        //save s in data collection
        list.add(s);

    }

    for(int x = 0; x<10; x++){
    //what will i do here next?
        Student xtt = list.get(x);
    }
}
}

This is the code for Student.java

public class Student {
//is this class correct?
String name;
String section;
Double midgrade;
Double secondqtr;
Double finexam;
Double fingrade;

public Student(){

}

public Student (String n, String s, Double mg, Double sq, Double fe, Double fg){
    name = n;
    section = s;
    midgrade = mg;
    secondqtr = sq;
    finexam = fe;
    fingrade = fg;      
}   

}

i want an output like this

Name        Midterm Grade    Second Quarter    Final Exam    Final Grade
Student2        80               79.69            85           87.674
Student2        78               77.69            81           79.257
Student3        88               87               89           88.15
Student4        88.67            84               86.78        86.4185   
Student5        98               90               94           93.8
Student6        93               91               90           91.05
Student7        84               78               87           83.55
Student8        82               86.4             89           86.47
Student9        72               79               81           78.15
Student10       85.34            81               83           82.985
  • 1
    Do you want the results displayed in a tabular form from the console, or in a UI of some kind? If you want to print in the console, [this might be a help to you](http://stackoverflow.com/questions/15215326/how-can-i-create-a-table-in-java-in-a-console) – Rudi Kershaw Mar 06 '14 at 13:43
  • @RudiKershaw i want the results displayed on the console of Eclipse. I use Eclipse when i do java programs. – Bloodymscre Mar 06 '14 at 13:49
  • You want to use Java's output formatting. See http://stackoverflow.com/questions/2745206/output-in-a-table-format-in-javas-system-out, for example. – Jim Mischel Mar 06 '14 at 22:23

3 Answers3

0

I advise that you read the data right into the student's field. To do that you can pass only the Scanner object to the Student constructor.

you will only need something like:

public Student (Scanner sc){
    name = sc.next();
    section = sc.next()
    .
    .
    . 
}

make sure to close the scanner when you're done reading student records.

You should also take a look at printf (an example) to print out tables.

Community
  • 1
  • 1
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
0

you should write something like this:

for(Student student: list){

    System.out.printf("%-8s   %-8s   %-8s  %-8s%n", "Name", "Midterm Grade", "Second Quarter", "Final Exam");

    System.out.printf("%1d  -8s   %7.1f   %7.1f   %7.1f%n", student.get...);
}

and the final grade math is:

float finalGrade = (student.get(FinalExam) * 45 /100 ) + (student.get(MidtermGrade) * 20 /100 ...)

dass
  • 1
  • 1
0

You'll notice a few changes but my take on this is:

public class GradeSummary {
    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        List<Student> list = new ArrayList<>();

        for (int x = 0; x < 10; x++) {
            Student s = new Student();
            System.out.print("Name: ");
            s.setName(cin.nextLine());
            System.out.print("Section: ");
            s.setSection(cin.nextLine());
            System.out.print("Midterm Grade: ");
            s.setMidterm(cin.nextDouble());
            System.out.print("Second Quarter: ");
            s.setSecondQuarter(cin.nextDouble());
            System.out.print("Final Exam: ");
            s.setFinalExam(cin.nextDouble());
            list.add(s);
        }

        for (Student student : list) {
            System.out.println(String.format("Name: %s, Final Grade: %s", student.getName(), student.getWeightedGrade()));
        }
    }
}

and

public class Student {
    private String name;
    private String section;
    private Double midterm;
    private Double secondQuarter;
    private Double finalExam;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public Double getMidterm() {
        return midterm;
    }

    public void setMidterm(Double midterm) {
        this.midterm = midterm;
    }

    public Double getSecondQuarter() {
        return secondQuarter;
    }

    public void setSecondQuarter(Double secondQuarter) {
        this.secondQuarter = secondQuarter;
    }

    public Double getFinalExam() {
        return finalExam;
    }

    public void setFinalExam(Double finalExam) {
        this.finalExam = finalExam;
    }

    public Double getWeightedGrade()
    {
        return this.midterm * .25 + this.secondQuarter * .30 + this.finalExam * .45;
    }
}
Mikkel Løkke
  • 3,710
  • 23
  • 37