• I need help with some Java for my assignment
    7 replies, posted
[code] import java.util.Scanner; public class vehicle_insurance { public static void main(String[] args) { Scanner iireader = new Scanner(System.in); String pnum, phol, regnum, vmodel, phrate = ""; int yman, nppce, odr, pamnt, phage, yinfr, yacci; float adprate, ddprate,adpamnt,ddpamnt, cmval,saprem = 0, srprem = 0; System.out.println("*** Insurance Calculator Stage 1 ***"); // Policy Number Input System.out.println("Enter Policy Number:"); pnum = iireader.nextLine(); // Policy Holder Input System.out.println("Enter Policy Holder:"); phol = iireader.nextLine(); // Vehicle Reg Input System.out.println("Enter Vehicle Registration Number:"); regnum = iireader.nextLine(); // Vehicle Model Input System.out.println("Enter Vehicle Make/Model:"); vmodel = iireader.nextLine(); // Vehicle Manufacturer Input System.out.println("Enter Year of Manufacture:"); yman = iireader.nextInt(); // Purchase Price Input System.out.println("Enter New Purchase Price:"); nppce = iireader.nextInt(); // Odometer Reading Input System.out.println("Enter Odometer Reading: "); odr = iireader.nextInt(); // Policy Holder Age Input System.out.println("Enter policy holder age: "); phage = iireader.nextInt(); // 5 Year Infringements Input System.out.println("Enter number of traffic infringements received in previous 5 years: "); yinfr = iireader.nextInt(); // 5 Year Accidents Input System.out.println("Enter number of 'at fault' accidents in the previous 5 years: "); yacci = iireader.nextInt(); System.out.println("\n*** Policy Information ***\n"); System.out.println("Policy Number: " + pnum); System.out.println("Policy Holder: " + phol); System.out.println("Vehicle Registration: " + regnum + "\n"); System.out.println("Vehicle Make / Model: " + vmodel); System.out.println("Manufacture Date: " + yman); System.out.println("\n*** Base Premium Calculation ***\n"); // Defining age and distance depreciation variables adprate = ((2015 - yman) + 1) * (5f / 100f); ddprate = (odr / 10000f) * (2f / 100f); adpamnt = Math.round(adprate * nppce); ddpamnt = Math.round(ddprate * nppce); cmval = nppce - ((int)adpamnt + (int)ddpamnt); pamnt = (int)(cmval * (5f / 100f)); System.out.println("New Purchase Price: $" + nppce); System.out.println("Age Depreciation Amount: $ -" + adpamnt); System.out.println("Distance Depreciation Amount: $ -" + ddpamnt); System.out.println("Estimated Current Market Value: $" + cmval + "\n"); System.out.println("Base Premium Amount: $" + pamnt); System.out.println("\n*** Adjustments to Premium Calculation ***\n"); // Defining Surcharge to Premium Depending on age if (phage < 25) { saprem = (int)(pamnt * (50f / 100f)); } else if (phage >= 25 && phage <= 34) { saprem = (int)(pamnt * (20f / 100f)); } else if (phage >= 35) { saprem = 0; } // Defining Policy Holder Rating Depending on Infringements and Accidents in the past 5 years if ((yinfr == 0) && (yacci == 0)) { phrate = "Good"; srprem = (saprem / (10f / 100f)); } else if ((yinfr >= 1 && yinfr <= 2) && (yacci == 0)) { phrate = "Average"; srprem = (saprem * (10f / 100f)); } else if ((yinfr >= 3) && (yacci >= 1)) { phrate = "Poor"; srprem = (saprem * (30f / 100f)); } System.out.println("Policy Holder Age: " + phage); System.out.println("Policy Holder Rating: " + phrate); System.out.println("Age-based premium adjustment: " + saprem); System.out.println("Rating-based premium adjustment: " + srprem + "\n"); //System.out.println("Adjusted Premium Amount: " + pamnt); } } [/code] So this is what I have its meant to be a vehicle insurance form And for some reason fir the Age base premium and Rating based premium it always prints Age-based premium adjustment: 0.0 Rating-based premium adjustment: 0.0 Please Halp
At a quick glance I can't tell what is wrong. For simple logic issues like this I'd highly recommend you learn how to use a debugger, this way you can see what values equal what and determine where calculations are going wrong, as well as follow where the program is going by seeing which statements it goes to (ie which if statements it uses). What values are you using?
The rating based premium is the age based premium multiplied by a certain factor, meaning if the age is over 35 it'll always be 0 Did you mean to multiply the payment instead? [editline]5th October 2015[/editline] Also there's no possible rating for having 0 infringements, but 1 or more accidents. That returns 0.0 too [editline]5th October 2015[/editline] [QUOTE=Karmah;48825756]At a quick glance I can't tell what is wrong. For simple logic issues like this I'd highly recommend you learn how to use a debugger, this way you can see what values equal what and determine where calculations are going wrong, as well as follow where the program is going by seeing which statements it goes to (ie which if statements it uses). What values are you using?[/QUOTE] This too, if something breaks, try it with different values to narrow down what could be causing it, then step through with a debugger to see where exactly the numbers start to go wrong
The 35 or over is meant to return 0
i don't have access to a computer, but i'd check all the (int) casting you're doing if you cast a float to an int, you're essentially flooring it so if any calculations like [code]pamnt = (int)(cmval * (5f / 100f));[/code] is between 0 and 1, it'll just drop the decimals giving you 0. and if you multiply that variable afterwards with anything else, it'll still be 0.
You should really break this up into more functions. Also, your class name vehicle_insurance should be named something like "VehicleInsurance". It just goes with Java naming conventions. Not to mention your variable names are little confusing also. The problem probably has to do with your type casting from int's to floats or vice versa. That's just my guess at a quick glance. [B]EDIT:[/B] I see you instantiate the saprem and srprem to be of type float, but set it to an initial value of 0 (for both), which would be an int. This could be a problem I think.
Thanks for all the help guys! I think ill be spending more time in the programming section of facepunch :)
type casting has been the problem with all my java assignments at uni
Sorry, you need to Log In to post a reply to this thread.