Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to use constructors and class objects together.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to use constructors and class objects together.

    I have an assignment to make a program that will use classes and objects to set up a piggy bank system.
    It should have a separate class that contains all of the actual calculations. I've been working on it and will post my progress below.
    I know (I think anyway) that I have to use objects in the main class to access the constructors of the second.
    This is as far as my understanding goes. I need help figuring out the syntax for doing so.

    My main class is just a switch that will tell my other class which calculations need to be made.
    There has to be a way to add a penny, nickel, dime, quarter, and to view the total or exit (just the way it was assigned).

    The code for my main class so far:

    // ********************************************************************************
    //
    //    PROGRAM: My Savings
    //
    //    PROGRAMMER:  IkeIII
    //    CLASS:       CSC 2623
    //    SOURCES:     N/A
    //
    //    DESCRIPTION: Uses classes to make simulate a piggy bank.
    //
    // ******************************************************************************** 
     
    package mysavings;
     
    import java.util.Scanner;//allows inputs
     
    public class MySavings {
     
        public static void main(String[] args) {
     
            Scanner input = new Scanner(System.in);
            int choice = input.nextInt();
     
            System.out.print("1. Show total in bank.");
            System.out.print("2. Add a penny.");
            System.out.print("3. Add a nickel.");
            System.out.print("4. Add a dime.");
            System.out.print("5. Add a quarter.");
            System.out.print("6. Take money out of bank.");
            System.out.print("Enter 0 to quit.");
            System.out.print("Enter your choice: ");
     
            while(choice == 0){
                switch(choice){
                    case 1: break;
                    case 2: break;
                    case 3: break;
                    case 4: break;
                    case 5: break;
                    case 6: break;
                    default: break;
            }
            }
            //Declare a class object, savings
            PiggyBank obj = new PiggyBank();
     
            //Initialize a class object, savings
            obj.setSavings(0);
     
     
        } //End of main
    } // End of test circle

    And the code for my sub-class:

     
    // ********************************************************************************
    //
    //    PROGRAM: Piggy Bank (Class)
    //
    //    PROGRAMMER:  IkeIII
    //    CLASS:       CSC 2623
    //    SOURCES:     N/A
    //
    //    DESCRIPTION: Uses classes to make simulate a piggy bank.
    //
    // ******************************************************************************** 
     
    package mysavings;
     
    public class PiggyBank {
     
        public double savings;
     
        //Constructor
        public PiggyBank(){
            savings = 0;
        }//End of constructor
        //CONSTRUCTOR With a parameter
        public PiggyBank( double s ) {
            savings = s;
     
        }//End of constructor with a parameter
     
        //METHOD: Set the raius of the circle.
        public void setSavings( double newSavings ) {
            savings = newSavings;
     
            //Display output.
        }//End of setRadius
     
        //METHOD: Get the current of the radius
        public double getSavings() {
            return (savings);}
     
    }//End of PiggyBank


    --- Update ---

    I know how to do most of the main stuff. Even if I haven't corrected some of it above. It's only the stuff dealing with moving between the two classes that has me confused if anyone can explain.


  2. #2
    Member
    Join Date
    Jan 2013
    Posts
    34
    My Mood
    Busy
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: How to use constructors and class objects together.

    'Moving between two classes'... Do you mean to be able to access stuff from other classes and/or call methods/constructors?
    ----------------------

    If so, you would access them usually by instantating an object of the class you want to access. Like this:

    MyClass myobj = new MyClass(); //This is calling the default constructor.

    Visit The Java™ Tutorials for tutorials on syntax and other stuff.
    For methods, after you've instantiated your object, you can call methods.

    myobj.maiMethod(); //Calling a method.

    Also some tips: if you aren't limited to adding those classes only, you should make more for yourself, making it easier for somethings. For example, creating seperate classes like a Dime class, Quarter class, etc. and having a method in some other class to add those of type Dime, Quarter etc. and increments a number based on an integer value in the Coin classes.
    Good luck!
    I hope I gave you some help.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use constructors and class objects together.

    Okay, so I have a lot more written now. I'm still having trouble with the first part accessing the second. I have added the obj to access it, but I keep getting error messages for my variable so I have it initialized in both classes at the moment. I know this won't work, but I got tired of seeing the red lines, lol. It doesn't seem to run my switch statement for some reason. Here is my code to date.

     
    package mysavings;
     
    import java.util.Scanner;//allows inputs
     
    public class MySavings {
     
        public static void main(String[] args) {
     
            System.out.println("1. Show total in bank.");
            System.out.println("2. Add a penny.");
            System.out.println("3. Add a nickel.");
            System.out.println("4. Add a dime.");
            System.out.println("5. Add a quarter.");
            System.out.println("6. Take money out of bank.");
            System.out.println("Enter 0 to quit.");
            System.out.println("Enter your choice: ");
     
            double savings = 0;
     
            PiggyBank obj = new PiggyBank();
     
            Scanner input = new Scanner(System.in);
            int choice = input.nextInt();
     
            while(choice != 0){
                switch(choice){
                    case 1: System.out.print(obj.getSavings()); break;
                    case 2: obj.addPenny(savings); break;
                    case 3: break;
                    case 4: break;
                    case 5: break;
                    case 6: break;
                    default: break;
                }//end of switch
            }//end of while
     
        } //End of main
    } //End of MySavings

    and this is the code I've completely for my other class

     
    package mysavings;
     
    public class PiggyBank {
     
        public double savings;
     
        //Constructor
        public PiggyBank(){
            savings = 0;
        }//End of constructor
     
        public double addPenny( double savings ) {
            savings = savings + .01;
            return savings;
            //Display output.
        }//End of set
     
        public double addNickel( double savings ) {
            savings = savings + .05;
            return savings;
     
            //Display output.
        }//End of set
     
        public double addDime( double savings ) {
            savings = savings + .1;
            return savings;
     
        }//End of set
     
        public double addQuarter( double savings ) {
            savings = savings + .25;
            return savings;
     
        }//End of set
     
        //METHOD: Get the current of the radius
        public double getSavings() {
            return savings;}
     
    }//End of PiggyBank

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: How to use constructors and class objects together.

    I keep getting error messages for my variable
    What error messages were you getting?
    What variable is the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use constructors and class objects together.

    I only had the "savings" variable declared in the second block of code before, so when I called the objects from the other class within my switch I couldn't use it as the parameter. I put another initialization of the variable in the MySavings class to get rid of the red lines, but now I think there is just more than one variable with that name, which I don't believe will work.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: How to use constructors and class objects together.

    The variable named savings in one class is entirely separate from a variable with the same name in another class.
    The PiggyClass's method: getSaving() will return the value of savings in the PiggyBank object to its caller.
    Assign the value that is returned to a variable in the method that calls the getSavings() method and then that method will have a variable with the same value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. To Make class immutable which has ref to other mutable objects
    By tcstcs in forum Object Oriented Programming
    Replies: 1
    Last Post: May 4th, 2012, 10:42 AM
  2. calling objects in a differnt class
    By jack_nutt in forum Object Oriented Programming
    Replies: 12
    Last Post: July 8th, 2011, 01:57 PM
  3. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM
  4. instantiating class objects from an array
    By BadAnti in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 12th, 2011, 03:27 PM
  5. How to store objects from a class inn an array?
    By dironic88 in forum Object Oriented Programming
    Replies: 1
    Last Post: April 7th, 2011, 02:42 PM

Tags for this Thread