![]() |
|
|
|
|
1
2nd November 21:29
External User
Posts: 1
|
bit flaky on me and won't allow me to reply to the group, only in e-mail
messages (I must have set a bad config option somewhere). Anyway... The explanations on OOP were helpful. Looking over the sample code that the other posters provided, as well as my textbook, I think I have what ought to be "working" code. But, it's not actually working. So..hmmm. .I have commented out some instructions (and thus cut them out of this post) till I have this initial version working. This explains why there are more variables than actually appear in the code .What I think I'm doing: In my app class: 1) I set the starting balance to 0. This is because before any transactions occur, the balance is nothing. 2) I input the "initial" balance (that is, the opening account balance which I've named 'initBalance1'). This will be treated as a transaction, so, I immediately assign it to 'transaction'. I save the 'initBalance1' value because I will need it when I print out the final results. 3) I pass the starting balance of 0 and the initial deposit to my worker classes (setBalance). In my worker class: 1) I create the new object with two parameters. An example by a previous poster seemed to show that I only ought to put one parameter in the constructor, but, my textbook example shows a constructor with all the parameters that will eventually be passed over. I have tried both one and both and neither seems to have an impact on the final results. 2) Having passed the transaction value to my setBalance method, I then set the 'change' variable to that value. 3) I set up my calculation method ('transAction'). I don't call this method at any point in the AppClass (which may be what my problem is???). 4) I set up my getBalance method. This calls the calculation in the transAction method (at least in theory), but, it seems to be where my code is breaking down, as it doesn't return anything other than 0.0 Back in my app class: 1) I call the final value after all calculations have been run on it from the worker class (getBalance). 2) I print the results to the screen. It shows my initial balance was 100.95 (my test number), my transaction was 100.95, and my final balance was 0.0 All three numbers (just for this example) ought to be the same. Any help? As I said, I think I'm missing the "point" on how the calculation method actually is run. I "thought" what I needed to do was reference the results of the method (endBal) in another method (getBalance) and it would be able to run the calcuation. Now, I don't think that's correct. But, I don't have any notes on how to call methods that aren't get/set/constructors. I can call the tranaction method if I change it to a getMethod, but, our professor wants both a calculation method _and_ a get method. How do I force the transAction method to transact? package lab4_bankaccounts; import javax.swing.*; import java.text.DecimalFormat; /* Name: Jennifer M. Morse * Due Date: 16 Oct 03 * Programming Assignment: Lab - banking transactions * Course: CS16 * Section: 0630 * * Description: * * This program teaches the usage of instance variables and constructs. The * Worker class will contain the following methods: a constructor to initialize the balance, * a transAction method, a getBalance method, and a setBalance method. Be sure * to compute the final balance for each account. Use the println method to display * the initial and final balances for each account. * * Input: * * current account balance, deposits, and withdrawals * * Output: * * Formatted table including the Customer number, starting balance, and * end balance, for each account. * * Assumptions: * * Two accounts * */ public class AppClass { public static void main(String[] args) { //extra variables for temp debugging double bal,bal1,bal2,bal3; //Define the instance variables double balance, transaction; double initBalance1, resultBalance1; // holders for formatted results // initialize starting balance to 0. balance=0; //Input and Calculations //Get the starting balance, deposits, and withdrawals from the first user. // Starting Balance String data = JOptionPane.showInputDialog ("Enter Starting Balance:"); initBalance1=Double.parseDouble (data); transaction=initBalance1; // keep one variable with the initial balance and have another to manipulate in transactions. WorkerClass bankObject = new WorkerClass(balance, transaction); bankObject.setBalance(transaction); balance=bankObject.getBalance(); bal=balance; //Output // Display the results of the calculation // System.out.println (firstObject.getWorker()); System.out.println("Starting Balance: " + initBalance1 + "Transaction: " + transaction + "Final Balance: " + bal); //Exit // Terminate the program System.exit(0); } package lab4_bankaccounts; /** * <p>Title: Banking Transactions</p> * <p>Description: banking transactions</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: CS16</p> * @author Jennifer M. Morse * @version 1.0 */ public class WorkerClass { // define the instance variables // Arguably, this can be done with two variables (myBal and change), but // for personal comprehension, I chose to use three and track both starting // and ending balances. private double startBal, change, endBal; // constructor (initialize balance) public WorkerClass(double balance, double transaction) { startBal = balance; change = transaction; } //Set Method //Input //Get balance change information from app class. public void setBalance(double transaction) { change = transaction; } // Transaction Method // Calculation // (covers both Deposit and Withdrawal methods by using negative/positive numbers) public double transAction() { endBal = startBal + change; return endBal; } // Get Method // Output // Return new balance to main class after calculations public double getBalance() { return endBal; } } -- Gwen Morse "There are some limitations to what you can expect from an operating system. You just can't expect Linux to work like a 3D-shooter game or something." http://tldp.org/HOWTO/Deciding-Linux...s_compare.html |
|
|
|
|
2
2nd November 21:34
External User
Posts: 1
|
You're still missing a bit here....
Comments inserted.... If the start balance you want is not 0, then you are wasting code and time by doing this. Set your original balance to what you want it to be by passing a value into your construcotr method....Such as.. Woker worker1 = new Worker(100.95); Not really. This is what your getBalance() method should return so you shouldn't ned to "save" the variable in your app class. You may need to do it temporarily depenidng on where you put your printout method. You shouldn't need two values going into setBalance(). You should only require one. You should initialise all values under your worker class in your constructor. This ensures that you don't end up trying to access a variable with a null value (which you might have forgotton to assign somewhere else). My next comment is about your code........ You don't really need these..... You should be storing them in your worker class, initBalance, resultBalance. You are using a confusing variable name.... All you should be doing here is initialising a worker object.... WorkerClass bankObject = new WorkerClass(balance); You should set initial balance and end balance to the same value to start with and only modify end balance when a transaction is called. You just did this above, you shouldn't need to do it again. What is "change" being used for? You should have startBalance and endBalance. They should be equal to each other at the start of the program since you yhaven't performed any operations yet. Your startbalance should remain the same and you should change the endbalance according to transactions. ok if change is your endbalance variable. Make this a void and include a variable input in the call. You shouldn't be returning anything yet. public void transAction(double transaction){ endbalance+=transaction; //add/subtract transaction to endbalance } note you can call this from your app method by .. bankObject1.transAction(transaction); It should permutate the endbalance variable in this instance of your WorkerClass object. ie bankObject1.endbalance. This is all it should do. Good, you've got this one. I don't think you really understand the concepts yet of passing variables and where they're stored. Your worker class should look something like.... public class Worker{ double startbalance,endbalance; //constructor method. This is to initialise the variables at default value. Note that both end and startbalance are equal to start. public Worker(double balance){ startbalance = balance; endbalance=balance; } //returns the endbalance for this instance of Worker public double getBalance(){ return endbalance; } //Sets the balance to a given value public void setBalance(double balance){ endbalance = balance; } //performs a transaction public void transaction(double amount){ endbalance+=amount; } } Note that since you also require your initial balance you may want a method. public double getStartbalance(){ return startbalance; } Try naming your variables more appropriately, you'll only get yourself (and others) more confused. You can call any method from the worker class with.. <ObjectName>.<methodName>(variables); Note if the method returns a value you need to do... myValue = <ObjectName>.<methodName>(variable); I still don't thin u're undestanding some of the basics here. But I hope this helps -- ___________,,,_o~o_,,,___________ The Power to Imagine starmage@dodo.com.au ___________,,,_____,,,___________ V |
|
|
3
2nd November 21:36
External User
Posts: 1
|
Further to my previous post.....
I think ur missing a little more than that (judging by your code and description). It's hard to say but judging b what you've said I don't think you are quite understanding some very basic Java concepts, an possibly some more basic programming ones. I'd recommend you do some very serious reading of the core concepts an not just your example code. Seeing as you couldn't figure out how to access your transAction method .You're code is very confusing. You seem to have plugcked variable names out of a hat. Try and get some uniformity into it and use variable names that best represent the type of variable you are holding (startbalance,endbalance.. etc). I'm not sure if you realise what the code is doing that you've created so far? Or the concept behind how to use/access it. Until you understand some of these core points you're going to find this increasingly difficult. -- ___________,,,_o~o_,,,___________ The Power to Imagine starmage@dodo.com.au ___________,,,_____,,,___________ V |
|
|
|