The Artima Developer Community
Sponsored Link

Java Answers Forum
I need help with this java application

1 reply on 1 page. Most recent reply: May 1, 2004 2:12 PM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Mustapha

Posts: 1
Nickname: sahara
Registered: May, 2004

I need help with this java application Posted: Apr 30, 2004 10:58 PM
Reply to this message Reply
Advertisement
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.*;
 
public class finalProject {
 
// to read keyboard input
private static BufferedReader br = null;
 
// main() method
public static void 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
public static String getLine() {
String inputLine = "";
 
try {
inputLine = br.readLine();
} catch (IOException e) {
// handle exception
}
 
return inputLine;
}
 
 
// displays array elements; where element is of type Name
private static void 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;
public int studentNumber = 0; //unique student id
public int 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;
}
public void setName( String newstrName)
{
strName = newstrName;
}
// get method for Score
public String getLabScore() {
 
return strLabScore;
}
public void setLabScore ( String newstrLabScore)
{
strLabScore = newstrLabScore;
}
//get method for Grade
public String getExamScore() {
return strExamScore;
}
public void setExamScore ( String newstrExamScore)
{
strExamScore = newstrExamScore;
}
public String getProjectScore() {
return strProjectScore;
}
public void setProjectScore( String newstrProjectScore)
{
strProjectScore = newstrProjectScore;
}
 
 
 
   // This method calculates the students grade in terms of a 
//percentage
 
 
public double 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;
}
 
public char getGrade()
{
getPercent();
char grade;
double finalGrade=0; 
if ( finalGrade > 80 )
 
         grade = 'A';
 
      else if ( finalGrade > 70 )
 
         grade = 'B';
 
      else if ( finalGrade > 60 )
 
         grade = 'C';
 
      else
 
         grade = 'F';          
return grade;
 
  } 
 
} // end of Student
 


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: I need help with this java application Posted: May 1, 2004 2:12 PM
Reply to this message Reply
MOST of the problem is here. Note the comments that I have added.
public double getPercent()
{
 
  double assignment=0, lab=0, exam=0, project=0, percent=0, finalGrade;
 
  assignment = assignment * 0.30; //Zero times .3 = Zero!
  lab = lab * 0.25;  //Zero times .25 = Zero!
  exam = exam * 0.15; //Zero times .15 = Zero!
  project =project * 0.30; //Zero times .3 = Zero!
  finalGrade = assignment + lab + exam + project; //Zero + Zero + Zero + Zero = Zero!
 
  return percent; // percent is initialized to Zero and never recalculated!!!!!
}


Here is my guess on what you may need to do.
public double getPercent()
{
 
  double assignment=0, lab=0, exam=0, project=0, percent=0, studentTotal, maxTotal;
 
  assignment = Double.parseDouble(strAssignmentScore) * 0.30;
  lab = Double.parseDouble(strLabScore) * 0.25;
  exam = Double.parseDouble(strExamScore) * 0.15;
  project = Double.parseDouble(strProjectScore) * 0.30;
  studentTotal = (assignment * 0.3) + (lab * 0.25) + (exam * 0.15) + (project * 0.3);
  maxTotal = assignment + lab + exam + project;
  percent = studentTotal / maxTotal;
 
  return percent;
}


You have a smaller problem in the getGrade() method. Here is the part with the problem. Again, note the added comments.
getPercent();  //gets the percent, but does nothing with it!
char grade;
double finalGrade=0; //set to zero, but never changed!
if ( finalGrade > 80 )

And here is how I think it needs to be fixed.
char grade;
double finalGrade = getPercent();  //the final grade IS the percent!
if ( finalGrade > 80 )


I hope this helps.
twc

Flat View: This topic has 1 reply on 1 page
Topic: java program help!! Previous Topic   Next Topic Topic: Open new frame with delay

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use