The Artima Developer Community
Sponsored Link

Java Answers Forum
Object Help-URGENT

2 replies on 1 page. Most recent reply: Nov 20, 2006 6:48 AM by Matthias Neumair

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 2 replies on 1 page
Jessica Harden

Posts: 2
Nickname: dynamight
Registered: Nov, 2006

Object Help-URGENT Posted: Nov 15, 2006 9:57 AM
Reply to this message Reply
Advertisement
I'm very new to this and I need to write a class, CourseManager, that will use the object Student to store last and first names of students, as well as three exam grades and an ind. average.

My student object works and the code is:


public class Student {

static int nextID = 001;



// Instance variables
private String _lastName;
private String _firstName;
private double _exam1;
private double _exam2;
private double _exam3;
private double _examAvg;
private int _idNum;


/* Constructors*/

public Student() {
_lastName = " ";
_firstName = " ";
_exam1 = 0;
_exam2 = 0;
_exam3 = 0;
_examAvg = 0;//consider change if doesn't work in CourseManager
_idNum = nextID++;
}



/* accessor methods */

public void setLastName(String L_name) {
_lastName= L_name;
}

public void setFirstName(String F_name) {
_firstName = F_name;
}

public void setExamOne(double testOne) {
_exam1 = testOne;
}

public void setExamTwo(double testTwo) {
_exam2 = testTwo;
}

public void setExamThree(double testThree) {
_exam3 = testThree;
}

public double getExamAvg() {
_examAvg = Math.round((_exam1 + _exam2 + _exam3)/3);
return _examAvg;
}




public int getID() { return _idNum; }
public String getLastName() { return _lastName; }
public String getFirstName() { return _firstName; }
public double getExamOne() { return _exam1; }
public double getExamTwo() { return _exam2;}
public double getExamThree() {return _exam3;}

} // end Student


I have some really garbled code for Coursemanager and am having trouble figuring out what to do to make this work.

My code thus far is:

import java.io.*; // Import all classes in java.io package. Save typing.
public class CourseManager {

public static void main (String[] args) {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

int initialNumStudents = 0;
int maxStudents = 100;
int k = 0;//array index for arrays receiving user input
int i = 0;//array index for computing exam averages
int averageAll = 0;//allows to get data for all students for total average

Student[] = new Student(maxStudents);

for(k= 0; k < maxStudents; k++) {



System.out.print("What is the student's last name?");//allows user to enter last name
String line = in.readLine();
if ( line.equals( ".")) break;//allows user to exit loop
_lastName[k] = line;

System.out.print("What is the student's first name?");//allows user to enter first name
firstName[k] = in.readLine();

System.out.print("What is the student's first exam grade?");//allows user to enter 1st exam
exam1[k] = Double.parseDouble(in.readLine());

System.out.print("What is the student's second exam grade? ");//allows user to enter 2nd exam
exam2[k] = Double.parseDouble(in.readLine());

System.out.print("What is the student's third exam grade? ");//allows user to enter 3rd exam
exam3[k] = Double.parseDouble(in.readLine());

examAvg[k] = Math.round ((exam1[k] + exam2[k] + exam3[k])/3);//formula to average each students
//individual average

}

//The following block of code calculates the average exam score for ALL student data.
initialNumStudents = k;

for (i = 0; i <= (initialNumStudents - 1);i++) {

averageAll = averageAll + examAvg; }

averageAll = Math.round (averageAll / initialNumStudents);//method to average all scores

//This block of code prints the results into a readable table

//Table headers
System.out.println("Number\tStudent Name\t\tExam 1\tExam 2\tExam 3\tAvg Exam Score");

//Table information
for (i =0; i <=(initialNumStudents - 1); i++) {

System.out.println(((Student.getID()))+"\t\t"+ Student.getLastName()+"\t"
+Student.getFirstName()+"\t\t"+Student.getExamOne()"\t"
+Student.getExamTwo()+"\t" +Student.getExamThree()+"\t\t"+Student.getExamAvg());
}

System.out.println ("The average exam grade for all entered students is " + averageAll +".");


} // end main




}//end CourseManager


Any help is greatly appreciated!


Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Re: Object Help-URGENT Posted: Nov 17, 2006 3:51 AM
Reply to this message Reply
Please specify ur problem, witht those 2 blocks of code, I am unable to figure whats ur exact problem.

Rajeev

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Object Help-URGENT Posted: Nov 20, 2006 6:48 AM
Reply to this message Reply
... and use the [ java ] tag when posting code.

Flat View: This topic has 2 replies on 1 page
Topic: Object Help-URGENT Previous Topic   Next Topic Topic: Pong

Sponsored Links



Google
  Web Artima.com   

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