When i try calling out my method it prints 0 why is this? am.area() " but if i take out the baseprice variable in the formula it prints out correctly how do i correct this? also i wanted to try out Associaton but i don't know how to use it.
class Base {
double baseprice;
public void setBaseprice(int bp) {
baseprice = bp;
}
public double getBaseprice() {
return baseprice;
}
}
class ID{
int agentId;
ID(int Id) {
this.agentId=Id;
}
}
class Agent{
String agentName;
Agent(String name){
this.agentName=name;
}
}
class AreaMinor extends Base {
double sides;
public AreaMinor(int s) {
sides = s;
}
public void area() {
double sum = sides*sides*(4+Math.sqrt(3))/4;
double area = sum*baseprice;
System.out.print(area);
}
}
public class Main {
public static void main(String args[]) {
java.util.Scanner kbd = new java.util.Scanner(System.in);
System.out.print("Enter your lot type ");
lot = kbd.nextInt();
if(lot == 1) {
System.out.print("Enter the Sides ");
int side = kbd.nextInt();
AreaMinor am = new AreaMinor(side);
am.area(); //why does it this line print 0
baseprice is set to 0 by default, then result 0xK = 0 . Normal
you should add am.setBaseprice(3);
in main method (if you want the baseprice to be 3) otherwise it is initialized to 0
alternatively, you can call the setter in the constructor of AreaMinor
also note that if baseprice setter accepts int values, I see no reason for it to be typed double
In your Base
Class, you've declared the baseprice
variable as double and not explicitly declared a value by calling that constructor.
Therefore, the problem is that you haven't initialised the baseprice
variable, Hence, it is by default 0.0
...
AreaMinor am = new AreaMinor(side);
am.setBasePrice(3);
am.area();
...
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.