Reputation: 1
Hi there I have created a class to run another Test class but I am facing some problems. I have declared both the agePremium and ticketPremium as private doubles. I later on use them in 2 methods to calculate the premium respectively. But when it gets to the part where i want t use the calculated variables in another calculation here
/*
public double premium() {
if (ticket <4){
return ((BASE_PREMIUM*value) * agePremium) * ticketPremium;
}
else {
System.out.println("Sorry, you have too many tickets !!");
return 0;
}
}
*/
it does not read the values from my methods and instead reads the 1 from the declaration and initialization.
private double agePremium =1; private double ticketPremium =1;
and it multiplies them.
How do i get them to link them to each other and replace the 1 with the new values and multiply. Thanks
import java.util.Scanner;
public class Driver {
private int age;
private int ticket;
private double value;
final double BASE_PREMIUM=0.05;
private double agePremium =1;
private double ticketPremium =1;
Scanner scanner = new Scanner(System.in);
public void read() {
System.out.println("Driver’s Age?");
age = scanner.nextInt();
System.out.println("Number of Tickets?");
ticket = scanner.nextInt();
System.out.println("Value of Car?");
value = scanner.nextDouble();
}
public double premium() {
if (ticket <4){
return ((BASE_PREMIUM*value)*agePremium)*ticketPremium;
}
else {
System.out.println("Sorry, you have too many tickets !!");
return 0;
}
}
public void premiumAge() {
if (age > 29) {
agePremium += 0;
}
else if (age <= 29 && age >= 25) {
agePremium += 0.10;
}
else {
agePremium += 0.15;
}
}
public void premiumTicket() {
switch (ticket) {
case '1':
ticketPremium += 0.1;
break;
case '2':
ticketPremium += 0.25;
break;
case '3':
ticketPremium += 0.50;
break;
case '0':
ticketPremium += 0.00;
default:
ticketPremium = 0.00;
break;
}
}
}
Upvotes: 0
Views: 122
Reputation: 209112
Your methods should be somewhat like this, returning a double
in order to access the private data fields in another class
public double premiumAge() {
if ...
return agePremium;
else if ...
return ....
...
}
You need to return a value to use the method in another class, if you're expecting a value.
Also good to note is naming convention. When you want methods to access a private data field, use the name of the data field with a prefix of get
eg. agePremium
: getAgePremium()
;
To Access From another class
Driver driver = new Driver(); // create a Driver object (instantiate)
System.out.println(driver.premiumAge());
// Or if you follow the naming convention in your Driver class
System.out.println(driver.getAgePremium());
Upvotes: 1
Reputation: 79875
Somewhere along the way, you'll need to call premiumAge()
and premiumTicket()
, to get the calculation to happen. Probably from somewhere in the premium()
method would be a good idea.
Note that keeping the calculated values in fields is not as good an idea as having the methods that calculate them actually return their results.
Upvotes: 0
Reputation:
I think you forgot to invoke methods premiumAge()
and premiumTicket()
.
They seem to be the ones modifying agePremium
and ticketPremium
.
Upvotes: 0