//this is given

public class Student {


private String name;
private String address;
private String phone;
private String major;


String[ ] classesTaken = new String[100];
double[ ] gradesReceived = new double[ 100];


public void addClassesTaken(String classID) {}
public void addGradeReceived(float classGrade) {}
public void changeGradesReceived(String classID,
double newGrade) {}
public void computeGPA( ) {}


}

I need to....

1 - randomly fill the gradesReceived array with random variables between 1 and 100
2 - populate the classesTaken array with empty strings
3 - have the computeGPA method throw a DivideByZeroException if no grades are entered

4 - rewrite the code below to include try, catch, and finally blocks

public double computeGPA () {


double sum = 0.0;
for (int i = 0; i <100; I++) {
if (!classesTaken.equals(" ")) {
sum += gradesReceived;
}
}
return sum / (double)i;


}

5 - the catch block should should print out an error message (nonGUI) and return zero as the GPA , and the finally block should print out a message (nonGUI) indicating the method finished
6 - write a main method that instantiates an object of the student class and calls the computeGPA method

Recommended Answers

All 8 Replies

Well, you didn't ask any questions. You just posted your assignment, and certainly no one is going to just write it for you.

Post the code that you have written for the assignment and specific questions or error messages that are giving you trouble.

Sorry that I was unclear. It was not my intentions to have someone just write it for me.

I am having problems with the section 4 and 5 using the try, catch, and finally blocks. I don't understand how I would use them in trying to compute the GPA. Basically I don't know where to start when writing this portion. I think if I can get help with the try block that I could probably figure the rest out.

Thanks.

the catch block should should print out an error message (nonGUI) and return zero as the GPA , and the finally block should print out a message (nonGUI) indicating the method finished

You will put the code that calculates the GPA inside the try. Inside the catch you will print an error message and give the GPA value 0 and inside finally you will again print whatever is required from you.
I would suggest to put the entire code inside the try just to be sure. Then have a variable declared outside the try. Inside the try calculate the value and put it inside it. Inside the catch you will have it take value 0. And at the end you will return it:

double ret=0;
try {
  //calcucate GPA

  ret= //the value calculated
} catch (Exception e) {
  ret=0;
} finally {

}
return ret;

Thanks for your response. Here is what I have so far, it was done before your response so I will have to go back and try it again because I have several errors.

public class Student {


private String name;
private String address;
private String phone;
private String major;


String[ ] classesTaken = new String[100];


///here are the getters and setters, not sure if they're correct, but no errors


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


public String getName() {
return name;
}


public void setAddress(String address) {
this.address = address;
}


public String getAddress() {
return address;
}


public void setPhone(String phone) {
this.phone = phone;
}


public String getPhone() {
return phone;


}


public void setMajor(String major) {
this.major = major;
}


public String getMajor() {


return major;
}


int[ ] grades;
int sum;


double[ ] gradesReceived = new double[ 100];


//this is to get a random value between 1 and 100, not sure if it's right, but no error here


public void randomize()
{
for (int i=0; i<grades.length; i++)
grades = (int)(Math.random() * 100) + 1;
}



String classID;
float classGrade;
double newGrade;


public void addClassesTaken(String classID) {


//i dont know exactly what to put here


}
public void addGradeReceived(float classGrade) {


//dont know what to put here  


}
}


public void changeGradesReceived(String classID,
double newGrade) {


//not sure what to put here
}


///here's my try, catch, and finally and it has tons or errors


public double computeGPA (){
int studentGPA;


try
{
studentGPA = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter your classes taken"));


}



catch(DivideByZeroException e){
System.err.println("Threw the Exception" +e.getMessage());
e.printStackTrace();
}


finally
{
if(classesTaken != null){
try {
gradesReceived.close();
} catch (DivideByZeroException e) {


}
}
System.out.println("--- File End ---");
}
}


}

I still cant figure out how to make

double sum = 0.0;
for (int i = 0; i <100; i++) {
if (!classesTaken.equals(" ")) {
sum += gradesReceived;
}

into try, catch, and finally blocks correctly.

still needing help...

gradesReceived as I can see is an array. Why do you call gradesReceived.close() ? I don't think there is such method and if it is then you don't need it.

Do you know what divided by zero means. In case you don't know English it happens when you are doing something like this 2/0. So you need to add the try-catch block where you trying to divide something, probably where you calculate the average.

Rewrite computeGPA without the try-catches. You probably get an error because you try to catch DividedByZeroException when you should be catching NumberFormatException:

try {
  int i = Integer.parseInt( . . . );
} catch (NumberFormatException nfe) {
}

This:

double sum = 0.0;
for (int i = 0; i <100; i++) {
if (!classesTaken.equals(" ")) {
sum += gradesReceived;
}

Doesn't need a try-catch. When you divide for the average you will need the try-catch

You have a grades and a gradesReceived array. What is the difference between these two?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.