I am trying to work out the following question:
write a program to determine the cost of car insurance premium based on the drivers age and the number of accidents they have had.
The basic insurance charge is £500. There is a surcharge of £100 if the driver is under 25 and additional surcharge for accidents:
1 accident= £50 surcharge
2 accidents= 125 surcharge
3 accidents= 225 surcharge
4 accidents- 375 surcharge
5 accidents= 575 surcharge
6 or more= no insurance
Here is my source code:
import java.util.Scanner;
public class carinsurance{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
double age, accidents, accident, surcharge, basic, total;
age= accidents= surcharge=accident=basic=total=0;
System.out.println("Please insert your age");
age=input.nextDouble();
if (age>=25){
System.out.println ("You dont have to pay the £100 surcharge");
}
else if (age<25)
{
System.out.println ("you are going to have to pay the £100 surcharge" + basic);
total=(100+500);
System.out.println ("How many accidents have you had?");
}
accidents=input.nextDouble();
if ((accidents>=1))
{
System.out.println ("you are going to have to pay £50 accident surcharge" + basic);
total=(50+300);
}
else if ((accidents>=2 )){
System.out.println ("you are going to have to pay £125 surcharge" + basic);
}
else if ((accidents>=3 )){
System.out.println("you are going to have to pay £225 surcharge" + basic);
total=(225+300);
}
if ((accidents>=4 && accidents<=4)){
System.out.println("you are going to have to pay £375 surcharge" + basic);
}
else if ((accidents>=5 ))
{
System.out.println("you are going to have to pay £575 surcharge" + basic);
total=(575+300);
}
else if ((accidents>=6)){
System.out.println("Sorry you dont qualify for any insurance!");
total=basic+surcharge;
}
}
}
This code when run asks for you to insert your age, if you put 25 it will say you dont have to pay the £100 surcharge, however it wont ask how many accidents you have had whereas if you put an age under 25 it says that you have to pay the £100 surcharge and also asks how many accidents you have had, but does still not display a total of how much somebody would have to pay.
Can anyone see where I am going wrong? im totally stumped as I thought all of the relevant sode was there.
Copyright © 2024 QUIZLS.COM - All rights reserved.
Answers & Comments
Verified answer
you have extra stuff. eg:
if (age>=2) {
...
}
else of (age<2){...
should be:
if (age>=2){...
}
else {... if age is not >=2 then it must be <2. No additional test needed
}
reverse an order of ifs
if (accidents>=1){...
}
else if (accidents>=2)...
well if it is >=1 then it executes the first block and never get chance to go to the second test which might be true, so reverse order of tests:
if (accidents>=6) {...
}
else if (accidents>=5) ...
else if (accidents>=4)... , well you get an idea.
also
if ((accidents>=4 && accidents<=4))
is equal to if (accidents==4)
it might be better to use switch:
switch(accidents) {
case 1: ...
break;
case 2:.....
break;
...
case 5:...
break;
default:
...You do not qualify for insurance....
}
1
After the line:
System.out.println ("You dont have to pay the £100 surcharge");
you need to add the line:
System.out.println ("How many accidents have you had?");
You don't want the number of accidents to be a double since it's not possible to have a fraction of an accident, it must be a whole number. So replace:
accidents=input.nextDouble();
with:
accidents=input.nextInt();
Your logic is wrong; it will stick on the first if statement since you say 'if accidents is greater than or equal to 1' which covers any value greater than one. You want to say
if (accidents == 0)
<no surcharge>
else if (accidents == 1)
<add £50 surcharge to total>
...
else if (accidents >= 6)
<display a message saying not possible to insure, exit from method using a return statement>
It's not displaying your total because you don't have a line that prints it out. Add a line that says:
System.out.println("Total insurance is " + total);
OK, here goes...
What if you could put all this rate stuff into an array? that'd be great, huh? Except, you'd have to remember rate[2] was for 3 accidents, rate[0] was for 1 or 2 accidents???? My point is: we have too many variables with your method or if we named all the variables rate[]. The much better way is an Object. We make an Object with a class. Think of a class as a "fancy" array, but we have names instead of the array[ number ].
Look at my code. I made two classes. They would have to be in the same folder for this to work (but that is how to java)
----------------------------
import java.util.Scanner;
public class QuoteForm {
public static void main(String[] args ) throws Exception {
Scanner sc = new Scanner(System.in);
boolean run = true;
while( run ) {
Rating r = new Rating();
System.out.print("Please insert your age: >");
r.setAge(sc.nextInt());
System.out.print("How many accidents in the last year? >");
r.setAccidents( sc.nextInt() );
System.out.print(r.quote());
System.out.print("--------------\n");
System.out.println( sc.nextLine() ); // have to clear out sc.buffer
System.out.print("Would you like to make another quote? [y/n] > ");
String answer = sc.nextLine();
run = ( ! answer.equalsIgnoreCase("n") ) ? true : false;
}
}
}
------------------------------------
public class Rating {
private final String pSign = "£";
private static final float[] RATES =
{ 50.00f,125.00f,
225.00f,375.00f, 575.00f};
private int numAccident;
private int age;
protected void setAge( int value ) { age=value; }
protected void setAccidents( int number ) { numAccident = number; }
public String quote() {
if( numAccident > 5 )
return "I'm sorry, but at the present we offer no insurance\n";
float base = 500.0f;
if( age < 25) base += 100.0f;
if( numAccident > 0 && numAccident <= 5 )
base += RATES[ numAccident - 1 ];
String q = "Based on your input,\n";
q += "we are happy to quote you\n";
q += "our basic auto liability policy\n";
q += String.format("for: %s%4.2f.%n",pSign, base);
return q;
}
}
------------------------
Learn the String.format();
% is an 'entity' or placeholder
%d is a digit
%s a string
%n a platform independent line return
%f is a decimal number, float or double
%4.2f means 4 columns on the right of . and 2 cols on the right
you can also use entities with
double grandTotal = 999.99;
System.out.printf("this is the total: $%4.2f%n", grandTotal);
What specifically regarding Java is the question? Your pseudocode would be: Get age, gender and no-accident-years from user If age < 20 or age > 75 {"Sorry, we can't serve you"; exit} If age < 25, premium = 1500 else premium = 800 If gender = male, premium = premium + 200 If no-accident-years > 5, no-accident-years = 5 premium = premium * ( 1 + (0.10 * no-accident-years))
I might suggest one to visit this website where onel can compare rates from different companies: http://quotes-for-insurance.net/index.html?src=5YA...
RE :JAVA - car insurance program?
I am trying to work out the following question:
write a program to determine the cost of car insurance premium based on the drivers age and the number of accidents they have had.
The basic insurance charge is £500. There is a surcharge of £100 if the driver is under 25 and additional surcharge for accidents:
1 accident= £50 surcharge
2 accidents= 125 surcharge
3 accidents= 225 surcharge
4 accidents- 375 surcharge
5 accidents= 575 surcharge
6 or more= no insurance
Here is my source code:
import java.util.Scanner;
public class carinsurance{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
double age, accidents, accident, surcharge, basic, total;
age= accidents= surcharge=accident=basic=total=0;
System.out.println("Please insert your age");
age=input.nextDouble();
if (age>=25){
System.out.println ("You dont have to pay the £100 surcharge");
}
else if (age<25)
{
System.out.println ("you are going to have to pay the £100 surcharge" + basic);
total=(100+500);
System.out.println ("How many accidents have you had?");
}
accidents=input.nextDouble();
if ((accidents>=1))
{
System.out.println ("you are going to have to pay £50 accident surcharge" + basic);
total=(50+300);
}
else if ((accidents>=2 )){
System.out.println ("you are going to have to pay £125 surcharge" + basic);
}
else if ((accidents>=3 )){
System.out.println("you are going to have to pay £225 surcharge" + basic);
total=(225+300);
}
if ((accidents>=4 && accidents<=4)){
System.out.println("you are going to have to pay £375 surcharge" + basic);
}
else if ((accidents>=5 ))
{
System.out.println("you are going to have to pay £575 surcharge" + basic);
total=(575+300);
}
else if ((accidents>=6)){
System.out.println("Sorry you dont qualify for any insurance!");
total=basic+surcharge;
}
}
}
This code when run asks for you to insert your age, if you put 25 it will say you dont have to pay the £100 surcharge, however it wont ask how many accidents you have had whereas if you put an age under 25 it says that you have to pay the £100 surcharge and also asks how many accidents you have had, but does still not display a total of how much somebody would have to pay.
Can anyone see where I am going wrong? im totally stumped as I thought all of the relevant sode was there.
Follow 5 answers