java - How to call a function with array parameters in main application? -
i've been doing tutorial on inheritance abstract class, , pass array function calculation of total price. then, when tried call function in main, didn't work , i've got error according method call.
here calculation code in subclasses :
public double calcprice(string[] a, int[] qty, int num){ int =0; for(i=0;i<=num;i++) { if (a[i]=="a") price=24.90; } double tot=price+qty[i]; return tot; }
this method call in loop. don't know how call method error says "non-static method calcprice() cannot referenced static context"
for(int i=0;i<=num;i++) { system.out.println("\t"+a[i]+"\t\t\t"+qty[i]+" "+calcprice()); }
the main method static, , can't call non-static code. have 2 solutions.
create instance of class performing calculation, , call
calcprice
on instance.make
calcprice
static.
i suggest option 1 you've been doing research on classes. practice you.
also not compare variable a
"a"
==
. use .equals
instead. check link why.
edit:
i'm not sure how abstract class plays have no abstract methods needing implementation.
public class calcclass{ public double calcprice(string[] a, int[] qty, int num){ int =0; for(i=0;i<=num;i++) { if ("a".equals(a[i])) price=24.90; } double tot=price+qty[i]; return tot; } } public class mainclass{ public static void main(string[] args){ //create instance of calc class calcclass c = new calcclass(); //call calc price method on calcclass c.calcprice(a, new int[]{1}, 1}; } }
Comments
Post a Comment