I just cant find out where i got it wrong...

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 478 Views - Last Post: 27 September 2011 - 12:25 PM Rate Topic: -----

#1 vienhuynh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 24-April 10

I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:34 AM

can you guys shed some lights for me?
this is the first class
import java.util.*;
public class Account 
{
	private String name;
	private int accNumber;
	private double balance;
	
	
	public Account()
	{
		name = "";
		accNumber = 0;
		balance = 0.0;
	}
	
	public Account(String name, int accNumber, double balance)
	{
		name = name;
		accNumber = accNumber;
		balance = balance;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAccNumber() {
		return accNumber;
	}

	public void setAccNumber(int accNumber) {
		this.accNumber = accNumber;
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}
	
	public String toString()
	{
		return "name " + name + "has an acc number of " + accNumber + " and a balance of: " + balance;
	}
	//public void deposit()
	//{
		
	//}
	
	//public double balanceRetriever()
	//{
		//return balanceRetriever;
		
	//}
	
	//public double withdraw()
	//{
		//return withdraw;
	//}
}
 


main class
import java.util.*;
public class Main 
{
	

	public static void main(String [ ]args)
	{
		System.out.println("Welcome to XYZ bank! ");
		System.out.println("You are a new customer, let make an account for you: ");
		
		System.out.println("let begin with your name first, please type in your name: ");
		Scanner key = new Scanner(System.in);
		Random generator = new Random();
		String name = key.next();
		key.nextLine();
		int number = Math.abs(generator.nextInt()) / 100000;
		System.out.print(number);
		double balance = key.nextDouble();
		Account member = new Account (name, number, balance);
		System.out.println(member.toString());
	}
}
 


Is This A Good Question/Topic? 0
  • +

Replies To: I just cant find out where i got it wrong...

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:36 AM

What problems are you having? Is it not working as intended or is it throwing errors? Please post them and what the problem is.
Thanks
Was This Post Helpful? 0
  • +
  • -

#3 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 17
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:36 AM

The problem is that in most of your functions in your class, you have identical names for your variables and your arguments, you should change them, because the compiler is going berserk, it doesn't know which one to choose.
Was This Post Helpful? 0
  • +
  • -

#4 creative code monkey  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 14-September 11

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:37 AM

What problem are you seeing?
Was This Post Helpful? 0
  • +
  • -

#5 vienhuynh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 24-April 10

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:39 AM

it seems my tostring function not working
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:42 AM

Actually Pontus it knows the difference because he is using the keyword this. You need to use this keyword in constructor like you did in the other methods. The toString method seems fine.. how is it not working? I need you to tell me what it is doing verse what you are wanting it to do.. a little more than "it isn't working".

This post has been edited by Fuzzyness: 27 September 2011 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7576
  • View blog
  • Posts: 12,729
  • Joined: 19-March 11

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:43 AM

    public Account(String name, int accNumber, double balance)  
     {  
         name = name;  
         accNumber = accNumber;  
        balance = balance;  

     }  


In this scope, the reference to "name" refers to the local variable that you declared in the constructor declaration (first line of this snip).
So name = name just means, set the variable name, which is the argument, not the field, equal to itself, which it already was.

Since your argument list shadows your fields, you need a way to refer to the field from the shadowed context. The way you do this is by using "this":

this.name = name;


this means "the current object", this.name means the field name of the current object, and not the local variable of the same name.

Clear?
Was This Post Helpful? 1
  • +
  • -

#8 vienhuynh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 24-April 10

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:43 AM

it suppose to print out since i did a return of type String in the toString method, i try to print it out in main using the instance of Account class, yet it wont.

Oh nevermind guys, it wont print since i didnt give an input for balance... i forgot to... sorry, hehe, without system println, it sure confusing.

This post has been edited by vienhuynh: 27 September 2011 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:44 AM

Use this keyword to differentiate between instance variables and local ones like
 this.name = name;
and so on everywhere where you use the same name for local and class variable.

This post has been edited by smohd: 27 September 2011 - 11:47 AM

Was This Post Helpful? 1
  • +
  • -

#10 vienhuynh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 123
  • Joined: 24-April 10

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:46 AM

View Postjon.kiparsky, on 27 September 2011 - 11:43 AM, said:

    public Account(String name, int accNumber, double balance)  
     {  
         name = name;  
         accNumber = accNumber;  
        balance = balance;  

     }  


In this scope, the reference to "name" refers to the local variable that you declared in the constructor declaration (first line of this snip).
So name = name just means, set the variable name, which is the argument, not the field, equal to itself, which it already was.

Since your argument list shadows your fields, you need a way to refer to the field from the shadowed context. The way you do this is by using "this":

this.name = name;


this means "the current object", this.name means the field name of the current object, and not the local variable of the same name.

Clear?


right, but i see people + my professor just do it that way too, i mean without using this. I remember there are cases this is needed, otherwise it fine, please point it out.
Was This Post Helpful? 0
  • +
  • -

#11 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:47 AM

If you are not supposed to use this then change the variable names in the parameters of the constructor.

This post has been edited by Fuzzyness: 27 September 2011 - 11:48 AM

Was This Post Helpful? 1
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:52 AM

In your case you have to use the keyword every time when you have two variable with the same name in a scope so as to differentiate between class and local(method) variables.
As jon said, we use this to mean this object(the current) and in your constructor we have a variable called name which is in its scope, and a class variable with the same name which is also in the scope. So if you dont differentiate the active one will be the local one that means:
name = name; //you assign a value to its own and not an instance variable

Was This Post Helpful? 0
  • +
  • -

#13 creative code monkey  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 14-September 11

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:54 AM

Just my 2 cents here, but I never name local mutator/constructor variables and private class fields the same. To someone who is a little unsure of how classes and objects work, it makes it very confusing to understand.

I would do the following :
public Account(String localName, int localAccNumber, double localBalance)
	    {
	        this.name = localName;
	        this.accNumber = localAccNumber;
	        this.balance = localBalance;
	    }


It's much easier to understand.

This post has been edited by creative code monkey: 27 September 2011 - 11:56 AM

Was This Post Helpful? 1
  • +
  • -

#14 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 11:59 AM

View Postcreative code monkey, on 28 September 2011 - 12:39 AM, said:

I would do the following :
public Account(String localName, int localAccNumber, double localBalance)
	    {
	        this.name = localName;
	        this.accNumber = localAccNumber;
	        this.balance = localBalance;
	    }

It's much easier to understand.

Then you don't need the this keyword here... ;)
Was This Post Helpful? 0
  • +
  • -

#15 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: I just cant find out where i got it wrong...

Posted 27 September 2011 - 12:00 PM

There is no point in using this in that code. The JVM already knows that there is only one variable named "name" so saying that it belongs to the class at the same time is redundant and pointless. If you are going to have different names no need for it. If you are going to have same names then you need it. If you cannot tell that "names" belongs to the class in this statement: name = localName; and that localName is a param, then you need to revisit method parameters.

All i am trying to say is that there is no need to ensure something that is already Solid.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2