Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #21 (permalink)  
Old 11-15-2007, 12:07 AM
JT4NK3D's Avatar
Member
 
Join Date: Nov 2007
Posts: 50
JT4NK3D is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #22 (permalink)  
Old 11-15-2007, 12:22 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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 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()); } }
Bookmark Post in Technorati
Reply With Quote
  #23 (permalink)  
Old 11-15-2007, 04:31 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
Excellent! Thanks a bunch guys, really appreciate it.

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

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

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 ?
Bookmark Post in Technorati
Reply With Quote
  #24 (permalink)  
Old 11-15-2007, 06:05 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
A good question. In this method signature
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:
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); } } }
Bookmark Post in Technorati
Reply With Quote
  #25 (permalink)  
Old 11-15-2007, 06:13 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
Thanks. Ok next question,

For the following code in main():

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

Code:
cannot find symbol symbol : constructor Account(java.lang.String,int,int) location: class Account accs[0] = new Account( "Pauline Smith", 300, 250 ) ; ^
Bookmark Post in Technorati
Reply With Quote
  #26 (permalink)  
Old 11-15-2007, 06:48 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
All this
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
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
Code:
Account(String s, int n1, int n2)
You can add a constructor into the Account class with this signature if you like.
Bookmark Post in Technorati
Reply With Quote
  #27 (permalink)  
Old 11-15-2007, 07:04 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #28 (permalink)  
Old 11-15-2007, 07:19 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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... } }
Bookmark Post in Technorati
Reply With Quote
  #29 (permalink)  
Old 11-15-2007, 07:30 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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:

Code:
Main(): System.out.println( accs.Print);
and in my accounts class:

Quote:
public String Print(Account acc)
{
// Print
return ( "User Name: " + name + " Acc: " + accountnmber ) ;
}
When I compile this I get the following error:

Quote:
cannot find symbol
symbol : variable Print
location: class Account[]
System.out.println( accs.Print);
Bookmark Post in Technorati
Reply With Quote
  #30 (permalink)  
Old 11-15-2007, 07:44 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
This line
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
Code:
Account[] accs = new Account[1]; accs[0] = new Account(... accs[0].Print();
Bookmark Post in Technorati
Reply With Quote
  #31 (permalink)  
Old 11-15-2007, 08:08 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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:

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

Code:
public String Print(String n) { // Print return ( "User Name: " + name + " Account: " + accNo + " balance: " + totalbalance + ")" ) ; }
I should get something like:

Quote:
User Name: Jack Account: 1121921 balance: 12313.12
But instead I'm getting:

Quote:
Account@190d11
I have a feeling that it has something to do with this line:
Code:
public String Print(String n)
Bookmark Post in Technorati
Reply With Quote
  #32 (permalink)  
Old 11-15-2007, 08:26 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
This
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
Code:
Account@190d11
looks like the hex address of the actual object in memory which you would get if you did something like this:
Code:
System.out.println(accs[0]);
Bookmark Post in Technorati
Reply With Quote
  #33 (permalink)  
Old 11-15-2007, 08:45 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #34 (permalink)  
Old 11-15-2007, 09:22 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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:
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); } } }
Bookmark Post in Technorati
Reply With Quote
  #35 (permalink)  
Old 11-15-2007, 09:25 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
Ok, it seems as though my transaction doesnt want to work.

I have some enum declared in a different file:

Code:
operate op { WITHDRAW, DEPOSIT, CLOSED }
In the transaction class:
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) }
}
Bookmark Post in Technorati
Reply With Quote
  #36 (permalink)  
Old 11-15-2007, 09:34 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #37 (permalink)  
Old 11-15-2007, 09:42 AM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #38 (permalink)  
Old 11-15-2007, 09:47 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
I don't know. Try printing it out to see what you get.
Bookmark Post in Technorati
Reply With Quote
  #39 (permalink)  
Old 11-15-2007, 11:47 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Quote:
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
Bookmark Post in Technorati
Reply With Quote
  #40 (permalink)  
Old 11-15-2007, 04:44 PM
Member
 
Join Date: Nov 2007
Posts: 36
Shaolin is on a distinguished road
hehe @ JavaBean

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

Before Transaction class:

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

Code:
private boolean closed ;
After class:

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 04:50 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the Priority for execution of Interface class and a Abstract class