1. 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-14-2007 at 11:37 PM.

2. Java Code:
```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 [i]int[/i] 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());
}
}```

3. Member
Join Date
Nov 2007
Posts
38
Rep Power
0
Excellent! Thanks a bunch guys, really appreciate it.

Ok, if I declare the following line before the class:

Java Code:
`boolean mybool = false;`
Then I call it in a function:

Java Code:
```	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 ?

4. A good question. In this method signature
Java Code:
`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:
Java Code:
```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);
}
}
}```

5. Member
Join Date
Nov 2007
Posts
38
Rep Power
0
Thanks. Ok next question,

For the following code in main():

Java Code:
`	Account[] accs = new Account[1] ;`
I get the following error:

Java Code:
```cannot find symbol
symbol  : constructor Account(java.lang.String,int,int)
location: class Account
accs[0] = new Account( "Pauline Smith", 300, 250 ) ;
^```

6. All this
Java Code:
`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
Java Code:
`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
Java Code:
`Account(String s, int n1, int n2)`
You can add a constructor into the Account class with this signature if you like.

7. Member
Join Date
Nov 2007
Posts
38
Rep Power
0
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 06:07 AM.

8. Java Code:
```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...
}
}```

9. Member
Join Date
Nov 2007
Posts
38
Rep Power
0
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:

Java Code:
```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);

10. This line
Java Code:
`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
Java Code:
```Account[] accs = new Account[1];
accs[0] = new Account(...
accs[0].Print();```

11. Member
Join Date
Nov 2007
Posts
38
Rep Power
0
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:

Java Code:
`System.out.println( accs[0].Print() );`
In my Account Class:

Java Code:
```      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

Account@190d11
I have a feeling that it has something to do with this line:
Java Code:
`public String Print(String n)`

12. This
Java Code:
`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
Java Code:
`Account@190d11`
looks like the hex address of the actual object in memory which you would get if you did something like this:
Java Code:
`System.out.println(accs[0]);`

13. Member
Join Date
Nov 2007
Posts
38
Rep Power
0
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.

14. 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:
Java Code:
```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);
}
}
}```

15. Member
Join Date
Nov 2007
Posts
38
Rep Power
0
Ok, it seems as though my transaction doesnt want to work.

I have some enum declared in a different file:

Java Code:
`operate op { WITHDRAW, DEPOSIT, CLOSED }`
In the transaction class:
Java Code:
```	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)
}```
}

16. Java Code:
```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.

17. Member
Join Date
Nov 2007
Posts
38
Rep Power
0
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 08:46 AM.

18. I don't know. Try printing it out to see what you get.

19. 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

20. Member
Join Date
Nov 2007
Posts
38
Rep Power
0
hehe @ JavaBean

Well I do have another problem, see code as follows:

Before Transaction class:

Java Code:
`boolean accountStatus=false;`
In the Transaction class:
Java Code:
```		if (acc.isClosed(accountStatus)==true) {
System.out.println("TEST: " + acc.Print());
}```
In Account class
before class:

Java Code:
`private boolean closed ;`
After class:

Java Code:
```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 03:50 PM.

Page 2 of 3 First 123 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•