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 - Question about constructors...

  • 28-02-2009 9:46am
    #1
    Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭


    Hi,

    I'm a little confused about a worksheet im working on at the moment.

    From Question 1 I have created a bank account class with the following constructors:
    // Default Constructor
         public BankAccount()
         {
         }
    // User Defined Constructor
         public BankAccount(String name, String accountNumber, double balance)
         {
             this.name = name;
             this.accountNumber = accountNumber;
             this.balance = balance;
         }
    

    In question 2 I am told to add a default constructor to the bank class that will initialise everyones account with a user defined value.

    I'm not entirely sure about this..

    From what I understand I must define a constructor that will take arguments (user values) and initialize the class variable to these. But that would not be a default constructor, because default constructors don't take argument...and there can be only 1 default constructor per class

    Maybe I have it done already. From my code, i'm sure the user defined constructor is what I must write but it's not a default constructor because it takes arguments.

    I hope this is clear enough.

    Thanks!

    Another thought: In the default constructor (already defined) can I access variables that are stored in the main method.. I'm going to try that now.


Comments

  • Registered Users, Registered Users 2 Posts: 1,352 ✭✭✭Phibsboro


    I'd like to see the full question! A default constructor is, by definition, one that is added implicitly if you don't have any constructor specified. All it does is call the super class "no arguments" constructor. So asking you to provide a default constructor doesn't make sense. And calling your own "no arguments" constructor a "default constructor" in yours comments isn't really correct.

    From a sun tutorial...

    You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.

    So as it stands it looks like the question is wrong...

    c


  • Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭techguy


    Here's what i've done now in the existing default constructor:
    // Default Constructor
         public BankAccount()
         {
             Scanner input = new Scanner(System.in);
             System.out.print("*NAME*: ");name = input.nextLine();
             System.out.print("*ACCOUNT NUMBER* :"); accountNumber = input.nextLine();
             System.out.print("*Balance* :"); balance = input.nextDouble();
         }
    

    What do you guys think?


  • Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭techguy


    Phibsboro wrote: »
    I'd like to see the full question!

    Worksheet 3:
    Classes, Constructors and Methods


    Question 1:
    Declare a class for a bank account. The attributes should be name, account number and balance. Add default and user defined constructors. You should also add the methods debit( ) and credit( ) to add money to the account and to subtract money from the account. Include also in your class a display method to write the account details to the screen. Write a program to test your class

    Question 2:
    Add a default constructor to the bank example in question 1 that initialises everyone’s account with a value entered by the user on initialization of the account.
    All the correct terminology like superclass is still a bit new to me so your answer is a little confusing but I think i get the jist!

    I've provided the questions anyway so you can have a look for yourself!

    I think the point of this exercise size is to get us used to using constructors and the like..


  • Registered Users, Registered Users 2 Posts: 1,352 ✭✭✭Phibsboro


    techguy wrote: »
    Here's what i've done now in the existing default constructor:
    // Default Constructor
         public BankAccount()
         {
             Scanner input = new Scanner(System.in);
             System.out.print("*NAME*: ");name = input.nextLine();
             System.out.print("*ACCOUNT NUMBER* :"); accountNumber = input.nextLine();
             System.out.print("*Balance* :"); balance = input.nextDouble();
         }
    

    What do you guys think?

    Nooooooooooooooo! :D I can't imagine thats what the question is trying to you to do. Post up the question...


  • Registered Users, Registered Users 2 Posts: 1,352 ✭✭✭Phibsboro


    Crazy question! I'd find a better source of questions and move on :D

    There is no way you should be taking user input in the constructor (does that even work?!). If you do want to do something like that, have the calling program ask for the input and then pass that into a constructor when initialising the object.

    But seriously, tear up that worksheet and move on...

    c


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭techguy


    Phibsboro wrote: »
    There is no way you should be taking user input in the constructor (does that even work?!). If you do want to do something like that, have the calling program ask for the input and then pass that into a constructor when initialising the object.
    c

    Thats what I was thinking, it works but seems odd! I just did that because I thaught the question wanted a constructor with no arguments but still takes user input.

    Lol, that is a college worksheet im sure it has it's purposes!

    Thanks for the advice..im sre my work will suffice for these questions. But im not sure because the questions seem to be pretty vague!!


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    I don't really understand the confusion. They want you to do exactly what the question says.

    The code you've given is wrong and shows a lack of understanding of the basics. :) No offence.

    As you know, you can have loads of constructors. So, in main() you ask the user for a value, and then as you create each object you pass that value as an argument. The constructor then uses that argument to initialise a variable.

    In pseudocode -

    [php]
    someClass {
    constructor(int value) {
    x = value
    }
    private x
    }

    main() {
    read in a
    object1 = new someClass(a)

    read in b
    object2 = new someClass(b)
    }

    [/php]


  • Registered Users, Registered Users 2 Posts: 843 ✭✭✭eoinbn


    AARRRGH wrote: »
    I don't really understand the confusion. They want you to do exactly what the question says.

    The code you've given is wrong and shows a lack of understanding of the basics. :) No offence.

    As you know, you can have loads of constructors.

    The question says the default constructor which means he can't pass the values into the constructor as arguments. However it probably is a misprint.

    OP: The reason people here are confused/shocked is because it would never be done like that. You would never have your user interface embedded in your 'business logic'.

    For example a bank will have ONE 'database model' storing all the user info. There is many ways to access this info- online banking, ATM, staff access etc. When you are 'logging off' at an ATM it will ask you do you want an advice slip. If that question was asked in the same method(java) that does the logging off then it's possible that it would also ask you do you want an advice slip when your logoff from online banking!


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Oops missed the "default" bit.

    It's a misprint. He means "add a constructor" not "add a default constructor".


Advertisement