Java Project Help

rowanB

Junior Member
Nov 4, 2004
14
0
0

Hi, I'm trying to write a program where the user enters his/her exam results
and the programme states whether the user has passed or failed. I am
supposed to write the program so that the public ModuleResult result()
method of the ModuleRecord class prints whether the users module result is a
pass, fail, or compensatable fail through accessing the ModuleResult class,
which I am not allowed to alter. I think that I have written the result()
method of the ModuleRecord class correctly to the extent that 'score' will
equal whatever 'result' will equal once a String has been entered into
private ModuleResult(String result). My problem is trying to write the
ModuleRecord class correctly so that the value of 'result' will be right.

As you can probably tell I'm a Java newbie, and I struggle to explain things
Java related clearly, but I hope one of you can understand my problem and
put me in the right direction.

Thanks.


public class ModuleRecord
{
private Module mod;
private int examMark;
private int courseMark;
private ModuleResult modResult;

public ModuleRecord(Module m, int eMark, int cMark)
{
mod = m;
examMark = eMark;
courseMark = cMark;

}

public ModuleResult result()
{
String score = modResult.toString();
return score;
}

} // End Class ModuleRecord

public class ModuleResult
{

private static String PASS_STRING = "Pass";
private static String COMPENSATABLE_FAIL_STRING = "Compensatable Fail";
private static String FAIL_STRING = "Fail";
private String result;

public static final int COMPENSATION_THRESHOLD = 35;
public static final int PASS_MARK = 40;

public static final ModuleResult PASS = new ModuleResult(PASS_STRING);

public static final ModuleResult COMPENSATABLE_FAIL = new ModuleResult(COMPENSATABLE_FAIL_STRING);

public static final ModuleResult FAIL = new ModuleResult(FAIL_STRING);

private ModuleResult(String result)
{
this.result = result;
}

public String toString()
{
return result;
}

}
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
A method signatures of:
"public ModuleResult result()"

Means you have a method called "result" that takes no parameters and returns something of the class ModuleResult.

Right now you're trying to return an object of class String in that method, which is why you're running into problems.

It looks like what you really want is something like this:

if (examMark > ModuleResult.PASS_MARK) {
return ModuleResult.PASS;
} else if (examMark > ModuleResult.COMPENSATION_THRESHOLD) {
return ModuleResult.COMPENSATABLE_FAIL;
} else {
return ModuleResult.FAIL;
}

instead of your current:
String score = modResult.toString();
return score;


 

rowanB

Junior Member
Nov 4, 2004
14
0
0
Can't thank you enough. I'm doing this for an important project and with the deadline nearing, I've been getting really stressed. You've helped me out of a real mess where I've been going wrong and much more complex route. Is there a way the result can be displayed through the same method without having to use to use 'inspect'?
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Originally posted by: rowanB
Can't thank you enough. I'm doing this for an important project and with the deadline nearing, I've been getting really stressed. You've helped me out of a real mess where I've been going wrong and much more complex route. Is there a way the result can be displayed through the same method without having to use to use 'inspect'?
The ModuleResult class overrides the toString() method to display its internal result String.

This means when you attempt to use a ModuleResult in a String concatination situation or when printing a ModuleResult that toString() method gets called implicitly.

Let's run through an example:

ModuleRecord mr = new ModuleRecord(someModule, 50, 50);
System.out.println(mr.result());

This actually prints:
"Pass"

Just as this:
ModuleRecord mr = new ModuleRecord(someModule, 10,10);
System.out.println(mr.result());

Prints:
"Fail"

You can also do something like this:
ModuleRecord mr = new ModuleRecord(someModule, 55, 50);
String someString = "For the module, someModule, your result was: " + mr.result();
System.out.println(someString);

This will print:
"For the module, someModule, your result was: Pass"

You can also explicitly call the toString() method of ModuleResult by doing something like
mr.result().toString();
Although in the most common use cases this will not be required.