Does anyone solve the error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
16th Aug 2022, 1:13 PM
Avijit Pal
Avijit Pal - avatar
11 Answers
+ 1
I tried removing show function and added print statement in calcFee function, called calcFee instead of show, it seems to work https://code.sololearn.com/cL9dW42bgj5z/?ref=app
16th Aug 2022, 3:13 PM
Prathyusha Muppidi
Prathyusha Muppidi - avatar
+ 3
Variable declared within a method will have local scope to method. And does not exist outside of that method. So declare those outside method, class level scope if you access it outside too. => about tutionFee, amount variables.. Yes. Another, Error: calcStipend() vs clacStipend()
16th Aug 2022, 2:05 PM
Jayakrishna 🇮🇳
+ 2
Hi, the error thrown is a syntax error. The spelling of the method you have defined and used are different. You have defined clacStipend method but have invoked calcStipend. Please fix the typo and try executing.
16th Aug 2022, 1:46 PM
Prathyusha Muppidi
Prathyusha Muppidi - avatar
+ 1
Hi! have you written this code yourself or copied it?
16th Aug 2022, 1:30 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Jayakrishna🇮🇳 is it okay now? But Still getting 0.0 in output interface BasicExp{ double hostelFee=5000.00, examFee=1200; void calcFee(); } class Stipend{ double totalStipend; double tutionFee=75000; int cgpa; void clacStipend(int cgpa){ if(cgpa<7){ totalStipend=tutionFee*0.25; System.out.println("Exc1"); } else{ totalStipend =tutionFee*0.40; } } } class TotalFee extends Stipend implements BasicExp{ double amount; public void calcFee(){ amount =(hostelFee+examFee+tutionFee)-totalStipend; } void show(){ System.out.println(amount); } } class Output{ public static void main(String args[]){ TotalFee tf=new TotalFee(); tf.clacStipend(7); tf.show(); } }
16th Aug 2022, 2:58 PM
Avijit Pal
Avijit Pal - avatar
+ 1
Avi Jit In your code, tf.calcFee() ; // call this before tf.show() ; // no need int cpga; outside method.
16th Aug 2022, 3:56 PM
Jayakrishna 🇮🇳
+ 1
Updated the code, i didnt realize that the amount is defined both as local variable and class variable. So when local variable amount was updated in calcFee function it didnt reflect, corrected it. https://code.sololearn.com/cL9dW42bgj5z/?ref=app
16th Aug 2022, 3:57 PM
Prathyusha Muppidi
Prathyusha Muppidi - avatar
+ 1
Thank you guys! it was a great quest of bug hunting 😁 https://code.sololearn.com/cr8TvLGq4jyO/?ref=app
16th Aug 2022, 4:02 PM
Avijit Pal
Avijit Pal - avatar
0
Prathyusha Muppidi I have corrected the typos but but getting 0.0 in output. Somehow I think "cgpa" value is not passing. Here is the code: interface BasicExp{ double hostelFee=5000.00, examFee=1200; void calcFee(); } class Stipend{ double totalStipend; double tutionFee=75000; int cgpa; void clacStipend(int cgpa){ if(cgpa<7){ totalStipend=tutionFee*0.25; System.out.println("Exc1"); } else{ totalStipend =tutionFee*0.40; } } } class TotalFee extends Stipend implements BasicExp{ double amount; public void calcFee(){ amount =(hostelFee+examFee+tutionFee)-totalStipend; } void show(){ System.out.println(amount); } } class Output{ public static void main(String args[]){ TotalFee tf=new TotalFee(); tf.clacStipend(7); tf.show(); } }
16th Aug 2022, 2:56 PM
Avijit Pal
Avijit Pal - avatar
0
Prathyusha Muppidi Amazing it works! But if try my method, how to get access the value of "amount" under the method "show()" ?
16th Aug 2022, 3:40 PM
Avijit Pal
Avijit Pal - avatar
0
Prathyusha Muppidi just now, I got the bug actually in my code, I am never using the method "calcFee" to compute the "amount" that is how it's showing the default double value 0.0. See here this working now: interface BasicExp{ double hostelFee=5000.00, examFee=1200; void calcFee(); } class Stipend{ double totalStipend; double tutionFee=75000; int cgpa; void clacStipend(int cgpa){ if(cgpa<7){ totalStipend=tutionFee*0.25; System.out.println("Exc1"); } else{ totalStipend =tutionFee*0.40; } } } class TotalFee extends Stipend implements BasicExp{ double amount; public void calcFee(){ amount =(hostelFee+examFee+tutionFee)-totalStipend; } void show(){ System.out.println(amount); } } class Output2{ public static void main(String args[]){ TotalFee tf=new TotalFee(); tf.clacStipend(7); tf.calcFee(); tf.show(); } }
16th Aug 2022, 4:00 PM
Avijit Pal
Avijit Pal - avatar