Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8616021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:31:17+00:00 2026-06-12T05:31:17+00:00

I think there are a few fundamental problems with my code here. I’m not

  • 0

I think there are a few fundamental problems with my code here. I’m not too familiar with the java syntax, so I’m not too sure where I’ve gone wrong. Any help would be greatly appreciated.

I have tried to use a constructor in the middle, and a few accessors towards the bottom, but I think I have over-complicated it for myself.

import java.util.Scanner;    
public class FerryBooking {

    public static void main(String args[]){
        class VehicleBooking {
            private String booking_ID = new String("");
            private String registration = new String("");
            private String make_model = new String("");
            private int number_passengers = 1;
            private boolean insurance_choice = false;
            private boolean insurance_flag = false;

            public static final int booking_fee= 100;
            public static final int extra_passenger = 50;
            public static final int insurance_fee = 50;

            VehicleBooking() {
                Scanner input = new Scanner(System.in);
                Scanner scan = new Scanner(System.in);

                System.out.print("Enter booking ID");
                booking_ID = input.next();
                System.out.print("Enter registration number");
                registration = input.next();
                System.out.print("Enter vehicle make/model");
                make_model = input.next();
                System.out.print("Enter number of passengers");
                number_passengers = scan.nextInt();
            }

            public String getBookingID(){
                return booking_ID;
            }
            public String getRegistration(){
                return registration;
            }
            public String getMakeModel(){
                return make_model;
            }
            public int getPassengers(){
                return number_passengers;
            }
            public boolean getInsurance(){
                return insurance_choice;
            }
            public boolean addInsurance(){
                insurance_choice = true;
                if (insurance_flag = false) {
                    insurance_flag = true;
                    return true;
                } else if (insurance_flag = true) {
                    return false;
                }
                return true;
            }
            public double getBookingFee(){
                int final_cost = booking_fee + (getPassengers()*extra_passenger);
                if (insurance_choice = true){
                    final_cost = final_cost + insurance_fee;
                }
                return final_cost;
            }
        }
    }
}

–EDIT–

I have re-written a lot of the code, and sized it down to where my big issue was; the constructor. However I am getting an error related to the constructor with the code provided to me.

import java.util.Scanner;

public class VehicleBooking {

    private String booking_ID = "";
    private String registration = "";
    private String make_model = "";
    private int number_passengers = 1;
    private boolean insurance_choice = false;

    public static final int BOOKING_FEE= 100;
    public static final int EXTRA_PASSENGER = 50;
    public static final int INSURANCE_FEE = 50;

    public VehicleBooking(String booking_ID1, String registration1,  String make_model1, int number_passengers1) {

        /** Initialise the variables **/
        booking_ID = booking_ID1;
        registration = registration1; 
        make_model = make_model1;
        number_passengers = number_passengers1;
    }

    public static void main(String args[]) {
        VehicleBooking vb = new VehicleBooking(booking_ID1, registration1, make_model1, number_passengers1);
    }       
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:31:18+00:00Added an answer on June 12, 2026 at 5:31 am

    First don’t initialize a String like the one below: –

    private String booking_ID = new String("");
    

    Rather use String.valueOf() or just assign an empty string to variable: –

    private String booking_ID = "";
    

    Second, make a habit to declare your CONSTANTS with UPPER_CASE letter: –

    public static final int BOOKING_FEE = 100;
    

    Third, take a look at your constructor: –

        VehicleBooking()  {
            Scanner input = new Scanner(System.in);
            Scanner scan = new Scanner(System.in);
    
            System.out.print("Enter booking ID");
            booking_ID = input.next();
            System.out.print("Enter registration number");
            registration = input.next();
            System.out.print("Enter vehicle make/model");
            make_model = input.next();
            System.out.print("Enter number of passengers");
            number_passengers = scan.nextInt();
        }
    

    You should never do I/O operation in your constructor.. Constructors are used to initialize the state of the object. It’s sole purpose is to initialize.

    For I/O purpose, make different method readInput() and call it after your object is created..

    Another thing: – Here’s your if-else-if block you have used..

             if (insurance_flag = false) {
                 insurance_flag = true;
                 return true;
             } else if (insurance_flag = true) {
                 return false;
             }
    

    In this code, your if is always false and your else if is always true.. Because you are actually assigning these values to your insurance_flag.. You should use == for comparison purpose..

    So, use if (insurance_flag == false).. In fact you don’t need to compare with a boolean literal…

    just use: – if (!insurance_flag).. They are equivalent..

    Ideally you should change that method containing your above if to the one below: –

           public boolean addInsurance(){
                boolean returnValue = !insurance_flag;
                insurance_flag = true;
                return returnValue;
           }
    

    Because that is what your method is doing, but in an odd way..

    You can also replace final_cost = final_cost + insurance_fee; with the below code: –

    final_cost += insurance_fee;
    

    By using it this way, final_cost will not be evaluated twice..

    EDIT ** : –

    Your constructor should look like this: –

    public VehicleBooking(String bookingId, String registration, String makeModel, 
                          String numberOfPassengers)  {
    
        /** Initialize the instance variables **/
        /** this represent the reference to current object **/
        this.booking_ID = bookingId;
        this.registration = registration; 
        this.make_model = makeModel;
        this.number_passengers = numberOfPassengers;
    }
    

    So, you’re actually passing those values you read, into the constructor as parameter, and initializing your instance attributes, with those parameters..

    I think this would clear your doubt..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think there should be a tool to do so ? is anyone here
Edit I think there is some confusion, I am not using both of the
I asked a similar question a few hours ago, but I think there was
I've read a few posts on this, but there's still one thing that's not
I think there's a gap in my mental model of WCF authentication, hoping someone
I think there is a problem when I echo $whereArray and orderByArray. If I
I think there is a simple answer to this, but for some reason I
I think there is something I don't understand about same origin limitation for XMLHttpRequest
With 32 bit floats I think there is something like 2^31 - 1 representable
I've been using the search function but I think there isn't any related question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.