CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2009
    Posts
    4

    Help making an Insurance Calculator

    Okay, this is for a class assignment, but I have no clue where to even begin. This is my first time ever taking a programming course and I'm a little lost. But, I've started what I know so far. We're suppose to do it on TextPad and this is what i have so far.


    import javax.swing.JOptionPane;

    public class LifeInsCalc {
    public static void main(String[] args) {



    That is about all I know so far. I'm pretty sure that I needed to import the JOptionPane because our instructor supported us with a screenshot of what it should look like when we're done and here it is:

    http://blackboard.uc.edu/courses/1/0...creenImage.jpg

    Also, here are the rules for the program and such.

    No one over 79 can apply. The term plus the customer age cannot exceed 80. Smokers of any age are not eligible. Using the actuarial rate table below, create a program that gets the necessary information, manipulates it, and then presents a quote to the customer.

    UCC Life Insurance Co.

    <Customer Name>, this term life insurance quote is based on your current age and the amount of insurance, as well as the length of the term you chose.

    Cost is <$cost> per month for <$amtOfInsurance> of straight term life for <termLength> years. This is based on your current age of <age>. Total cost to you over the term of the policy is <$totalCost>.


    Actuarial Table:

    Age Cost/$10,000/mo
    0 to 9 $1
    10 to 19 $3
    20 to 29 $5
    30 to 39 $8
    40 to 49 $12
    50 to 59 $15
    60 to 69 $18
    70 to 79 $21


    If anyone could please help me out, give me a few pointers on what to do, that would be awesome. Thanks much.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Help making an Insurance Calculator

    Looks like you have to display a prompt on the console and get input from the keyboard for each item of data, then when the last item has been entered, do the calculation and display the result. Just display the result to the console to start with. You can worry about displaying a fancy results window once everything else is working.

    If you haven't been taught how to display stuff on the console and get input from the keyboard, I guess you're supposed to find that out for yourself. It's not difficult if you can follow simple examples. A good place to start is the Java Tutorials.

    Ask a specific question if you get stuck, and post the relevant code if necessary.

    The only way to learn a new programming language is by writing programs in it...
    B. Kernighan & D. Ritchie
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Feb 2009
    Posts
    4

    Re: Help making an Insurance Calculator

    still don't get it. just went through a bunch of tutorials...

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Help making an Insurance Calculator

    Forget the code for a moment and get a pen and paper and run through an insurance quote as if you are the computer. That is, write down each question you need to ask in order, providing sample answers as you go. Perform the calculation and finally write down the result. You may want to repeat the above process for each potential variation ie for an 80 year old, for a 40 year old smoker etc.

    You now have the sequence of events you need to perform to complete the task. You'll find coding is much easier when you know exactly what you have to code.

  5. #5
    Join Date
    Feb 2009
    Posts
    4

    Re: Help making an Insurance Calculator

    Here is what i go so far, but when i go to compile it, still says errors... can anyone help?

    Code:
    import java.lang.String;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    public class LifeInsCalc {
      public static void main(String[] args) {
    
    	  // Create a Scanner object
    
    	  Scanner input = new Scanner(System.in);
    
    	  // Prompt the user to enter their name
    
    	  System.out.print(
    		  "Welcome to UCC Life Insurance!\n" +
    		  "Note: only those under 80 years of age and non-smokers may apply.\n" +
    		  "Enter your name: ");
    
    	  custName = input.next(); // use the Scanner to get the name string
    
    	  // Prompt the user to enter their age and declare/initialize the variable 'age'
    
    	  System.out.print("Enter your age:");
    
    	  int age = input.nextInt(); // user the Scanner to get an integer input
    
    	  if(age >=80) {
    
    		  message = custName +", you must be under the age of 80 to apply.\n" +
    
    		  "Thanks for visiting!";
    
    		  JOptionPane.showMessageDialog(null,
    
    		  message,"Sorry!",JOptionPane.INFORMATION_MESSAGE);
    
    		  System.exit(0); // stop the program and exit
    
    	  } // end if (age >=80)
    
    
    	  // Prompt the user to enter tobacco use and declare/initialize the variable 'smoker'
    
    	  System.out.print("Do you use tobacco products (1 = Yes, 0 = No)? ");
    
    	  int smoker = intput.nextInt();
    
    	  if(smoker == 1) {
    
    		  message = custName +", you cannot apply if you use tobacco products.\n" +
    
    		  		"Thanks for visiting!";
    
    		  JOptionPane.showMessageDialog(null,
    
    		  		message,"Sorry!",JOptionPane.INFORMATION_MESSAGE);
    
    		  System.exit(0);
          } // end if
    
          // Prompt the user to enter amount of insurance and declare/initialize the variable 'insAmount'
    
          System.out.print("Enter the amount of insurance you need, \n" +
    
          		"i.e. 20000, 100000, etc. (without commas):");
    
          int insAmount = input.nextInt();
    
          // Prompt the user to enter term of the policy and declare/initialize the variable 'term'
    
          System.out.print("enter the term in 5 or 10 year increments:");
    
          int term = input.nextInt();
    
          // if the term plus age equals or exceeds 80, reduce the term to 80 - age
    
          if((term + age)>=80) {
    
    		  term = 80 - age
    
    		  message = "The term plus your age cannot exceed age 80." +
    
    		  "the term will be reduced to " + term;
    
    		  JOptionPane.showMessageDialog(null, message,
    
    		  "Notice",JOptionPane.INFORMATION_MESSAGE);
    
    	  } // end if
    
    	  int rate = 1; // declare and initialize rate which is per 10k of coverage
    
    	  // Determine rate for age with multiple if...else if statements
    	  // i.e. if age is greater than or equal to 0 and age is less than 10 then rate = $1
    
    	  if((age>=0) && (age<10)) {
    
    		  rate = 1;
    
    	  }
    
    	  else if((age >=10) && (age <20)) {
    
    		  rate = 3;
    
    	  }
    
    	  else if((age >=20) && (age <30)) {
    
    		  rate = 5;
    
    	  }
    
    	  else if((age >=30) && (age <40)) {
    
    		  rate = 8;
    
    	  }
    
    	  else if((age >=40) && (age <50)) {
    
    		  rate = 12;
    
    	  }
    
    	  else if((age >=50) && (age <60)) {
    
    		  rate = 15;
    
    	  }
    
    	  else if((age >=60) && (age <70)) {
    
    		  rate = 18;
    
    	  }
    
    	  else if((age >=70) && (age <80)) {
    
    		  rate = 21;
    
    	  }
    
    	  // declare variables monthly payment and total payment for the term
    
    	  int monthlyPayment, totalPayment;
    
    	  // calculate payments
    
    	  monthlyPayment = rate * (insAmount / 10000);
    
    	  totalPayment = monthlyPayment * 12 * term;
    
    	  // display results
    
    	  message = custName +", the monthly payment is $" + monthlyPayment +
    
    	   "\nThe total payment over the term is $" + totalPayment;
    
    	  JOptionPane.showMessageDialog(null,message,"Result",
    
    	  		JOptionPane.INFORMATION_MESSAGE);
    
      } // end main
    
    } // end class LifeInsCalc()
    Last edited by Jakea6; February 4th, 2009 at 07:39 PM.

  6. #6
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Help making an Insurance Calculator

    We're usually best at answering specific questions, but it sounds to me as if you are somewhat lost at sea and foundering a bit, and your code appears confirms this. Why not talk to the teacher to get some one-on-one tutoring? This will give you your best chance at moving ahead with Java.

    One bit of specific advice I would like to give is to have you change your style of coding. Instead of creating a whole program and then testing it, you should create your program in small steps. First write out on paper what you want your program to do, then break these steps down into small steps, then try to add code to a program. When you do add code, only add a small bit of code at a time. After you add this small bit of code, compile and run the program to make sure that there are no errors. If you find any, fix them, and only then do you try to add more code. The key here is to never add code to a program that is already broken.

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Help making an Insurance Calculator

    Quote Originally Posted by Jakea6 View Post
    Here is what i go so far, but when i go to compile it, still says errors... can anyone help?
    Unless you tell us what the errors are, we can't really help. When you want help with an error, post up the full text of the error.

    You seem to be on the right track with your code, but you must remember to declare your variables before trying to use them - as far as the compiler is concerned, they don't exist until you declare them. You must also make sure you spell them right each time - the compiler won't guess. Make sure you end each complete statement with a semi-colon ';'.

    Computers are good at following instructions, but not at reading your mind...
    D. Knuth
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured