I want to create a Java application, incorporating a class that I define. I should create a Student Class that ~includes attributes: name,id,assignmentPercent,labPercent,examPercent, projectPercent. ~includes methods: accessor methods for each attribute. mutator method to assign values to a Student object from the program. default constructor to initialize attributes. calculate percentage method that returns a value between 0 and 100 based upon the following: assignments - 10% labs - 35% exams - 45% project - 10%
~include a function to input information from the user for a Student object. ~include a function to calculate the student's grade and determine the corresponding letter grade. ~include a function to output the student's name and letter grade. ~include an array of Student objects to store in memory a list of students in a class.
Here is what I coded. the program compiles without errors. But, it's not calculating the final grades correctly and it's not assigning letter grades proparly. Please help.
import java.io.*;
publicclass finalProject {
// to read keyboard input
privatestatic BufferedReader br = null;
// main() method
publicstaticvoid main(String[] args) {
// declare an array of elements of name type
Student[] nameArr;
// set numOfNames to 0; later it will be set to what user enters
int numOfNames = 0;
// string variables to read name and phone from user
String strName;
String strID;
String strAssignmentScore;
String strLabScore;
String strExamScore;
String strProjectScore;
// create reader
br = new BufferedReader(new InputStreamReader(System.in), 1);
// get number of names to be created from user.
// this way you dont have to create an array of some large
//size and waste space
System.out.println ("\nPlease enter number of Names to be entered:");
// get number from user by calling getLine method and
//converting input string to integer
numOfNames = Integer.parseInt(getLine());
// create array with the size to match the number user entered
nameArr = new Student[numOfNames];
// initialize name array with the data from the user
for (int i = 0; i < nameArr.length; i++) {
System.out.println("\n\nPlease enter name of student " + (i+1) + ".");
strName = getLine();
System.out.println("Please enter the ID for Student " + (i+1) + ".");
strID = getLine();
System.out.println("Please enter the total Assignment Score for Student " + (i+1) + ".");
strAssignmentScore = getLine();
System.out.println("Please enter the total Lab Score for Student " + (i+1) + "." );
strLabScore = getLine();
System.out.println("Please enter the total ExamScore for Student " + (i+1) + ".");
strExamScore = getLine();
System.out.println("Please enter the total Project Score for Student " + (i+1) + ".");
strProjectScore = getLine();
// create name object for this person and assign to array
nameArr[i] = new Student (strName, strID, strAssignmentScore, strLabScore, strExamScore, strProjectScore);
}
// print values of name array to show what user has entered.
// first produce extra lines for readability
System.out.println("\n=========================================");
System.out.println("You entered data for " + nameArr.length + " name(s)");
// call method to display array
displayStudent(nameArr);
System.out.println("=========================================\n");
} // end of main()
// a method to help in reading input line from user
publicstatic String getLine() {
String inputLine = "";
try {
inputLine = br.readLine();
} catch (IOException e) {
// handle exception
}
return inputLine;
}
// displays array elements; where element is of type Name
privatestaticvoid displayStudent(Student[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println("Name: " + arr[i].getName() +
", Score: " + arr[i].getPercent() + ", Grade: "
+arr[i].getGrade());
}
} // end of displayStudent()
} // end of class NameArr
// Name class; with this blue print we can create as many names as we need...
class Student {
// data members of Name class
private String strName;
private String strID;
private String strAssignmentScore;
private String strLabScore;
private String strExamScore;
private String strProjectScore;
publicint studentNumber = 0; //unique student id
publicint studentMark = 0; //the mark the student received out of 100
public String strGrade;
double percentage;
// default constructor
public Student () {
strName = "";
strID = "";
strAssignmentScore = "";
strLabScore = "";
strExamScore = "";
strProjectScore = "";
}
// constructor with 3 parameters
public Student (String strName, String strID, String strAssignmetScore,
String strLabScore, String strExaScore, String strProjectScore) {
this.strName = strName;
this.strID = strID;
this.strAssignmentScore = strAssignmentScore;
this.strLabScore = strLabScore;
this.strExamScore = strExamScore;
this.strProjectScore = strProjectScore;
}
// get method for Name
public String getName() {
return strName;
}
publicvoid setName( String newstrName)
{
strName = newstrName;
}
// get method for Score
public String getLabScore() {
return strLabScore;
}
publicvoid setLabScore ( String newstrLabScore)
{
strLabScore = newstrLabScore;
}
//get method for Grade
public String getExamScore() {
return strExamScore;
}
publicvoid setExamScore ( String newstrExamScore)
{
strExamScore = newstrExamScore;
}
public String getProjectScore() {
return strProjectScore;
}
publicvoid setProjectScore( String newstrProjectScore)
{
strProjectScore = newstrProjectScore;
}
// This method calculates the students grade in terms of a
//percentage
publicdouble getPercent()
{
double assignment=0, lab=0, exam=0, project=0, percent=0, finalGrade;
assignment = assignment * 0.30;
lab = lab * 0.25;
exam = exam * 0.15;
project =project * 0.30;
finalGrade = assignment + lab + exam + project;
return percent;
}
publicchar getGrade()
{
getPercent();
char grade;
double finalGrade=0;
if ( finalGrade > 80 )
grade = 'A';
elseif ( finalGrade > 70 )
grade = 'B';
elseif ( finalGrade > 60 )
grade = 'C';
else
grade = 'F';
return grade;
}
} // end of Student