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: Don't understand void methods, need help!

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Don't understand void methods, need help!

    I have an assignment which consists of obtaining a balance from the user, then depending on the type of transaction, return the results of their balance. It's a transaction program.

    My current code:

    import javax.swing.JOptionPane;
    public class Assignment1 
    {
     
     
     
           public static CheckingAccount info;
     
     
     
         public static void main(String[] args)
        {
           String balance, transcode, transamount, message;
     
           CheckingAccount info;
     
           double ibalance;
     
     
           balance = JOptionPane.showInputDialog ("Enter your initial balance: ");
            ibalance = Double.parseDouble(balance);
     
            info = new CheckingAccount(ibalance);
     
           do
           {
            int tcode;   
            tcode = getTransCode();
     
            if (tcode == 1)
            {
     
             double tamount;
             tamount = getTransAmt();
     
     
             message = "Transaction: Check in the amount of $" + tamount+
                     "\n" +
                       "Current balance: $" + info.setBalance() +
     
     
     
            public static int getTransCode()
            {
                int tcode;
                String transcode;
                transcode = JOptionPane.showInputDialog ("Enter your transaction" + 
                       " code:");
                 tcode = Integer.parseInt(transcode);
     
                 return tcode;
            }
     
            public static double getTransAmt()
            {
                  double tamount;
                  String transamount;
             transamount = JOptionPane.showInputDialog ("Enter your trans amount:");
             tamount = Double.parseDouble(transamount);
     
             return tamount;
            }
     
            public static _________ processCheck(___________)
            {
            }
            public static __________ processDeposit(___________)
            {
            }
     
     
     
     
        }
    }

    public class CheckingAccount 
    {
        private double balance;
        private double totalServiceCharge;
     
        public CheckingAccount(double number)
        {
     
          number = balance;
     
            totalServiceCharge= number+0.10;
     
     
        }
        public static double getBalance()
        {
            return balance;
     
     
     
        }
        public void setBalance(double transAmt, int tCode, double Cbalance)
        {
               if(tCode == 1)
               {
               double tamount;
               double cbalance;
               cbalance = Cbalance;
               double servicecharge;
               tamount = transAmt;
     
               cbalance = balance-tamount;
     
     
               }
               else if(tCode == 2)
               {
                   double cbalance;
                   double tamount;
                   tamount = transAmt;
                   cbalance = balance+tamount;
               }
     
     
        }
     
        public getServiceCharge()
        {
     
          return totalServiceCharge;
     
        }
        public void setServiceCharge(double currentServiceCharge)
        {
            totalServiceCharge = currentServiceCharge;
            if(tcode ==1)
        }
     
     
        }
    }


    My question is nothing about errors or compiling.

    1) As you can see on the first code, right after I construct the if loop I begin to input the current balance to the user.

    How do I input the result of the current balance to the user? If you check the 2nd code, on the setBalance method the current balance is shown there. Where cbalance = balance-tamount (This is because tcode == 1 represents the user checking in an amount, thus the balance -tamount)

    how do i show the cbalance to the user? The method is a void, so it cant return a value, so how do I show the user their current balance? do i have to construct a joptionpane inside the void method to show the result?

    2) What are the processCheck and processDeposit helper methods for? This is from my professor's sample code, I'm confused because why do I need these 2 methods if I already have a void method of setBalance that already calculates current balance?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Don't understand void methods, need help!

    My question is nothing about errors or compiling
    The posted code has multiple errors, does not compile, and seems to be missing parts. It would be easier to tell you how the code works if you posted code that compiles.



    How do I input the result of the current balance to the user?
    how do i show the cbalance to the user?
    how do I show the user their current balance?
    do i have to construct a joptionpane inside the void method to show the result?
    Do you instructions say to output on a joptionpane? A joptionpane would be one possible way to display results, but how are we to know what your instructions included?



    What are the processCheck and processDeposit helper methods for?
    Processing a check and processing a deposit if I had to guess.



    I'm confused because why do I need these 2 methods if I already have a void method of setBalance that already calculates current balance?
    Processing a check or a deposit involves far more than adding or subtracting a dollar amount from a balance. The process methods might save facts about the entire transaction to a database including things like the check/deposit slip number, the transaction number, the location, date, and time the transaction took place, the id of the employee, etc. Calling setBalance is one of the smaller steps in processing a transaction of either type.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Don't understand void methods, need help!

    Quote Originally Posted by jps View Post
    The posted code has multiple errors, does not compile, and seems to be missing parts. It would be easier to tell you how the code works if you posted code that compiles.



    Do you instructions say to output on a joptionpane? A joptionpane would be one possible way to display results, but how are we to know what your instructions included?



    Processing a check and processing a deposit if I had to guess.



    Processing a check or a deposit involves far more than adding or subtracting a dollar amount from a balance. The process methods might save facts about the entire transaction to a database including things like the check/deposit slip number, the transaction number, the location, date, and time the transaction took place, the id of the employee, etc. Calling setBalance is one of the smaller steps in processing a transaction of either type.

    Sorry for being rather vague.


    The instructions are:

    Write a class with methods to help you balance your checking account(an object class-main method is not in this class). The CheckingAccount Class should have at least two instance variables: the balance and the total service charges, along with methods to get and set each instance variables. You may add other variables and methods if you like. The program should read the initial balance for the month, followed by a series of transactions. For each transaction entered, the program should display the transaction data, the current balance for the account, and the total service charges. Service charges are $0.10 for a deposit and $0.15 for a check. If the balance drops below $500.00 at any point during the month, a service charge of $5.00 is assessed once for the month. Anytime the balance drops below $50.00, the program should print a warning message. If the balance becomes negative, an additional service charge of $10.00 should be assessed for each check until the balance becomes positive again. A transaction takes the form of an int number, followed by a double number. If the int number is a 1, then the double number is the amount of a check. If the int number is 2, then the double number is the amount of a deposit. The last transaction is 0 with no number to follow it.
    A sample Input/Output dialogue might look like thisUse the JOptionPane for the dialog)


    So we have to use the JOptionPane to input the results.

    The missing code is due to the template of the code my instructor provided;

    public class Main
    {
       //global variables:
       //define a CheckingAccount object to keep trach of the
       // account information.
       public static void main (String[] args)
       {
           // defines local variables
           //  get initial balance from the user
           //  perform in a loop until the trans code = 0
           //    get the trans code from the user
                 and process it with appropriate helper method
          //   When loop ends show final balance to user.
       }
     
       public static __________ getTransCode()
       {
       }
       public static _________ getTransAmt()
       {
       }
       public static __________ processCheck(___________)
       {
       }
       public static __________ processDeposit(___________)
       {
       }
     
    }
     
    --------------------CheckingAccount.java -------------------------
     
    public class CheckingAccount
    {
     
          private double balance;
          private double totalServiceCharge;
     
          public CheckingAccount(double initialBalance)
          {
                balance = ______________________;
                totalServiceCharge = ______________;
          }
     
          public ____________ getBalance()
          {
                return _______________;
          }
     
          public void setBalance(double transAmt, int tCode)
          {
                if(tCode == 1)
                balance = ___________________;
                else //if(tCode == 2)
                    balance = ______________________;
          }
     
          public ____________ getServiceCharge()
          {
                return totalServiceCharge;
          }
     
            public void setServiceCharge(double currentServiceCharge)
          {
                totalServiceCharge = ___________________________;
          }
    }

    This being the sample code.

    The missing parts are due to incomplete methods from the sample code/not knowing how to continue on.

    How do I call upon the setbalance method if it's a void?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Don't understand void methods, need help!

    You call methods which have a void return type the same way you call any other method:

    public void method1()
    {
    }
     
    // calling method1
    method1();

    void simply tells Java that the method doesn't return a value, and since it doesn't exist you can't do anything with the return object.

    This is in contrast with methods which do return something:

    public int method2()
    {
        return 1;
    }
     
    // calling method2
    int val;
    val = method2(); // stores the result of method2() into val

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Don't understand void methods, need help!

    Quote Originally Posted by helloworld922 View Post
    You call methods which have a void return type the same way you call any other method:

    public void method1()
    {
    }
     
    // calling method1
    method1();

    void simply tells Java that the method doesn't return a value, and since it doesn't exist you can't do anything with the return object.

    This is in contrast with methods which do return something:

    public int method2()
    {
        return 1;
    }
     
    // calling method2
    int val;
    val = method2(); // stores the result of method2() into val

    My updated code for the input is:

     
           do
           {
            int tcode;   
            tcode = getTransCode();
     
            if (tcode == 1)
            {
                 double tamount;
             tamount = getTransAmt();
     
             info = new CheckingAccount(tamount);
     
     
     
     
             message = "Transaction: Check in the amount of $" + tamount+
                     "\n" +
                       "Current balance: $" + info.getBalance() +
                       "Service Charge: Check --- charge $0.15" +
                       "Service Charge: Below $500 --- charge 5.00" +
                       "Total service charge:" + info.getServiceCharge();
                       JOptionPane.showMessageDialog (null, message);
            }
     
            else if (tcode ==2)
            {
     
                double tamount;
                tamount = getTransAmt();
     
                message = "Transaction deposit in amount of" + tamount + "\n" +
                          "Current balance: " + info.getBalance() +
                          "Service Charge: Deposit --- charge $0.10" +
                          "Total Service Charge: " + info.getServiceCharge();
     
                          JOptionPane.showMessageDialog(null, message);
            }

    where I say, current balance: info.getbalance(); is that correct? I am not calling on the setBalance method, which contains the correct calculations depending on the tcode, I am simply calling on the getbalance method. Would this input the current balance WITH the transaction amount calculations? Or would I have to change the code to simply, "current balance: $" + setBalance() + ?

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Don't understand void methods, need help!

    where I say, current balance: info.getbalance(); is that correct?
    Does it compile, run, and provide correct results?


    Or would I have to change the code
    If you update the balance with the transaction first, then the balance will reflect the balance after the transaction. Call the method first that you want executed first. If you are unsure what is happening use printlns to see the values in the variables every step of the way. (If that means a println after every line of code, so be it...)

Similar Threads

  1. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  2. Passing variables with void methods
    By knightmetal in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 21st, 2012, 08:46 PM
  3. Help me understand when to use static on functions/methods
    By thenk24 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 1st, 2011, 11:42 AM
  4. How to combine these 3 void methods?..
    By TimoElPrimo in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 8th, 2011, 01:25 PM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 04:24 PM