I can put the input but in output, I got a result of zero. I am using two class one is main which is logbook and another one is grade calculation. In grade calculator, I have written method and constructor and in logbook, class calling those methods through the constructor.
import java.util.Scanner;
public class LogBook {
public static void main(String[] args) {
GradeCalculation g = new GradeCalculation();
g.Askmarks();
g.finalmark();
}
}
import java.util.Scanner;
public class GradeCalculation {
Scanner kb = new Scanner(System.in);
private double quiz, midsemester, appliedproject, finaltest, finalmarks;
GradeCalculation(double quiz, double midsemester, double appliedproject, double finaltest, double finalmarks){
}
GradeCalculation(){
}
public void Askmarks(){
System.out.print("Enter the QUIZ marks: ");
double quiz = kb.nextDouble();
System.out.print("Enter the midsemester marks: ");
double midsemester = kb.nextDouble();
System.out.print("Enter the appliedproject: ");
double appliedproject = kb.nextDouble();
System.out.print("Enter the finaltest: ");
double finaltest = kb.nextDouble();
}
public void finalmark(){
finalmarks= 0.1*(quiz)+0.2*(midsemester)+0.4*(appliedproject)+0.3*(finaltest);
System.out.print("Enter the final: " + finalmarks);
}
}
The problem is that you were defining local variables in Askmarks
double quiz = kb.nextDouble();
...
double finaltest = kb.nextDouble();
They don't affect the fields
private double quiz, midsemester ,appliedproject ,finaltest,finalmarks;
and, therefore, in finalmark
, you are getting the default value for each double
field, which is 0.0
.
The solution would be setting the fields rather than initializing local variables .
this.quiz = kb.nextDouble();
...
this.finaltest = kb.nextDouble();
In your Askmarks
method, you are redefining the variables by prefacing them with double
. If you remove the doubles
from your Askmarks
method, it should work.
public void Askmarks(){
System.out.print("Enter the QUIZ marks: ");
quiz = kb.nextDouble();
System.out.print("Enter the midsemester marks: ");
midsemester = kb.nextDouble();
System.out.print("Enter the appliedproject: ");
appliedproject = kb.nextDouble();
System.out.print("Enter the finaltest: ");
finaltest = kb.nextDouble();
}
You're shadowing your fields in GradeCalculation
in Askmarks()
. Remove the type declarations of the variables inside the function. So, instead of:
...
double quiz = kb.nextDouble();
...
You should use:
...
quiz = kb.nextDouble();
...
or
...
this.quiz = kb.nextDouble();
...
What you are doing with:
...
double quiz = kb.nextDouble();
...
double midsemester = kb.nextDouble();
...
is that you are shadowing the class fields with the same name (eg double quiz
is going to shadow the class's quiz
). Shadowing refers to the behaviour in which a variable declared and/or initialized within some scope "replaces" another variable of the same name residing in an outer scope.
In your case, Askmarks()
will now think that, for example, quiz
is a local variable that only exists inside the function, and the class field quiz
no longer exists in its viewpoint.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.