Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java-Need a bit of help(not too hard I don't think)

  • 15-12-2010 7:15am
    #1
    Registered Users, Registered Users 2 Posts: 1,039 ✭✭✭


    Hey all!

    I'm doing an assignment, basic Java thing. Basically I've gotta make a program that a user will enter details, age/years driving etc and the program will determine what category the person is in for insurance cost.

    Depending on the Vehicle type being insured there's an additional cost:
    Van-€700
    Car-€576
    Motorcycle-€345
    Truck- €765

    The problem I am having is with the "if statements", I don't know how to get it so that it will record the vehicle type and then do calculations on the cost.
    class assignment1{
    public static void main(String[]args){

    Screen.newline();

    fnWelcome();
    fnInputData();
    fnDetermineCategory();

    }

    static void fnWelcome(){ //Welcome Function

    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("Welcome to this Insurance Cost Assesment Program");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("Here is a description of the categorys of insurance offered");
    Screen.newline(3);
    Screen.cursorpos(5);
    Screen.message("Category A: for those in the age group 17 to 25");
    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Category B: for those in the age group 26 to 59");
    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Category C: for those in the age group 60 and over");
    Screen.newline(3);
    Screen.message("There is also a registration fee charged on what vehicle type is being insured");
    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Vehicle Type Registration Fee");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Motorcycle $345");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Car $567");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Van $700");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Truck $765");
    Screen.newline(2);


    }//End Function Welcome

    static void fnInputData(){

    String personname; //Variable
    int age; //Variable
    int yearsdriving; //Variable
    String vehicletype; //Variable
    char accident; //Variable
    char ppoints; //Variable

    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Please enter your name: ");
    personname=Keyboard.readString();
    Screen.cursorpos(5);
    Screen.message("Please enter your age: ");
    age=Keyboard.readInt();
    Screen.cursorpos(5);
    Screen.message("Please Enter The Number Of Years You Are Driving: ");
    yearsdriving=Keyboard.readInt();
    Screen.cursorpos(5);
    Screen.message("Please enter your vehicle type: ");
    VehicleType=Keyboard.readString();
    Screen.cursorpos(5);
    Screen.message("Please enter Y/N if you have had an accident: ");
    accident=Keyboard.readChar();
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Please enter Y/N if you Penalty Points: ");
    ppoints=Keyboard.readChar();
    Screen.newline();
    }

    static void fnDetermineCategory(int age, int yearsdriving,char accident,char ppoints){

    Screen.newline();
    Screen.message("Determining Category");
    Screen.newline();
    if(age >=17 || <=25 && yearsdriving <5 && accident=='n' || accident=='N' && ppoints=='n' || ppoints=='N'){
    Screen.newline();
    Screen.message("Message: Young Driver - please be careful when driving and take care on the roads");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category A");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >=17 || <=25 && yearsdriving <5 && accident=='y' || accident=='Y' && ppoints=='y' || ppoints=='Y'){
    screen.message("Young Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%");
    Screen.message("You are in Category A");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >=17 || <=25 &&yearsdriving >5 &&accident=='n' || accident=='N'&&ppoints=='n' || ppoints=='N'){
    Screen.newline();
    Screen.message("Message: Young Driver - your driving record is good so far, keep up the good work and be careful when driving and take care on the roads");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category A");
    Screen.newline();
    Screen.message("Congratulations: A discount of 2% is going to be applied to your insurance");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >=17 || <=25 && yearsdriving >5 &&accident=='y' || accident=='Y'&&ppoints=='y' || ppoints=='Y'){
    Screen.newline();
    Screen.message("Young Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category A");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >=26 || <=60 && yearsdriving <5 && accident=='n' || accident=='N' && ppoints=='n' || ppoints=='N'){
    Screen.newline();
    Screen.message("Mature Driver - your driving record is good so far, keep up the good work and be careful when driving and take care on the roads");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category B");
    Screen.newline();
    Screen.message("Congratulations: A discount of 3% is going to be applied to your insurance");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >=26 || <=60 && yearsdriving <5 && accident=='y' || accident=='Y'&& ppoints=='y' || ppoints=='Y'){
    Screen.newline();
    Screen.message("Mature Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category B");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >=26 || <=60 && yearsdriving >5 && accident=='n' || accident=='N'&& ppoints=='n' || ppoints=='N'){
    Screen.newline();
    Screen.message("Mature Driver - your driving record is good so far, keep up the good work and be careful when driving and take care on the roads");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category B");
    Screen.newline();
    Screen.message("Congratulations: A discount of 4% is going to be applied to your insurance");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }

    else if(age >=26 || <=60 && yearsdriving >5 && accident=='y' || accident=='Y' && ppoints=='y' || ppoints=='Y'){
    Screen.newline();
    Screen.message("Mature Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category B");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >60 && yearsdriving <5 && accident=='n' || accident=='N' && ppoints=='n' || ppoints=='N'){
    Screen.newline();
    Screen.message("Senior Driver- your driving record is good so far, keep up the good work and be careful when driving and take care on the roads");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category C");
    Screen.newline();
    Screen.message("Congratulations: A discount of 5% is going to be applied to your insurance");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >60&&yearsdriving <5 && accident=='y' || accident=='Y' && ppoints=='y' || ppoints=='Y'){
    Screen.newline();
    Screen.message("Senior Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category C");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >60 && yearsdriving >5 && accident=='n' || accident=='N'&&ppoints=='n' || ppoints=='N'){
    Screen.newline();
    Screen.message("Senior Driver- your driving record is good so far, keep up the good work and be careful when driving and take care on the roads");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category C");
    Screen.newline();
    Screen.message("Congratulations: A discount of 10% is going to be applied to your insurance");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >60 && yearsdriving >5 && accident=='y' || accident=='Y' && ppoints=='y' || ppoints=='Y'){
    Screen.newline();
    Screen.message("Senior Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category C");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    }
    }

    This is what I have so far, I'll type in the category A bit hopefully it will make my problem a bit clearer on what I need to do.

    Category A for those in the agre group 17 to 25
    Category B for those in the age group 26 and 60
    Category C for those over 60

    If applicant is Category A with less than 5 years driving experience and no accident and penalty points deducted from their driving license, then the cost of insuring their vehicle is €3500. No discount applies.

    If applicant is Category A with more than 5 years driving experience and no accident and penalty points deducted from their driving license, then the cost of insuring their vehicle is €2750. A discount of 2% is given on top of the fee charged.

    The rest is the same really, can anyone help me get the calculation part down right? Mainly just getting the vehicletype part done.


Comments

  • Moderators, Sports Moderators, Regional Abroad Moderators Posts: 2,666 Mod ✭✭✭✭TrueDub


    I haven't read all the way through your code, but this jumped out at me:
    age >=26 || <=60
    

    This won't work, I'm afraid, you'll need to specify the variable both times i.e.
    age >=26 || age <=60
    

    A couple of suggestions for you:

    Use a text editor or IDE that will indent your code for you, it makes it much more readable
    Use brackets in your IF statements to ensure you're getting the precedence you want
    Or better again, use method that return booleans to provide your if statement stuff. For instance, in the above case, I'd define a method as follows:
    private boolean ageAppropriate(int age) {
    if (age >=26 || age <=60) {
    return true;
    } 
    return false;
    }
    

    Then the first portion of your if statement should read
    if (ageAppropriate(age) || ....
    

    This abstracts out your logic and makes it easier to read your code i.e. cleaner code.


  • Registered Users, Registered Users 2 Posts: 1,039 ✭✭✭IrishGrimReaper


    Hey I appreciate the reply. This thing is giving me a headache. I'm using Jcreator version 3.50.013, it's an older one but it's what I use at college so I'm used to it.

    I fixed the age bit in the if statement.

    Still trying to get around the cost calculation part..
    class assignment1{
    public static void main(String[]args){

    Screen.newline();

    fnWelcome();
    fnInputData();
    fnVehicleType();
    fnDetermineCategory();

    }

    static void fnWelcome(){ //Welcome Function

    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("Welcome to this Insurance Cost Assesment Program");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("Here is a description of the categorys of insurance offered");
    Screen.newline(3);
    Screen.cursorpos(5);
    Screen.message("Category A: for those in the age group 17 to 25");
    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Category B: for those in the age group 26 to 59");
    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Category C: for those in the age group 60 and over");
    Screen.newline(3);
    Screen.message("There is also a registration fee charged on what vehicle type is being insured");
    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Vehicle Type Registration Fee");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Motorcycle $345");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Car $567");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Van $700");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Truck $765");
    Screen.newline(2);


    }//End Function Welcome

    static void fnInputData(){

    String personname; //Variable
    int age; //Variable
    int yearsdriving; //Variable
    String vehicletype; //Variable
    char accident; //Variable
    char ppoints; //Variable

    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Please enter your name: ");
    personname=Keyboard.readString();
    Screen.cursorpos(5);
    Screen.message("Please enter your age: ");
    age=Keyboard.readInt();
    Screen.cursorpos(5);
    Screen.message("Please Enter The Number Of Years You Are Driving: ");
    yearsdriving=Keyboard.readInt();
    Screen.cursorpos(5);
    Screen.message("Please enter your vehicle type: ");
    vehicletype=Keyboard.readString();
    Screen.cursorpos(5);
    Screen.message("Please enter Y/N if you have had an accident: ");
    accident=Keyboard.readChar();
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Please enter Y/N if you Penalty Points: ");
    ppoints=Keyboard.readChar();
    Screen.newline();
    }

    static void fnVehicleType(String vehicletype){

    int cost;

    if(vehicletype== 'motorcycle' || vehicletype=='Motorcycle'){
    cost=345
    return(cost)
    }

    else if(vehicletype== 'car' || vehicletype=='Car'){
    cost=567
    return(cost)
    }

    else if(vehicletype== 'van' || vehicletype=='Van'){
    cost=700
    return(cost)
    }
    else if(vehicletype== 'truck' || vehicletype=='Truck'){
    cost=765
    return(cost)
    }

    } // End fnVehicleType
    static void fnDetermineCategory(int cost,int age, int yearsdriving,char accident,char ppoints){

    int insurance;

    Screen.newline();
    Screen.message("Determining Category");
    Screen.newline();
    if(age >=17 || age <=25 && yearsdriving <5 && accident=='n' || accident=='N' && ppoints=='n' || ppoints=='N'){
    Screen.newline();
    Screen.message("Message: Young Driver - please be careful when driving and take care on the roads");
    Screen.newline();
    Screen.cursorpos(10);
    fnVehicleType(String vehicletype);
    int insurance=int cost+3500;
    return(int insurance)
    Screen.message("You are in Category A");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    Screen.write(int insurance);

    }
    else if(age >=17 || age <=25 && yearsdriving <5 && accident=='y' || accident=='Y' && ppoints=='y' || ppoints=='Y'){
    screen.message("Young Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%");
    Screen.message("You are in Category A");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >=17 || age <=25 &&yearsdriving >5 &&accident=='n' || accident=='N'&&ppoints=='n' || ppoints=='N'){
    Screen.newline();
    Screen.message("Message: Young Driver - your driving record is good so far, keep up the good work and be careful when driving and take care on the roads");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category A");
    Screen.newline();
    Screen.message("Congratulations: A discount of 2% is going to be applied to your insurance");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >=17 || age <=25 && yearsdriving >5 &&accident=='y' || accident=='Y'&&ppoints=='y' || ppoints=='Y'){
    Screen.newline();
    Screen.message("Young Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category A");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >=26 || age <=60 && yearsdriving <5 && accident=='n' || accident=='N' && ppoints=='n' || ppoints=='N'){
    Screen.newline();
    Screen.message("Mature Driver - your driving record is good so far, keep up the good work and be careful when driving and take care on the roads");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category B");
    Screen.newline();
    Screen.message("Congratulations: A discount of 3% is going to be applied to your insurance");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >=26 || age <=60 && yearsdriving <5 && accident=='y' || accident=='Y'&& ppoints=='y' || ppoints=='Y'){
    Screen.newline();
    Screen.message("Mature Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category B");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >=26 || age <=60 && yearsdriving >5 && accident=='n' || accident=='N'&& ppoints=='n' || ppoints=='N'){
    Screen.newline();
    Screen.message("Mature Driver - your driving record is good so far, keep up the good work and be careful when driving and take care on the roads");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category B");
    Screen.newline();
    Screen.message("Congratulations: A discount of 4% is going to be applied to your insurance");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }

    else if(age >=26 || age <=60 && yearsdriving >5 && accident=='y' || accident=='Y' && ppoints=='y' || ppoints=='Y'){
    Screen.newline();
    Screen.message("Mature Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category B");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >60 && yearsdriving <5 && accident=='n' || accident=='N' && ppoints=='n' || ppoints=='N'){
    Screen.newline();
    Screen.message("Senior Driver- your driving record is good so far, keep up the good work and be careful when driving and take care on the roads");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category C");
    Screen.newline();
    Screen.message("Congratulations: A discount of 5% is going to be applied to your insurance");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >60&&yearsdriving <5 && accident=='y' || accident=='Y' && ppoints=='y' || ppoints=='Y'){
    Screen.newline();
    Screen.message("Senior Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category C");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >60 && yearsdriving >5 && accident=='n' || accident=='N'&&ppoints=='n' || ppoints=='N'){
    Screen.newline();
    Screen.message("Senior Driver- your driving record is good so far, keep up the good work and be careful when driving and take care on the roads");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category C");
    Screen.newline();
    Screen.message("Congratulations: A discount of 10% is going to be applied to your insurance");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    else if(age >60 && yearsdriving >5 && accident=='y' || accident=='Y' && ppoints=='y' || ppoints=='Y'){
    Screen.newline();
    Screen.message("Senior Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("You are in Category C");
    Screen.newline();
    Screen.message("Sorry: No Discount Applies To You");
    Screen.newline();
    Screen.message("The cost to insure your vehicle is: ");
    }
    }
    }
    Sorry about the wall of text ;(

    I've bolded the bits where I'm stuck, maybe if you can help me clear this bit I'm hoping that will sort my problem out.

    Sorry if it's confusing to look at, if there's anything you need to make it clearer let me know.


  • Registered Users, Registered Users 2 Posts: 3,992 ✭✭✭Korvanica


    The "static void fnVehicleType(String vehicletype)" method should be int not void. You'd probably want to get rid of the static too and just mark it private or public

    So it should be:
    public int fnVehicleType(String vehicletype)
    

    when comparing your strings you must use .equals, not ==.
    vehicletype.equals("motorcycle") || vehicletype.equals("Motorcycle")
    

    you sould also use .equalsIgnoreCase to simplify this down more.
    vehicletype.equalsIgnoreCase("motorcycle")
    

    when you're returning the cost, you have cost in brackets after return -> return(cost); this is wrong, see below.
    return cost;
    

    ..

    i also noticed youve left out a lot of semicolons.


  • Registered Users, Registered Users 2 Posts: 134 ✭✭d4v1d


    another little thing to check...

    if (age >= 26 || age <=60)

    in this case the age will always equate to true. you need to use the && in order to get this to work.

    in the current form, if age == 1 then the first check will equate to false and the second check will equate to true.

    if age == 100 then the first check will equate to true and the second check will never be performed.

    also...
    int insurance=int cost+3500; does not look too healthy either. cost has already been defined as an argument. no need to declare it again.
    int insurance=cost+3500;

    also...
    i see your method calls in the main method do not match the methods you have declared. am example is..
    static void fnVehicleType(String vehicletype)
    in your main method you are calling it using
    fnVehicleType();
    this will not work as the method expects a string to be passed in the function call in the main method.
    as this seems to be a simple homework app what i would do is make vehicleType a class variable and not bother passing it as an argument. below is something like what i mean. i'm assuming all methods and variables remain static just to keep it simple.

    class assignment1{

    private static String vehicleType;

    public static void main(String[]args){
    ...
    fnVehicleType();
    ...
    }

    private static void fnInputData(){
    ...
    String vehicletype; //Variable remove this line
    ...
    Screen.message("Please enter your vehicle type: ");
    vehicletype=Keyboard.readString();
    ...
    }

    private static void fnVehicleType(){
    ...
    }


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    accident=='y' || accident=='Y
    

    you should convert all your input to Uppercase when checking values
    then you would only need
    UCase(accident)=='Y')
    

    much easier to read and debug.
    if(age >=17 || age <=25 &&yearsdriving >5 &&accident=='n' || accident=='N'&&ppoints=='n' || ppoints=='N'
    
    the logic is hard to follow and I bet its not doing what you want it to do.
    Always use brackets when doing boolean checks so
    if(age >=17 || age <=25) &&(yearsdriving >5) &&(accident=='n' || accident=='N')&&(ppoints=='n' || ppoints=='N')
    
    is much more readable.

    I would be inclined to put each boolean check on a new line for readability.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    You need to clean it up as there are too many lines to see clearly.

    For example instead of:
    if(.....){
       YoungDriver;
    } 
    
    

    Then move the young driver code into a private function.

    As someone mentioned put each logical part of your IF statement (if complex) onto its own line with each logical section together.

    For example.
    if(  
        (age >=17 || age <=25) && 
        yearsdriving <5 && 
        (accident=='n' || accident=='N' && ppoints=='n' || ppoints=='N')
    
    ) {
    

    Lastly you need to realise that && is a short circut AND. What this means is the first statement to prove false will drop past the IF statement (nothing else will be checked). So to speed things up move the items likely to hit false to the front.

    Last main thing that pops out is the string comparing.

    accident == 'n'

    This does not do what you think it does. This will check to see if accident is using the same memory as 'n' is using. Depending on the JVM this may or may not work.

    What you should do is.

    accident.equalsIgnoreCase("n")


  • Registered Users, Registered Users 2 Posts: 1,039 ✭✭✭IrishGrimReaper


    Thanks a lot for the replies so far, I'm trying to tidy things up. But my mind is scrambled. I haven't done much on Java so far(as you can probably tell, so I'm a noob at it).
    class assignment1{

    private static String vehicleType;

    public static void main(String[]args){


    fnWelcome();
    fnInputData();
    fnDetermineCategory();

    }

    static void fnWelcome(){ //Welcome Function

    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("Welcome to this Insurance Cost Assesment Program");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("Here is a description of the categorys of insurance offered");
    Screen.newline(3);
    Screen.cursorpos(5);
    Screen.message("Category A: for those in the age group 17 to 25");
    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Category B: for those in the age group 26 to 59");
    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Category C: for those in the age group 60 and over");
    Screen.newline(3);
    Screen.message("There is also a registration fee charged on what vehicle type is being insured");
    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Vehicle Type Registration Fee");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Motorcycle $345");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Car $567");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Van $700");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Truck $765");
    Screen.newline(2);


    }//End Function Welcome

    private static void fnInputData(){

    String personname; //Variable
    int age; //Variable
    int yearsdriving; //Variable
    char accident; //Variable
    char ppoints; //Variable

    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Please enter your name: ");
    personname=Keyboard.readString();
    Screen.cursorpos(5);
    Screen.message("Please enter your age: ");
    age=Keyboard.readInt();
    Screen.cursorpos(5);
    Screen.message("Please Enter The Number Of Years You Are Driving: ");
    yearsdriving=Keyboard.readInt();
    Screen.cursorpos(5);
    Screen.message("Please enter your vehicle type: ");
    vehicletype=Keyboard.readString();
    Screen.cursorpos(5);
    Screen.message("Please enter Y/N if you have had an accident: ");
    accident=Keyboard.readChar();
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Please enter Y/N if you Penalty Points: ");
    ppoints=Keyboard.readChar();
    Screen.newline();



    } // End fnInputData
    static void fnDetermineCategory(int cost,int age, int yearsdriving,char accident,char ppoints){

    String CategoryA;
    String CategoryB;
    String CategoryC;
    String YoungDriverN;
    String YoungDriverY;
    String MatureDriverN;
    String MatureDriverY;
    String SeniorDriverN;
    String SeniorDriverY;
    String NoDiscount;
    String InsureCost;


    CategoryA="You are in category A";
    CategoryB="You are in category B";
    CategoryC="You are in category C";

    YoungDriverN="Message: Young Driver - please be careful when driving and take care on the roads";
    YoungDriverY="Young Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%";
    MatureDriverN="Mature Driver - your driving record is good so far, keep up the good work and be careful when driving and take care on the roads";
    MatureDriverY="Mature Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%";
    SeniorDriverN="Senior Driver- your driving record is good so far, keep up the good work and be careful when driving and take care on the roads";
    SeniorDriverY="Senior Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%";

    NoDiscount="Sorry: No Discount Applies To You";
    InsureCost="The Cost To Insure Your Vehicle Is";


    Screen.message("Determining Category");
    Screen.newline();

    if(
    (age >=17 || age <=25) &&
    (yearsdriving <5) &&
    (UCase(accident)=='N') &&
    (UCase(ppoints)=='N')
    ){


    Screen.message(YoungDriverN);
    Screen.message(CategoryA);
    Screen.message(NoDiscount);
    Screen.message(InsureCost);

    }

    else if(
    (age >=17 || age <=25) &&
    (yearsdriving <5) &&
    (Ucase(accident=='Y') &&
    (Ucase(ppoints=='Y')
    ){


    Screen.message(YoungDriverY);
    Screen.message(CategoryA);
    Screen.message(NoDiscount);
    Screen.message(InsureCost);
    }

    else if(
    (age >=17 || age <=25) &&
    (yearsdriving >5) &&
    (Ucase(accident=='N')&&
    (Ucase(ppoints=='N')
    ){


    Screen.message(YoungDriverN);
    Screen.message(CategoryA);
    Screen.newline();
    Screen.message("Congratulations: A discount of 2% is going to be applied to your insurance");
    Screen.message(InsureCost);
    }

    else if(
    (age >=17 || age <=25) &&
    (yearsdriving >5) &&
    (Ucase(accident=='Y') &&
    (Ucase(ppoints=='Y')
    ){

    Screen.message(YoungDriverY);
    Screen.message(CategoryA);
    Screen.message(NoDiscount);
    Screen.message(InsureCost);
    }

    else if(
    (age >=26 || age <=60) &&
    (yearsdriving <5) &&
    (Ucase(accident=='N')&&
    (Ucase(ppoints=='N')
    ){

    Screen.message(MatureDriverN);
    Screen.message(CategoryB);
    Screen.newline();
    Screen.message("Congratulations: A discount of 3% is going to be applied to your insurance");
    Screen.message(InsureCost);
    }

    else if(
    (age >=26 || age <=60) &&
    (yearsdriving <5) &&
    (Ucase(accident=='Y') &&
    (Ucase(ppoints=='Y')
    ){

    Screen.message(MatureDriverY);
    Screen.message(CategoryB);
    Screen.message(NoDiscount);
    Screen.message(InsureCost);
    }

    else if(
    (age >=26 || age <=60) &&
    (yearsdriving >5) &&
    (Ucase(accident=='N') &&
    (Ucase(ppoints=='N')
    ){

    Screen.message(MatureDriverN);
    Screen.message(CategoryB);
    Screen.newline();
    Screen.message("Congratulations: A discount of 4% is going to be applied to your insurance");
    Screen.message(InsureCost);
    }

    else if(
    (age >=26 || age <=60) &&
    (yearsdriving >5) &&
    (Ucase(accident=='Y') &&
    (Ucase(ppoints=='Y')
    ){

    Screen.message(MatureDriverY);
    Screen.message(CategoryB);
    Screen.message(NoDiscount);
    Screen.message(InsureCost);
    }

    else if(
    (age >60) &&
    (yearsdriving <5) &&
    (Ucase(accident=='N') &&
    (Ucase(ppoints=='N')
    ){

    Screen.message(SeniorDriverN);
    Screen.message(CategoryC);
    Screen.newline();
    Screen.message("Congratulations: A discount of 5% is going to be applied to your insurance");
    Screen.message(InsureCost);
    }

    else if(
    (age >60) &&
    (yearsdriving <5) &&
    (Ucase(accident=='Y') &&
    (Ucase(ppoints=='Y')
    ){

    Screen.message(SeniorDriverY);
    Screen.message(CategoryC);
    Screen.message(NoDiscount);
    Screen.message(InsureCost);
    }

    else if(
    (age >60) &&
    (yearsdriving >5) &&
    (Ucase(accident=='n') &&
    (Ucase(ppoints=='n')
    ){

    Screen.message(SeniorDriverN);
    Screen.message(CategoryC);
    Screen.message("Congratulations: A discount of 10% is going to be applied to your insurance");
    Screen.newline();
    Screen.message(InsureCost);
    }

    else if(
    (age >60) &&
    (yearsdriving >5) &&
    (Ucase(accident=='y') &&
    (Ucase(ppoints=='y')
    ){

    Screen.message(SeniorDriverY);
    Screen.message(CategoryC);
    Screen.message(NoDiscount);
    Screen.message(InsureCost);
    }
    }

    Here is the new code. I honestly don't know how to get the vehicletype part done. I don't want to sound like I want to plagiarize, but can someone post the code on how they would do that particular bit?

    EG: Whatever vehicle type the user enters, it will get the cost of it, then add with the category it is in. It's probably simple for you guys, but I just can't get it by myself :(

    Also the bit where I have used (Ucase(accident=='N') etc is that correct now? I like that way of doing it if it is.


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    UCase is a Visual Basic function and does not exist in Java. You need to either consult your notes or a look at the javadocs for java.lang.String for the correct method to use. Or you could look at several of the previous posts by others who correctly showed you how to do string comparisons.

    Also, you code has several compilation errors. fnDetermineCategory requires several parameters yet you are calling it as fnDetermineCategory(); in your main method.


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Well it is a class assignment so it may be possible that the tutor has given UCase as valid function (which would be really nasty as it breaks coding conventions).

    Even so this would never work in Java.

    Ucase(ppoints=='N')

    Or would be the same as UCase(true)

    As mentioned earlier do not use == to compare strings.


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    Hobbes wrote: »
    Well it is a class assignment so it may be possible that the tutor has given UCase as valid function (which would be really nasty as it breaks coding conventions).

    Even so this would never work in Java.

    Ucase(ppoints=='N')

    Or would be the same as UCase(true)

    As mentioned earlier do not use == to compare strings.

    A previous poster had posted a code snippet with UCase(), which in itself was not a valid java function (unless ucase was a static function within the class).


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,039 ✭✭✭IrishGrimReaper


    Hey guys sorry for the late reply.

    I've been working on the code and this is where I am at the minute. Long post incoming.
    public class altassignment{

    public int Insurance;
    public int InsuranceTotal;
    public int regcost;

    public int CategoryALess5 = 3500;
    public int CategoryAMore5 = 2750;
    public int CategoryBLess5 = 2500;
    public int CategoryBMore5 = 2000;
    public int CategoryCLess5 = 2000;
    public int CategoryCMore5 = 1800;
    // Category Costs to use in calculations

    public double TwoPercent = 0.02;
    public double ThreePercent = 0.03;
    public double FourPercent = 0.04;
    public double FivePercent = 0.05;
    public double TenPercent = 0.10;
    public double TwentyFivePercent = 0.25;
    // Percentange discounts and accident and penalty points fee

    public String personname, vehicletype; //Variable The persons name and the vehicletype
    public int age, yearsdriving; //Variable The age of the person and how many years driving
    public char accident, ppoints; //Variable If the person has had an accident or penalty points


    public String CategoryA ="You are in category A";
    public String CategoryB ="You are in category B";
    public String CategoryC ="You are in category C";
    //Category Strings


    public String YoungDriverN="Message: Young Driver - please be careful when driving and take care on the roads";
    public String YoungDriverY="Young Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%";
    public String MatureDriverN="Mature Driver - your driving record is good so far, keep up the good work and be careful when driving and take care on the roads";
    public String MatureDriverY="Mature Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%";
    public String SeniorDriverN="Senior Driver- your driving record is good so far, keep up the good work and be careful when driving and take care on the roads";
    public String SeniorDriverY="Senior Driver- you have been part of an accident or have penalty points, you will face an additional charge of 25%";

    public String NoDiscount="Sorry: No Discount Applies To You";
    public String InsureCost="The Cost To Insure Your Vehicle Is";
    //Driver Message Strings



    public static void main(String[]args){


    fnWelcome();
    fnInputData();
    fnCalculateRegFee();
    fnDetermineCategory();


    }

    public void fnWelcome(){//Start

    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("Welcome to this Insurance Cost Assesment Program");
    Screen.newline();
    Screen.cursorpos(10);
    Screen.message("Here is a description of the categorys of insurance offered");
    Screen.newline(3);
    Screen.cursorpos(5);
    Screen.message("Category A: for those in the age group 17 to 25");
    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Category B: for those in the age group 26 to 59");
    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Category C: for those in the age group 60 and over");
    Screen.newline(3);
    Screen.message("There is also a registration fee charged on what vehicle type is being insured");
    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Vehicle Type Registration Fee");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Motorcycle $345");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Car $567");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Van $700");
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Truck $765");
    Screen.newline(2);


    }//End

    public void fnInputData(){

    Screen.newline(2);
    Screen.cursorpos(5);
    Screen.message("Please enter your name: ");
    personname=Keyboard.readString();
    Screen.cursorpos(5);
    Screen.message("Please enter your age: ");
    age=Keyboard.readInt();
    Screen.cursorpos(5);
    Screen.message("Please Enter The Number Of Years You Are Driving: ");
    yearsdriving=Keyboard.readInt();
    Screen.cursorpos(5);
    Screen.message("Please enter your vehicle type: ");
    vehicletype=Keyboard.readString();
    Screen.cursorpos(5);
    Screen.message("Please enter Y/N if you have had an accident: ");
    accident=Keyboard.readChar();
    Screen.newline();
    Screen.cursorpos(5);
    Screen.message("Please enter Y/N if you Penalty Points: ");
    ppoints=Keyboard.readChar();a
    Screen.newline();



    } // End fnInputData

    public int fnCalculateRegFee(String vehicletype){


    if (vehicletype == "car"||"Car") {
    regcost = 567;
    } else if (vehicletype == "motorcycle"||"motorcycle") {
    regcost = 345;
    } else if (vehicletype == "truck"||"Truck") {
    regcost = 765;
    } else if (vehicletype == "van"||"Van") {
    regcost = 700;
    } else {
    regcost = 0;
    }

    return regcost;

    }// end


    public fnDetermineCategory(){


    Screen.message("Determining Category");
    Screen.newline();



    if (age >=17)
    if (age <=25)
    if (yearsdriving <5)
    if (accident == 'N'||'n')
    if (ppoints == 'N'||'n'){
    fnCalculateRegFee();
    InsuranceTotal = regcost + CategoryALess5;
    return InsuranceTotal;

    Screen.message(YoungDriverN);
    Screen.message(CategoryA);
    Screen.message(NoDiscount);
    Screen.message(personname);
    Screen.message(vehicletype);
    Screen.message(InsureCost);
    Screen.message(InsuranceTotal);
    }

    else if (age >=17)
    if (age <=25)
    if (yearsdriving <5)
    if (accident=='Y'||'y')
    if (ppoints=='Y'||'y'){
    fnCalculateRegFee();
    Insurance = regcost + CategoryALess5 * TwentyFivePercent;
    return Insurance;
    InsuranceTotal = regcost + CategoryALess5 - Insurance;
    return InsuranceTotal;



    Screen.message(YoungDriverY);
    Screen.message(CategoryA);
    Screen.message(personname);
    Screen.message(vehicletype);
    Screen.message(NoDiscount);
    Screen.message(InsuranceTotal);
    }

    else if (age >=17)
    if (age <=25)
    if (yearsdriving >5)
    if (accident=='N'||'n')
    if (ppoints=='N'||'n'){
    fnCalculateRegFee();
    Insurance = regcost + CategoryAMore5 * TwoPercent;
    return Insurance;
    InsuranceTotal = regcost + CategoryAMore5 - Insurance;
    return InsuranceTotal;

    Screen.message(YoungDriverN);
    Screen.message(CategoryA);
    Screen.message(personname);
    Screen.message(vehicletype);
    Screen.message("Congratulations: A discount of 2% is going to be applied to your insurance");
    Screen.message(InsuranceTotal);
    }

    else if (age >=17)
    if (age <=25)
    if (yearsdriving >5)
    if (accident=='Y'||'y')
    if (ppoints=='Y'||'y'){
    fnCalculateRegFee();
    Insurance = regcost + CategoryAMore5 * TwentyFivePercent;
    return Insurance;
    InsuranceTotal = + regcost + CategoryAMore5 + Insurance;
    return InsuranceTotal;

    Screen.message(YoungDriverY);
    Screen.message(CategoryA);
    Screen.message(personname);
    Screen.message(vehicletype);
    Screen.message(NoDiscount);
    Screen.message(InsuranceTotal);
    }

    else if (age >=26)
    if (age <=60)
    if (yearsdriving <5)
    if (accident=='N'||'n')
    if (ppoints=='N'||'n'){
    fnCalculateRegFee();
    Insurance = regcost + CategoryBLess5 * ThreePercent;
    return Insurance;
    InsuranceTotal = + regcost + CategoryBLess5 - Insurance;
    return InsuranceTotal;

    Screen.message(MatureDriverN);
    Screen.message(CategoryB);
    Screen.message(personname);
    Screen.message(vehicletype);
    Screen.message("Congratulations: A discount of 3% is going to be applied to your insurance");
    Screen.message(InsuranceTotal);
    }

    else if (age >=26)
    if (age <=60)
    if (yearsdriving <5)
    if (accident=='Y'||'y')
    if (ppoints=='Y'||'y'){
    fnCalculateRegFee();
    Insurance = regcost + CategoryBMore5 * TwentyFivePercent;
    return Insurance;
    InsuranceTotal = + regcost + CategoryBMore5 + Insurance;
    return InsuranceTotal;

    Screen.message(MatureDriverY);
    Screen.message(CategoryB);
    Screen.message(personname);
    Screen.message(vehicletype);
    Screen.message(NoDiscount);
    Screen.message(InsuranceTotal);
    }

    else if (age >=26)
    if (age <=60)
    if (yearsdriving >5)
    if (accident=='N'||'n')
    if (ppoints=='N'||'n'){
    fnCalculateRegFee();
    Insurance = regcost + CategoryBMore5 * FourPercent;
    return Insurance;
    InsuranceTotal = regcost + CategoryBMore5 - Insurance;
    return InsuranceTotal;

    Screen.message(MatureDriverN);
    Screen.message(CategoryB);
    Screen.message(personname);
    Screen.message(vehicletype);
    Screen.message("Congratulations: A discount of 4% is going to be applied to your insurance");
    Screen.message(InsuranceTotal);
    }

    else if (age >=26)
    if (age <=60)
    if (yearsdriving >5)
    if (accident=='Y'||'y')
    if (ppoints=='Y'||'y'){
    fnCalculateRegFee();
    Insurance = regcost + CategoryBMore5 * TwentyFivePercent;
    return Insurance;
    InsuranceTotal = regcost + CategoryBMore5 + Insurance;
    return InsuranceTotal;

    Screen.message(MatureDriverY);
    Screen.message(CategoryB);
    Screen.message(personname);
    Screen.message(vehicletype);
    Screen.message(NoDiscount);
    Screen.message(InsuranceTotal);
    }

    else if (age >60)
    if (yearsdriving <5)
    if (accident=='N'||'n')
    if (ppoints=='N'||'n'){
    fnCalculateRegFee();
    Insurance = regcost + CategoryCLess5 * FivePercent;
    return Insurance;
    InsuranceTotal = regcost + CategoryCLess5 - Insurance;
    return InsuranceTotal;

    Screen.message(SeniorDriverN);
    Screen.message(CategoryC);
    Screen.message(personname);
    Screen.message(vehicletype);
    Screen.message("Congratulations: A discount of 5% is going to be applied to your insurance");
    Screen.message(InsuranceTotal);
    }

    else if (age >60)
    if (yearsdriving <5)
    if (accident=='Y'||'y')
    if (ppoints=='Y'||'y'){
    fnCalculateRegFee();
    Insurance = regcost + CategoryCLess5 * TwentyFivePercent;
    return Insurance;
    InsuranceTotal = regcost + CategoryCLess5 + Insurance;
    return InsuranceTotal;

    Screen.message(SeniorDriverY);
    Screen.message(CategoryC);
    Screen.message(personname);
    Screen.message(vehicletype);
    Screen.message(NoDiscount);
    Screen.message(InsuranceTotal);

    }
    else if (age >60)
    if (yearsdriving >5)
    if (accident=='n')
    if (ppoints=='n'){
    fnCalculateRegFee();
    Insurance = regcost + CategoryCMore5 * TenPercent;
    return Insurance;
    InsuranceTotal = regcost + CategoryCMore5 - Insurance;
    return InsuranceTotal;

    Screen.message(SeniorDriverN);
    Screen.message(CategoryC);
    Screen.message(personname);
    Screen.message(vehicletype);
    Screen.message("Congratulations: A discount of 10% is going to be applied to your insurance");
    Screen.newline();
    Screen.message(InsuranceTotal);
    }

    else if (age >60)
    if (yearsdriving >5)
    if (accident=='Y'||'y')
    if (ppoints=='Y'||'y'){
    fnCalculateRegFee();
    Insurance = regcost + CategoryCLess5 * TwentyFivePercent;
    return Insurance;
    InsuranceTotal = regcost + CategoryCLess5 + Insurance;
    return InsuranceTotal;

    Screen.message(SeniorDriverY);
    Screen.message(CategoryC);
    Screen.message(personname);
    Screen.message(vehicletype);
    Screen.message(NoDiscount);
    Screen.message(InsuranceTotal);
    }
    }
    }
    Now I have a question, for this part here.
    public fnDetermineCategory(){
    Do I need to include all the variables that will go in it, into the brackets such as:
    public int Insurance;
    public int InsuranceTotal;
    public int regcost;

    public int CategoryALess5 = 3500;
    public int CategoryAMore5 = 2750;
    public int CategoryBLess5 = 2500;
    public int CategoryBMore5 = 2000;
    public int CategoryCLess5 = 2000;
    public int CategoryCMore5 = 1800;

    So it will look like
    public fnDetermineCategory(int Insurance, int InsuranceTotal, int regcost){
    And so forth, basically there will be loads, can I put all them into a, method so to speak? and just use the method name in there instead.

    I know the code is sloppy, but I'll improve in time :)

    There's a lot coming at me in the past few weeks from java. Could anyone explain laymans terms(or link a nice tutorial) of the following things:

    functions
    methods
    booleans

    Will the way I have the "Calculateregfee" function work like it is?
    if (age >=17)
    if (age <=25)
    if (yearsdriving <5)
    if (accident == 'N'||'n')
    if (ppoints == 'N'||'n'){
    fnCalculateRegFee();
    InsuranceTotal = regcost + CategoryALess5;
    return InsuranceTotal;

    Screen.message(YoungDriverN);
    Screen.message(CategoryA);
    Screen.message(NoDiscount);
    Screen.message(personname);
    Screen.message(vehicletype);
    Screen.message(InsureCost);
    Screen.message(InsuranceTotal);
    }

    p.s: I know I have a lot of public expressions, but I'm just happy enough with getting the program to work for now then I can clean it up properly once I know I have it working.


  • Registered Users, Registered Users 2 Posts: 4,277 ✭✭✭km991148


    Edit - Long rambling post ahead - hopefully it helps, do not mean to sound like I am beating on op!

    Hi,

    very short answer - yes you can create methods like this and pass parameters.

    Longer, hopefully more helpful answer..

    Can I ask what course you are doing and what year you are in? the reason I ask is if you are trying to become a programmer/developer etc then there are many basics that you seem to be completely missing (I dont mean this to sound harsh, everyone starts at the same spot with no knowledge!). If you have time then I suggest that you look at some basic concepts as if you keep on at it the way you are you will waste much time and have very difficult to read (and therefore maintain - especially for others looking at it). Also (again if you have time for this assignment) once you have a basic understanding of programming in a OO language such as Java, do some design on the code. Launching in head first with little understanding of what you are trying to achieve is often counterproductive (of course, you cant understand any system fully and must start at some point, and be prepared to refactor often, but I will leave this point for now).
    If you don't have that much time, get down to the library and start looking at some basic books and the course texts. Spend a few hours and t will help massively. Next I would write down (Pen and paper!) the calculatons and work out the algorithm required to produce the premium. Break it down as much as possible, for example while splitting the code into private methods (think of a method as a function, such as fnDetermineCategory in your example) there is little benefit if you have one huge gigantic block.

    For example, it looks as if there is a discount to be applied based on penalty points regardless of driver age, therefore it would make sense to have a method:

    public int ApplyDiscount(int premium, bool hasPoints){

    if(!hasPoints){
    // Calculate premium as w edont have points- example here is 5%
    // off (hopefully maths is bad..)
    return premium - premium/100*5;
    }
    // Driver has points, do the points calc (i.e. no discount here for example)
    return premium;
    }

    This method can then be used after you have worked out a premium.


    Ok.. I will wrap this up now as I have rambled a bit and it is difficult to explain desing with one example. hopefully this helps a little. Post up some specific questions, but please do a bit of reading, at least to get used to the basic terminology (even the wikipedia has extensive articles of programming, OO design, Java and Java syntax).

    If you dont care much for programming and this is a quick fix, one time course then hopefully you can get help, if you are seriously wanting to code for cash then I suggest you spend some time with some books - either way good luck!


  • Registered Users, Registered Users 2 Posts: 134 ✭✭d4v1d


    have you written any smaller little test apps that you can use to just test the 'if' statements? i would suggest you do this before trying to compile/debug your application in it's entirety. it will also help you spot a lot of problems in your current code.

    taking one of your if statements, as from your code, put it into a test app and run it. the code below is invalid and going to fail. you need to figure out why.

    once you get things like this working you can then see what's wrong in your code and fix it.
    public class TestIf{
    
    public static void main(String[] args){
    char accident = 'N';
    
    if (accident == 'N'||'n')
    System.out.println("Accident = no");
    else
    System.out.println("Accident = yes");
    
    }
    }
    

    try start small and build it up.


Advertisement