0

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;
        }
    }           
}
4
  • Did you call your premium() and premiumAge()? If so, show the code where you're instantiating the Driver Commented Oct 17, 2013 at 1:07
  • Just wondering what agePremium += 0; is suppose to do? Commented Oct 17, 2013 at 1:07
  • @Pshemo Pretty awesome huh? Commented Oct 17, 2013 at 1:08
  • @JuanMendes Yea. I am shocked. Just like when I learned that meaning of life is 42. Commented Oct 17, 2013 at 1:12

3 Answers 3

1

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());
Sign up to request clarification or add additional context in comments.

1 Comment

Looks like you actually decrypted what the code was trying to do
0

I think you forgot to invoke methods premiumAge() and premiumTicket().

They seem to be the ones modifying agePremium and ticketPremium.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.