We've been learning about methods in java (using netbeans) in class and I'm still a bit confused about using methods. One homework question basically asks to design a grade calculator using methods by prompting the user for a mark, the max mark possible, the weighting of that test and then producing a final score for that test. eg. (35/50)*75% = overall mark
However, I am struggling to use methods and I was wondering if someone could point me in the right direction as to why my code below has some errors and doesn't run? I don't want any full answers because I would like to try and do it best on my own and not plagiarise. Any help would be greatly appreciated :)! (Also pls be nice because I am new to programming and I'm not very good) Thanks!
import java.util.Scanner;
public class gradeCalc
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
scoreCalc();
System.out.print("Your score is" + scoreCalc());
}
public static double scoreCalc (int score1, int maxMark, double weighting, double finalScore)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter mark");
in.hasNextInt();
score1 = in.nextInt();
System.out.print("Enter Max mark");
in.hasNextInt();
maxMark = in.nextInt();
System.out.print("Enter weighting as a decimal (eg. 75% = 0.75)");
in.hasNextInt();
weighting = in.nextInt();
finalScore = (score1/maxMark)* weighting;
return finalScore;
}
}
A couple of things spring to mind:
You execute
scoreCalc()
twice. Probably you want to execute it once and save the result in adouble
variable like:double score = scoreCalc()
.Speaking of
scoreCalc()
: Your definition takes 4 parameters that your don't have as input for that method. You should remove those from your method definition, and instead addscore1
,maxMark
,weighting
andfinalScore
variable declarations in the method-body.In your main function, you declare and instantiate a
Scanner
object you don't use.Be careful with arithmetic that mixes
int
anddouble
.Some mistakes/errors to point out are:-
1) You do not need this statement
Scanner in = new Scanner(System.in);
in yourmain()
, as you are not taking input from user through that function.2) Your function
scoreCalc (int score1, int maxMark, double weighting, double finalScore)
takes parameters, for example its call should look likescoreCalc(15, 50, 1.5, 2.7)
, but you are calling it asscoreCalc()
, that is without paramters frommain()
.Edit:- There is one more serious flaw in your program, which might work , but is not good coding. I wont provide code for it , and will leave implementation to you. Take input from user in the
main()
(usingscanner
) , assign the result to a temp variable there, and then pass that variable as parameter to the functionscoreCalc()
Or you can make your scoreCalc function without parameters, and take user input in it (like present), and finally return just the result to
main()
.Both approaches seem appropriate and you are free to choose :)
You are calling your method
scoreCalc()
without passing the parameters you defined. When you are calling it, it was defined as having 3 parameters.Also, when creating a class, start it with upper case,
GradeCalc
To start :
1) Follow coding conventions. (Class name should start with a capital letter).
2) In your context, you don't need
Scanner in = new Scanner(System.in);
inmain()
because you are not using it.3) You are a calling the method
scoreCalc()
without parameters. Whereas, the method needs to be called with parameters.4) A method,is a module. It as block of code which increases re-usability. So I suggest that accept the values from user in
main()
method and then pass them to the method for calculation.As opposed to other answers I will start with one other thing.
You forgot about the most important method - and that is the
Constructor
.You have to create a grade calculator, so you create a class(type) that represents objects of grade calculators. Using the Java convention this class should be named
GradeCalculator
, don't use abbreviations likeCalc
so that the name is not ambiguous.So back to the
constructor
- You have not created your Calculator object. It may not be needed and you may achieve your goal not using it, but it's not a good practice.So use this method as well - this way, you'll create actual Calculator object.
It can be achieved like that:
And now you can imagine you have your calculator in front of you. Whan can you do with it? Getting a mark would be a good start - so, what you can do is:
Now you'll have to define an method
getMark()
:In which you would prompt the user for the input. You can also do:
and that way get max mark (after defining a method).
The same way you can call method
myCalculator.getWeighting()
,myCalculator.calculateFinalResult()
,myCalculator.printResult()
.This way you'll have actual object with the following mehtods (things that it can do):
And that I would call creating a calculator using methods - and that I would grade highly. Try to think of the classes you are creating as a real objects and create methods representing the behaviour that objects really have. The sooner you'll start to do that the better for you. Writing whole code in one method is not a good practice and in bigger applications can lead to various problems. So even when doing small projects try to do them using best practices, so that you don't develop bad habbits.
Edit: So that your whole program can look like this:
Please mind the fact that after constructing the Calculator object you don't have to use methods that are static.
As you can see
scoreCalc
method needs a set of parameters, but you call it without parameters.The second: there is no need in
Scanner in = new Scanner(System.in);
into yourmain(String[] args)
method. You are calling it intoscoreCalc
method.Third: you are calling
scoreCalc
twice. The first call is beforeSystem.out.println
, the second is intoSystem.out.println
. And your application will ask user twice to enter values.store result value in a variable and show it later: