Droid Questions - Java


#1

//This program creates a Droid with an initial battery level of 100 that goes through a series of functions that cause a fluctuation in its battery level.

class Droid {
> int batteryLevel = 100;

public Droid(int batteryLevel){
> batteryLevel = 100;
}

public void activate() {
System.out.println(“Activated. How can I help you?”);
batteryLevel = batteryLevel - 5;
System.out.println(“Battery level is: " + (batteryLevel) + " percent.”);
}

public void chargeBattery(int hours) {
System.out.println(“Droid charging…”);
> batteryLevel = batteryLevel + hours;
if (batteryLevel >= 100) {
batteryLevel = 100;
System.out.println("Battery level is: " + (batteryLevel) + “percent.”);
}
else {
System.out.println("Battery level is: " + (batteryLevel) + “percent.”);
}
}

public int checkBatteryLevel() {
System.out.println("Battery level is: " + (batteryLevel) + “percent.”);
return batteryLevel;
}

public void hover (int feet) {
if (feet > 2) {
System.out.println(“Error! I cannot hover above 2 feet.”);
}
else {
System.out.println(“Hovering…”);
batteryLevel = batteryLevel - 20;
System.out.println("Battery level is: " + (batteryLevel) + “percent.”);
}
}

public static void main(String[] args) {
Droid myDroid = new Droid(100);
myDroid.activate();
myDroid.chargeBattery(5);
myDroid.hover(2);
}
}

OUTPUT:

Activated. How can I help you?
Battery level is: 95 percent.
Droid charging…
Battery level is: 100percent.
Hovering…
Battery level is: 80percent.

Good afternoon, above is my completed code for the Droid project for Java. I had a few questions revolving around it. This code works, but some things about it seemed… odd?


For example, line 7 “batteryLevel = 100;” within the Droid constructor seems completely unnecessary for the accurate execution of the code even though step 3 in the instructions calls for it. The reasoning seems sound, but step 2 has you create “int batteryLevel” without a value that if executed results in an error. At first I was confused by this error because it seemed to me that batteryLevel of the Droid was defined within the Droid constructor as explained in the instructions. The error produced values of -5, 0, and -20 at the end which, using basic math, told me that the code had a Droid starting batteryLevel value of 0 rather than 100!

What was happening? It appeared as if the code was completely ignoring “batteryLevel = 100;” in line 7 within the Droid constructor. So I had to find a means of getting Java to recognize batteryLevel having an integer of 100. Given that step 2 had me create an “int batteryLevel” outside the constructor and the instructions sounded as if they were hinting at a change to this code later on, but never went back to, I deducted that this must be where I define it “Do not set it equal to anything at the moment.” So, I changed line 4 to say “int batteryLevel = 100” and it suddenly worked fine producing a code of 95, 100, 80! Well, if the code was receiving the initial value of the batteryLevel from outside the Droid constructor rather than from within it then this made line 7 pointless! I tested it and, just as I suspected, the code ran fine without it! So, why does step 3 call for line 7’s creation?


In the above code I used addition for line 18, as per the instructions, in step 10, but why isn’t it multiplication?

  batteryLevel = batteryLevel + hours;

Hours is defined in the main method as 5 on line 47 of step 25:

myDroid.chargeBattery(5);

I ask this for 2 reasons. First is that both multiplication and addition produce the same answer which doesn’t make mathematical sense. Second, shouldn’t it be multiplication rather than addition in the first place? I say this because if we set the hours to 5 then it will be batteryLevel = 100 + 5 = 105% or 1% per hour which is unrealistically slow, but if multiplied it should charge at a rate of 100% per hour or batteryLevel = 100 * 5 = 500%. So the thing that’s odd about that is that the code is returning a batteryLevel value of 100% in both instances despite the math for either showing different values from the one given and from each other? If you add “myDroid.checkBatteryLevel();” to the main method it will also produce a value of 100% thereby validating this observation after the if/else statements.


I would appreciate your input on what I logically find odd about these circumstances. Perhaps I am understanding the operation of Java incorrectly and if so please enlighten me!

Thank you for your time, considerations, and with best regards.


#2

Your constructor has access to two different variables both named batteryLevel, which one did you set to 100, and which one did you mean to set to 100?

When you charge a battery you keep the current charge, you don’t start from zero. So you’ll probably want to multiply charge rate by charge time and add it to pre-existing charge. Whether the rate is 1% per hour or not depends on the battery and supplied current. Storing five times the capacity of the battery on the other hand is very unrealistic, and indeed, you’ve got code which limits the charge to 100%


#3

The code returns a value of 0 for batteryLevel if

public Droid(int batteryLevel){
batteryLevel = 100;
}

is the only place where “batteryLevel” is set to equal 100. However, if I set the integer batteryLevel to equal 100 prior to this after creating the class Droid

class Droid {
int batteryLevel = 100;

the code returns a value of 100 with or without it being set to 100 in the

public Droid(int batteryLevel){
batteryLevel = 100;
}


ERGO

class Droid {
int batteryLevel = 100;

public Droid(int batteryLevel){}

= 100 while

class Droid {
int batteryLevel;

public Droid(int batteryLevel){
batteryLevel = 100;
}

= 0.

Correct, multiplying the time by a charge rate and adding that to the current charge up to 100% from a <100% charge would be more realistic. Where in the code though is it limiting the value to a maximum of 100?

Thanks for getting back to me!


#4

Are you sure you read that carefully enough?

As for where the 100 limit is, your code is very short. You can read it all to find where this is.


#5

Could you please state what you’re referring to? Having both present like the following = 100:

class Droid {
int batteryLevel = 100;

public Droid(int batteryLevel){
batteryLevel = 100;
}

Removing it from public Droid also produces 100:

class Droid {
int batteryLevel = 100;

public Droid(int batteryLevel){}

It’s only when you don’t have it immediately following the class that it equals 0:

class Droid {
int batteryLevel;

public Droid(int batteryLevel){
batteryLevel = 100;
}

So, why bother having it under “public Droid”, as the instructions clearly request, if it only matters after “class Droid” where the instructions don’t clearly state it?


I can see where the initial value is set to 100, but not where its limit or maximum is set hence why I asked?


#6

If you have two variables by the same name, and you’ve assigned to one of them, and don’t get the same value back, then you probably wrote to one of them and read from the other.

Then you’ve ruled out one occurrence of 100 in the code as being that limit. Look at the others. Not only do you not have to read it all because you already know it’ll mention 100, but you can also change the numbers to see if it has the expected effect. But really, reading the code should tell you where it is. It’s like if you have a piece of text describing a shopping trip and you want to find the part that picks up milk. If you read it, you’ll notice it, but you can also search for the word milk.


#7

So… you’re saying that perhaps the program wrote the value of batteryLevel to:

class Droid {
int batteryLevel = 100;

and read it from:

public Droid(int batteryLevel){}

That would only reinforce the idea that it was pointless to instruct the individual to set “batteryLevel = 100;” under “public Droid” and frustrating not to have clear instructions about “int batteryLevel” under “class Droid” where it really mattered “Do not set it equal to anything at the moment.”


if (batteryLevel >= 100) {
batteryLevel = 100;

reverts the batteryLevel to 100 even if you set the initial value under “classDroid” to 200, but this isn’t a limit just resets it. Changing the integer of myDroid from 100 also doesn’t change any limits.

public static void main(String[] args) {
Droid myDroid = new Droid(100);

I’m not seeing anything that limits it; just raises it, decreases it, or alters the initial state, but not limit it (ceiling)?


#8

The write in question is the one that you’re asking whether it’s doing anything at all. As I keep saying, you have a name collision, two variables share a name, meaning that if you write to one of them, you can read from the other and therefore perceive no effect

If you set a variable to at most 100 after each time you modify it, then yes, you are limiting it. Since you’ve found a piece of code that could create the behaviour you’re observing, you might consider if that piece of code is being used, like mentioned before, you can change the numbers to see if the limit changes

In both cases you’ve got some behaviour, and something that is a likely explanation. Go find out whether that’s the case. Assign to the other one instead, if the read now produces that value, then you confirmed that this was the explanation, or if it doesn’t, then you’ve discarded it.


#9

What do you mean by “perceive no effect”? I just want to know why not having it in one place = 0 while in another always = 100 that’s all.

Where is “to at most 100” anywhere in the code? Prior to the “if” statement the battery could be over 100 and setting it to 100 with an “if” statement doesn’t mean the battery is literally 100, but that it is going to report it as 100.

This entire time I’ve been modifying the code to better understand it. That’s why I started the thread in the first place to try and understand something that didn’t make sense after experimenting with the code…