14 Replies - 311 Views - Last Post: 24 September 2013 - 01:58 PM Rate Topic: -----

#1 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Constructor to no constructor conversion issues...

Posted 24 September 2013 - 11:10 AM

Solved

This post has been edited by daonlyillwiz: 24 September 2013 - 12:49 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Constructor to no constructor conversion issues...

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 11:20 AM

Why do you want to remove the constructor? Generally using constructors is a better idea than using setter methods.
Was This Post Helpful? 0
  • +
  • -

#3 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 11:28 AM

Yea i know that but its just an exercise though...trust me a constructor is definitely better than setter methods.
Was This Post Helpful? 0
  • +
  • -

#4 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 11:32 AM

A constructor is called automatically to initialize the class. You shouldn't use only non constructor methods for creating a class. Like Ryano121 states, it's bad practice.
Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 11:35 AM

If you have to do it that way then like I said you need to use the setter methods instead. Something like:

CreditCard2 card = new CreditCard2();
card.setNumber(012345);
card.setName("my name");
//more setting

wallet[i] = card;


Btw this is a stupid exercise.
Was This Post Helpful? 0
  • +
  • -

#6 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 11:41 AM

Setters modify values of classes and getters access values of classes. With no custom constructor you just need to call the default that is already provided.

wallet[0] = new CreditCard2(); // default takes no argument



Now all you have to do is invoke the necessary setters to initialize each objects to what their values would have been using the constructor.

Your setters aren't actually 'setters'. They assign the same value to the variables no matter what you pass in as the new value.

This
public void setNumber(String number) {  

        this.number = "1111 0375 9387 5309";  

}  


should be
public void setNumber(String number) {  

        this.number = number;

}  


Was This Post Helpful? 0
  • +
  • -

#7 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 11:58 AM

Solved

This post has been edited by daonlyillwiz: 24 September 2013 - 12:49 PM

Was This Post Helpful? -5
  • +
  • -

#8 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 12:03 PM

You have an array that holds 10 values yet a for loop that goes from 1 to 16 - accessing the i'th element upon each iteration. You get an exception when you try to access an index in the array that doesn't exist.
Was This Post Helpful? 0
  • +
  • -

#9 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 12:07 PM

File 2 line 4 builds it for 10. For statement on line 12 iterates more than that so it's out of bounds.
Was This Post Helpful? 0
  • +
  • -

#10 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 12:13 PM

Alrght i just fixed that (cant believe i missed it :\)

now its giving Exception in thread "main" java.lang.NullPointerException
at Test2.main(Test2.java:17)


which is this:
wallet[1].chargelt((double) i);

Was This Post Helpful? 0
  • +
  • -

#11 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 12:23 PM

Because you never create a new credit card for that index.

for (int i = 1; i <= 16; i++) {
	// You want to add a new credit card per index if you're going to access an index.
	wallet[0] = new CreditCard2();
	wallet[i] = card;
	// With this being 1 and nothing having changed, you're just doing the same thing over and over. The result is always the same.
	wallet[1].chargelt((double) i);
	wallet[1].chargelt(2.0 * i); // implicit cast
	wallet[1].chargelt((double) 3 * i); // explicit cast
}



Maybe this is what you want?
for (int i = 1; i <= 16; i++) {
	wallet[i] = new CreditCard2();
	wallet[i] = card;
	wallet[i].chargelt((double) i);
	wallet[i].chargelt(2.0 * i); // implicit cast
	wallet[i].chargelt((double) 3 * i); // explicit cast
}



Now it's at least acting upon the current index in the for loop.
The actual behavior of your program is for you to decide though. I don't think you want to keep calling chargeIt like that though.

EDIT:
Of course after having looked at your code for chargeIt, maybe that is what you want. The way it was before would simply rack up a ton of expense on one particular card.

Sorry for the confusion.

EDIT:
One more note. Your chargeIt method returns a boolean but you seem not to care at all about it in your code. Without checking the value, you could be continually trying to charge money the card doesn't have.

This post has been edited by MentalFloss: 24 September 2013 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#12 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 12:38 PM

Solved

This post has been edited by daonlyillwiz: 24 September 2013 - 12:49 PM

Was This Post Helpful? -2
  • +
  • -

#13 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 12:44 PM

Because you start at index one for filling, index zero is null.
Was This Post Helpful? 1
  • +
  • -

#14 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 12:49 PM

WOW!!!

problem solved, thanks alot man!!
Was This Post Helpful? -3
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Constructor to no constructor conversion issues...

Posted 24 September 2013 - 01:58 PM

-1 for removing code from older posts
Some may have benefit from the solutions proposed by other DICs but if you remove the initial problem the thread is useless to I am closing it... why would some one want to open it to get disappointed later ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1