In: Computer Science
***IN JAVA***
Write a program contained a class Student which has firstName, lastName, mark, grade. The program should allow creation an array of object instances to assign firstName, lastName and mark from input user and perform and assign grade based on mark’s criteria displayed below.
MARKS INTERVAL |
95 - 100 |
90 - <95 |
85 - <90 |
80 - <85 |
75 - <80 |
70 - <75 |
65 - <70 |
60 - <65 |
0 - <60 |
LETTER GRADE |
A+ |
A |
B+ |
B |
C+ |
C |
D+ |
D |
F |
SAMPLE OF OUTPUT:
Enter section #: 1
How many students: 3
Enter student details:
First Name: John
Last Name: Kendall
Marks : 96
Enter student details:
First Name: Mary
Last Name: Kendall
Marks : 76
Enter student details:
First Name: Lucy
Last Name: Kendall
Marks : 76
Result for Section 1
-------------------------------------------------------------------
FirstName Last Name Grade
John Kendall A+
Mary Kendall C+
Lucy Kendall C+
Total students: 3
Average marks: 82.2 Average Grade: B
Highest: 96, by John Kendall
Location: 0
Another batch? (Yes – 1, No - 0) :> 1
Enter section #: 2
How many students: 6
Enter student details:
1)
First Name: John
Last Name: Kendall
Marks : 96
2)
First Name: Mary
Last Name: Kendall
Marks : 76
3)
First Name: Lucy
Last Name: Kendall
Marks : 76
4)
First Name: Martin
Last Name: Kendall
Marks : 86
5)
First Name: Mary
Last Name: Kendall
Marks : 76
6)
First Name: Lucy
Last Name: Kendall
Marks : 98
Result for Section 2
-------------------------------------------------------------------
FirstName Last Name Grade
John Kendall A+
Mary Kendall C+
Lucy Kendall C+
Martin Kendall B+
Mary Kendall C+
Lucy Kendall A+
Total students: 6
Average marks : 80.7 Average Grade: B
Highest: 98, by Lucy Kendall
Location: 6
Another batch? (Yes – 1, No - 0) :> 0
PROBLEM SOLVING TIPS:
Num |
Tips |
Grade |
a) |
Use class Student to create another class Students that will be built in the class implementing a Comparator. |
/1m |
b) |
Use for enhanced method to display the grade of student by each batches |
/1m |
c) |
In the main program, create sections of studlist, receive section number and number of students for each section. Use loop control structure to display output as shown in above and to assign input utilizing the studList structure. |
/3m |
d) |
For each section, program will control input firstname, lastname and marks received from user and add into StudList. |
/2m |
e) |
Use appropriate control structure to control input and program execution. |
/4m |
f) |
In main, use Arrays sort() method, to help find the maximum grade achieved by each class and display the name of the student. |
/3m |
g) |
In main, find the average performance (average section, mark and grade) of every batch and include in the output display. Formula : Average_mark = total/number of students; |
/2m |
h) |
Use solution for practical provided to extend the program to capable to process more than one sections of students to process. |
/ 4m |
TOTAL: |
/20m |
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// Student.java
import java.util.Comparator;
public class Student implements Comparator<Student>
{
private String firstName;
private String lastName;
private double mark;
public Student() {
}
/**
* @param firstName
* @param lastName
* @param mark
*/
public Student(String firstName, String lastName,
double mark) {
this.firstName = firstName;
this.lastName = lastName;
this.mark = mark;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName
* the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName
* the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the mark
*/
public double getMark() {
return mark;
}
/**
* @param mark
* the mark to set
*/
public void setMark(double mark) {
this.mark = mark;
}
public String gradeLetter(double mark) {
String letter = "";
if (mark >= 95 &&
mark <= 100)
letter =
"A+";
else if (mark >= 90 &&
mark <95)
letter =
"A-";
else if (mark >= 85 &&
mark <90)
letter =
"B+";
else if (mark >= 80 &&
mark <85)
letter =
"B";
else if (mark >= 75 &&
mark < 80)
letter =
"C+";
else if (mark >= 70 &&
mark < 75)
letter =
"C";
else if (mark >= 65 &&
mark < 70)
letter =
"D+";
else if (mark >= 60 &&
mark <65)
letter =
"D";
else if (mark <60)
letter =
"F";
return letter;
}
@Override
public int compare(Student s1, Student s2) {
if (s1.getMark() <
s2.getMark())
return 1;
else if (s1.getMark() >
s2.getMark())
return -1;
else
return 0;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
String s = new
String().format("%-15s%-15s%-15s", firstName,
lastName,gradeLetter(mark));
return s;
}
}
=========================================
// Students.java
import java.util.Arrays;
import java.util.Scanner;
public class Students {
public static void main(String[] args) {
int size, count = 0, maxIndex
=0;
String firstName, lastName;
double sum = 0.0, avg = 0.0;
Student s=null;
double max,mark;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
while (true) {
count++;
// Getting the
input entered by the user
System.out.println("Enter section #:" + (count));
System.out.print("How many students: ");
size =
sc.nextInt();
Student studList[] = new Student[size];
for (int i = 0;
i < studList.length; i++) {
System.out.println("Enter student
details:");
System.out.print("First Name:");
firstName = sc.next();
System.out.print("Last Name :");
lastName = sc.next();
System.out.print("Marks :");
mark = sc.nextInt();
s = new Student(firstName, lastName,
mark);
studList[i] = s;
}
Arrays.sort(studList,new Student());
System.out.println("Result for Section " + (count));
System.out.println("-----------------------------------------");
System.out.printf("%-15s%-15s%-15s\n", "FirstName",
"LastName","Grade");
max =
studList[0].getMark();
for (int
i=0;i<studList.length;i++)
{
sum += studList[i].getMark();
System.out.println(studList[i]);
if (max < studList[i].getMark())
{
max =
studList[i].getMark();
maxIndex = i;
}
}
avg=((double)sum/size);
System.out.println("Total Students:" + size);
System.out.printf("Average marks:%.1f\n" ,avg);
System.out.println("Average Grade:" +s.gradeLetter(avg));
System.out.println("Highest:" +max+", by
"+studList[maxIndex].getFirstName()+"
"+studList[maxIndex].getLastName()+" Location: "+maxIndex);
System.out.print("Another batch? (Yes - 1, No - 0):>");
int
choice=sc.nextInt();
if(choice==0)
break;
}
}
}
==========================================
Output:
==================================Thank You