|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

11-15-2007, 12:07 AM
|
 |
Member
|
|
Join Date: Nov 2007
Posts: 50
|
|
|
for the pervious post, ++ operator is increment. you can either put it after
myVar++; // this adds 1 to myVar, equivelant to myVar += 1 or myVar = myVar + 1.
or you could put it in front : ++myVar.
depending on how you use it, there is a difference. e.g.:
int num = 1;
System.out.println(num);
System.out.println(num++);
System.out.println(num)
for the conversion constructor, no clue. in java there is normal constructors but never heard of that.
the results would be 1, 1 and 2. you would think, why two ones since i incremented on the second? it is because first it prints num, then increments its value. its as if its on its own line, except it displays the variable first. then printing num after that will give its value which was incremented before.so
int num = 5;
int getnum = num++;
System.out.println(getnum);
System.out.println(num);
this would return 5 and 6., because assigning num++ to getnum is actually assigning num, then incrementing num. then displaying num would give the value which you incremented. you might think this is annoying, is there a way around it? in some places , it does not matter, such as a for loop,
for (int i = 0; i < 6; i++) { //code }
and
for (int i = 0; i < 6; ++i) { //code }
would be identical, no matter what the code inside is. the way around it is befire the variable, ++num. so
int num = 1;
int getnum = ++num;
System.out.println(getnum);
System.out.println(num);
would return 2 and 2, because it adds one to num, then returns num. so its setting getnum to num+1 and adding 1 to num.
int num = 5;
System.out.println(num);
System.out.println(++num);
System.out.println(num);
would return 5, 6 and 6.
so, yourVar++ is like returning yourVar, then adding 1 to yourVar's value, while
++yourVar, is adding one to yourVar and returning yourVar after 1 has been added;
hope that helped for your ++ question
Last edited by JT4NK3D : 11-15-2007 at 12:37 AM.
|
|

11-15-2007, 12:22 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
public class Trans {
private Operation op; /*Deposit or Withdrawal*/
private Account acc; /*account for which a transaction*/
/*amount of money involved in that transaction, deposit or withdrawal*/
private double amt;
public Trans(Account acct, Operation op, double amt) {
this.acc = acc;
this.op = op;
this.amt = amt;
}
public double getAmount() {
return amt;
}
public static void main(String[] args) {
// What is the type of Operation.WITHDRAWAL?
// If is is an int you'll hava to change the Trans
// constructor to:
// public Trans(Account acct, int op, double amt)
Trans t1 = new Trans( account1, Operation.WITHDRAWAL, 100 ) ;
// Use the variable "t1" to access fields and methods in
// this instance of Trans
System.out.println("amt = " + t1.getAmount());
}
}
|
|

11-15-2007, 04:31 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
Excellent! Thanks a bunch guys, really appreciate it.
Ok, if I declare the following line before the class:
Then I call it in a function:
public myfunct(boolean mybool )
{
mybool = true;
}
As you can see, in the function I have changed the value to true, will this also change the global value which is declared before the class ?
|
|

11-15-2007, 06:05 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
A good question. In this method signature
public myfunct(boolean mybool )
The argument "mybool" is a local variable which has scope limited to the body (curley braces) of the method. In this local scope it hides/shadows the member variable (in class scope) of the same name. So the expected answer is "no."
Let's test it:
import java.util.Random;
public class Test {
static Random r = new Random();
static boolean isItTrue = false;
static private boolean howBoutThis(boolean isItTrue) {
isItTrue = r.nextBoolean();
return isItTrue;
}
public static void main(String[] args) {
Random seed = new Random();
for(int j = 0; j < 5; j++) {
boolean b = seed.nextBoolean();
System.out.printf("howBoutThis(%5b) = %5b isItTrue = %b%n",
b, howBoutThis(b), isItTrue);
}
}
}
|
|

11-15-2007, 06:13 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
Thanks. Ok next question,
For the following code in main():
Account[] accs = new Account[1] ;
I get the following error:
cannot find symbol
symbol : constructor Account(java.lang.String,int,int)
location: class Account
accs[0] = new Account( "Pauline Smith", 300, 250 ) ;
^
|
|

11-15-2007, 06:48 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
All this
Account[] accs = new Account[1];
does is to instantiate an array of type Account and length one.
The only (and every) element of the array is null. To initialize the first/only element
you would call a constructor of the Account class. It looks like you did this with
accs[0] = new Account( "Pauline Smith", 300, 250 ) ;
and got the compile error which is saying that the Account class does not have a
constructor with the signature
Account(String s, int n1, int n2)
You can add a constructor into the Account class with this signature if you like.
|
|

11-15-2007, 07:04 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
|
My bad, fixed!
Another question though, if I wanted to output that array using a function in another one of my classes, how would I do it ?
Last edited by Shaolin : 11-15-2007 at 07:07 AM.
|
|

11-15-2007, 07:19 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
class One {
Account[] accounts;
One() {
accts = new Account[1];
accts[0] = new Account(...);
}
}
class AnotherOne {
AnotherOne() {
// This could also be declared as a member variable.
One one = new One();
Account[] accts = one.accounts;
// carry on with accts array...
}
}
|
|

11-15-2007, 07:30 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
Beautiful.
Ok, I want to another thing. If I want to print a string, call it from main and print it from my accounts class, how would I do that. Here is what I've done so far:
Main():
System.out.println( accs.Print);
and in my accounts class:
public String Print(Account acc)
{
// Print
return ( "User Name: " + name + " Acc: " + accountnmber ) ;
}
When I compile this I get the following error:
cannot find symbol
symbol : variable Print
location: class Account[]
System.out.println( accs.Print);
|
|

11-15-2007, 07:44 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
This line
System.out.println( accs.Print);
appears to be accessing the "accs" array according to the compile error which is saying
the compiler cannot find the "Print" variable in the Account[] class, ie the Account[] (array).
If you have an Account class and an array of Accounts then you would access an instance of
it in the array like this
Account[] accs = new Account[1];
accs[0] = new Account(...
accs[0].Print();
|
|

11-15-2007, 08:08 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
Thanks. Well I did manage to get it to work but the results ouputted werent what I expected.
Here is what I have in main:
System.out.println( accs[0].Print() );
In my Account Class:
public String Print(String n)
{
// Print
return ( "User Name: " + name + " Account: " + accNo + " balance: " + totalbalance + ")" ) ;
}
I should get something like:
User Name: Jack Account: 1121921 balance: 12313.12
But instead I'm getting:
I have a feeling that it has something to do with this line:
public String Print(String n)
|
|

11-15-2007, 08:26 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
This
System.out.println( accs[0].Print() );
is calling a no-argument method Print.
Your Account class Print method takes a String argument. Unless it has a no-argument Print method you should get a compile error.
This
looks like the hex address of the actual object in memory which you would get if you did something like this:
System.out.println(accs[0]);
|
|

11-15-2007, 08:45 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
|
Ah ha, great stuff as usual.
I have an issue with my increment. for my accno (account number) I want to increment the number by one for each customer. so I do this:
// outside class
private int accno1 = 0;
then in public account() I do this:
accno = accno1++;
But when I run the app all the customers IDs are the same value.
|
|

11-15-2007, 09:22 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
Each new Account is just that: a new Account instance, not shared by any other class. So the incrementing account number needs to be declared, incremented and dispensed in/from the class that is creating the Account[]. Each Account class instance can then be sent a unique "accNo" which it will keep for itself. In java:
class Account {
int accNo;
Account(...args..., int accNo) {
...
this.accNo = accNo;
}
/**
* Of course this main method can be in any class that you want.
* I put it here it as a matter of convenience.
*/
public static void main(String[] args) {
Account[] accs = new Account[10];
for(int j = 0; j < accs.length; j++) {
accs[j] = new Account(...args..., j);
}
for(int j = 0; j < accs.length; j++) {
System.out.printf("accs[%d].accNo = %d%n", j, accs[j].accNo);
}
}
}
|
|

11-15-2007, 09:25 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
Ok, it seems as though my transaction doesnt want to work.
I have some enum declared in a different file:
operate op { WITHDRAW, DEPOSIT, CLOSED }
In the transaction class:
private Operate op ; // what type of transaction
private Account acc ; // account for which a transaction private double amt ; // amount of money involved in that transaction, deposit or withdrawal
public Transaction(Account acc, Operation op, double amt)
{
// Withdraw/Deposit conversion constructor
this.acc = acc;
this.op = op;
this.amt = amt;
}
public void processing()
{
if (op == Operate.WITHDRAW)
{
acc.withdrawal(amt)
}
}
|
|

11-15-2007, 09:34 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
public Transaction(Account acc, Operation op, double amt) {
System.out.println("acc.Print = " + acc.Print());
The Account object you are sending in to this Transaction constructor would likely contain a unique account number.
|
|

11-15-2007, 09:42 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
|
EDIT: Typo.
The results outputted are the same for both classes. I havent implemented the uniqueIDs code yet.
Is "if (op == Operate.WITHDRAW)" the correct condition for checking which option has been selected in the enum ?
Last edited by Shaolin : 11-15-2007 at 09:46 AM.
|
|

11-15-2007, 09:47 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
|
I don't know. Try printing it out to see what you get.
|
|

11-15-2007, 11:47 AM
|
 |
Moderator
|
|
Join Date: May 2007
Posts: 1,272
|
|
I don't know. Try printing it out to see what you get.
I was wondering when will this thread stop growing  Probably, this is the longest thread in the forum so far. Congratulations hardwired and Shaolin. :P
|
|

11-15-2007, 04:44 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 36
|
|
hehe @ JavaBean
Well I do have another problem, see code as follows:
Before Transaction class:
boolean accountStatus=false;
In the Transaction class:
if (acc.isClosed(accountStatus)==true) {
System.out.println("TEST: " + acc.Print());
}
In Account class
before class:
After class:
public boolean isClosed(boolean stat)
{
if(closed == true) // Check if account is closed
{
return true ; // Account Closed
}
else
{
return false ; // Account not Closed
}
}
It only works for false not true. Any ideas on where I could be going wrong ?
Last edited by Shaolin : 11-15-2007 at 04:50 PM.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
| |