Page 1 of 1
About Question enthuware.ocajp.i.v8.2.1451 :
Posted: Tue Jan 19, 2016 9:01 pm
by Deleted User 2655
Can someone please explain to me why;
Code: Select all
bw.addMore(-bw.getAvailable()); will print 0?
Re: About Question enthuware.ocajp.i.v8.2.1451 :
Posted: Tue Jan 19, 2016 9:39 pm
by admin
bw.getAvailable() returns 100, therefore when you pass -bw.getAvailable() to addMore, you are essentially passing -100 . addMore() performs 100+ (-100), which makes available = 0. That is why System.out.println(bw.getAvailable()); prints 0.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v8.2.1451 :
Posted: Thu Jan 21, 2016 8:07 am
by Deleted User 2655
Thanks Paul.
Re: About Question enthuware.ocajp.i.v8.2.1451 :
Posted: Fri Dec 15, 2017 6:54 pm
by Rinkesh
Why is bw(0) wrong??
Re: About Question enthuware.ocajp.i.v8.2.1451 :
Posted: Fri Dec 15, 2017 9:34 pm
by admin
Rinkesh wrote:Why is bw(0) wrong??
Well, this is a very basic programming question. bw is a variable and () can only be applied to a method name while invoking that method on a variable. For example, bw.addMore(0). So not sure why you think bw(0) makes sense? What do you think bw(0) means?
Looking at other questions that you have asked, I will suggest you to go through a good Java book to understand the basics before attempting mock exams. Otherwise, mock exams will not help you as much.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v8.2.1451 :
Posted: Mon Nov 04, 2019 9:43 pm
by wdphipps
Hi team,
Hoping you can clear up some confusion for me.
I see in this question the explanation "Bandwidth class does not have a no-args constructor so this will not compile." is given as the reason for "bw = new Bandwidth();" being incorrect.
Wouldn't Java create a default constructor in this example?
My original guess is that this line is important:
"and a piece of code from another class:"
Thanks in advance,
-WP
Re: About Question enthuware.ocajp.i.v8.2.1451 :
Posted: Mon Nov 04, 2019 10:38 pm
by admin
The default no args constructor is provided by the compiler only when there is no other constructor present in the class.
Which book are you following?
Re: About Question enthuware.ocajp.i.v8.2.1451 :
Posted: Fri Nov 08, 2019 5:28 pm
by wdphipps
I'm using the
latest book.
I looked further into the default constructor, it seems like I gained some understanding getting this wrong. I had understood that without a *no-args* constructor, a default would be created by the compiler. I know now that a default is only created if *no* constructor is given at all. This was also in the book:
"The compiler will complain that Account class does not have a constructor that takes no arguments. What happened to the default constructor, you ask? Well, since this class provides a constructor explicitly, the compiler feels no need to add one on its own."
Thanks for the help!
Re: About Question enthuware.ocajp.i.v8.2.1451 :
Posted: Mon Nov 18, 2019 6:10 pm
by wdphipps
Now that i'm further in the book, I see how unnecessary my asking this question was. The default constructor is discussed at length further along this version of the book. Thanks again, team.