Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (ellecullen):

Help With homework questions please: I tried all I can,these I have seem very hard.

OpenStudy (ellecullen):

Questions: SELECT ALL THAT APPLY: Which of the following phrases can be used within this doStuff method? class BankAccount { private double balance; static double dollar; public static void doStuff (double given) { double interest; ... }  A. this.balance  B. this.dollar C. this.given  D. this.interest E. none of the above Complete this coding to compute the balance at the end of 10 years when you put $1000 in a savings account at the end of each year and earn 5% interest annually. Use only one arithmetic operator in your answer. int k = 1; double balance = 1000; while (k <= 10) { k = k + 1; balance = ________ _________ ________ + 1000; } System.out.println ("saving $1000 per year gives $" + balance + " after 10 years."); The Test class is given below. Assume that ann and sue are variables of this Test class, where y is a sta class Test { private int x = 5; private static int y = 5; public void think () { x++; y++; } public void print () { System.out.println (x * y); } } A. 36 B. 42 C. 49 D. none of the above Complete this coding in such a way that it prints out all the values read in that are between 100 and 999 inclusive. Assume that read() is a method that obtains a number that the user enters. for (double x = read(); x != 0; x = read()) if (x <= _________ _________ _______ ) System.out.println (x); my thought please check (1000 && x >= 100) Complete this coding to compute the gross pay that a person receives when she gets time-and-ahalf for all hours over 40 worked in a week. if (hoursWorked <= 40) grossPay = hoursWorked * ratePerHour; else grossPay = 40 * ratePerHour + ________ _________ ________ * ratePerHour * 1.5; my answer used to be hoursWorked. The first number that this coding prints is _____ _________ ___________ . for (x = 1; x < 100; x *= 2); System.out.println (x); System.out.println (x); printed number = 128. Complete this statement to round a given positive double value to the nearest integer value. int rounded = _______ _________ _________ (given + 0.5);

OpenStudy (lyrae):

What do you think? Any guesses?

OpenStudy (ellecullen):

@Lyrae ,For the first question, I'm just putting what I think. What if we use this.given, will that work with the method?

OpenStudy (ellecullen):

I think we can use this.interest as well, as we have interest as a variable.

OpenStudy (lyrae):

A good guess! but sadly this one is a little bit tricky. Lets try and see what we have available ``` class BankAccount { private double balance; <- Instance variable static double dollar; <- Static variable public static void doStuff (double given) { <- Static method and declaration of double interest; <- Another local variable a local variabe ... } ``` the answers ``` A. this.balance <- (non-static) instance variable B. this.dollar <- (non-static) instance variable C. this.given <- (non-static) instance variable D. this.interest <- (non-static) instance variable E. none of the above ``` Can you figure out what I'm trying to show you? Do you know of the differences between static and non-static methods and variables?

OpenStudy (ellecullen):

I know difference between static and non static methods. Not the variables.

OpenStudy (lyrae):

The major difference is that a static variable can be used in both static and non-static methods. An instance variable (non-static) can however only be accessed from non-static methods.

OpenStudy (lyrae):

Furthermore the "this" keyword can only be used to access instance variables (i.e. non static variables).

OpenStudy (ellecullen):

According to your explanation, it looks like we can use this.given and this.interest in the method.

OpenStudy (lyrae):

Sorry, I confused you a bit there :P given and interest are local variables, "this" keyword can only be used to access instance variables.

OpenStudy (lyrae):

It's my fault for writing non-static in the parentheses.

OpenStudy (ellecullen):

okay, but am I correct in my answers, or did I do something wrong?

OpenStudy (lyrae):

Can you use "this" keyword to access given and interest? What did I write in my two previous posts?

OpenStudy (ellecullen):

Looks like I can't use "this" to access given and interest. So, it looks like I'm wrong.

OpenStudy (lyrae):

Yes! Any clue what the answer might be now?

OpenStudy (ellecullen):

the answer is most likely using this.balance with this.dollar as this question an have up to two answers.

OpenStudy (lyrae):

dollar is a static variable, can you use "this" to access static variables? balance is an instance variable, can you access instance variables in static methods? What did i write in my previous posts?

OpenStudy (ellecullen):

No, Then the only answer remains is this.balance

OpenStudy (lyrae):

balance is an instance variable, can you access instance variables in static methods?

OpenStudy (ellecullen):

Yes,

OpenStudy (lyrae):

"... An instance variable (non-static) can however only be accessed from non-static methods."

OpenStudy (ellecullen):

My method is static.... , it looks like none of the answers are correct but I can't leave this question blank.

OpenStudy (ellecullen):

Also, I have figured out question number 3 while the site was glitching. The answer to number 3 is 42.

OpenStudy (lyrae):

How about "E. none of the above"? As for the third I think a part of the question is missing?

OpenStudy (ellecullen):

I figured it out, but the first one has none of the above. Can you check my latest answer for question number 4? my latest answer is : if (x<= 999 && >= 100);

OpenStudy (lyrae):

4 Is almost correct, there's a x missing.

OpenStudy (lyrae):

5 is almost correct. Remember that hoursWorked contains all hours in the week including the 40 you've already calculated with 40*ratePerHour

OpenStudy (ellecullen):

@Lyrae

OpenStudy (ellecullen):

I still can't figure thatone out. Ican say hoursWorked/2for half time

OpenStudy (ellecullen):

whatdo you think of my answer so far? for 5

OpenStudy (lyrae):

Nope, you are not there quite yet.

OpenStudy (ellecullen):

I also thought on this, and 5 % can be put as 0.05 hoursWorked + 0.05 and then

OpenStudy (ellecullen):

can this work with gross pay calculation?

OpenStudy (ellecullen):

how would you start calculating gross pay using what the question gives?

OpenStudy (lyrae):

hoursWorked contains all hours worked in the week including the initial 40. in the first expression hoursWorked * ratePerHour you calculate the pay for the first 40 hours. What you are supposed to enter is how many excess hours the person worked. So if a person works 48 hours the excess time is 8 hours. or if another person works 43 hours the excess time is three hours.' How do you calculate the excess time if the total time is in hoursWorked?

OpenStudy (ellecullen):

I don't know but the best thing to do is to do 40/5

OpenStudy (lyrae):

48 - 40 = 8 43 - 40 = 3 total time - 40 = excess time

OpenStudy (ellecullen):

so, hoursWorked -40

OpenStudy (lyrae):

yup

OpenStudy (ellecullen):

This one is hard too... Complete this statement to round a given positive double value to the nearest integer value. int rounded = _______ _________ _________ (given + 0.5); my answer was 1. But I need help on understanding what I have to do on this one.

OpenStudy (lyrae):

1. ?

OpenStudy (ellecullen):

I thought of the math rule 5 or more raise it high. so I thought 0.5 would round to 1

OpenStudy (lyrae):

The correct answer have to do with casting. When a double is casted to an integer all decimals will be lost. For example ```java double a = 4.2, b = 2.9; int c = (int) a; int d = (int) b; System.out.println("c="+c+", d="+ d); ``` prints ``` c=4, d=2 ``` Java don't round anything automatically or mathematically.

OpenStudy (ellecullen):

so, int rounded = (int) given +0.5 I also found a few other things that we can try: Math.ceil (x) (int) round (float a)

OpenStudy (e.mccormick):

Yah, all of those should work.

OpenStudy (ellecullen):

can you check my answer please? I wantedto put int rounded = (int) given + 0.5.Whatwould you put?

OpenStudy (e.mccormick):

That method is valid. You just might want to put some other paren in there: int rounded = (int) (given + 0.5) See, that makes sure it is the sum that gets cast to an int.

OpenStudy (ellecullen):

okay :). I get it. Iwanted to ask if you know anything about this: Complete this coding to compute the balance at the end of 10 years when you put $1000 in a savings account at the end of each year and earn 5% interest annually. Use only one arithmetic operator in your answer.

OpenStudy (ellecullen):

I tried something and I ended up doing (0.05 * 10). How do I calculate the balance? thequestion is confusing

OpenStudy (e.mccormick):

Well, the arithmetic operators ar /*+-. If you can only use one of them, you need to find out which one. However, algebra can help here. It is the same idea as: 5x+x = 6x If I show that the long way: 5x+x factor out the x: x(5+1) x(6) 6x Well, you can do the same thing with: interest = ballance * .08 ballance + interest = new balance And loop it 10 times. So interest = ballance * .08 balance += interest But that still has two operators.... However, if I sub in for interest: balance += ballance * .08 Still two, but now it looks a lot more like the x(5+1) and can be simplified more...

OpenStudy (ellecullen):

so, in my case, Ican just do this: balance = (balance *0.05) + 1000 ?

OpenStudy (ellecullen):

the 0.05 represents 5%

OpenStudy (ellecullen):

the x(5+1) can be x(6)

OpenStudy (e.mccormick):

Well, 1000 is the original ballance. This wil compound every year, so: balance = (balance *0.05) + balance

OpenStudy (e.mccormick):

Do you see how to get rid of the + now?

OpenStudy (ellecullen):

Yes, I see. I can say that (1000 * 0.05) could be the answer, and we don't need the balance as our original is 1000

OpenStudy (ellecullen):

1000 * 0.05 = 50

OpenStudy (e.mccormick):

Um, not quite. Lets just take this: balance *0.05 + balance What do I get if I factor out ballance?

OpenStudy (ellecullen):

if you do balance *0.05 + balance -balance - balance _________________________ 0.05 if the balance is factored out, we have just plain 0.05

OpenStudy (e.mccormick):

No. To factor is not to subtract, but to divide: \(\dfrac{x}{x}=1\) I will just use b for ballance: \(b*.05+b\) \(b\left(\dfrac{b*.05}{b}+\dfrac{b}{b}\right)\) Do you see how that would cancel, leaving a lot of 1s?

OpenStudy (ellecullen):

yes, I see it. I have b (0.05) It seems like the ( means to multiply .

OpenStudy (e.mccormick):

No, you dropped part still... It would simplify to: b(1*.05+1) Now, the multiply by 1 is simple, that is just itself: b(.05+1) But what happens to the .05+1?

OpenStudy (ellecullen):

it becomes 1.05 b (1.05)

OpenStudy (e.mccormick):

=) Yah. so in code: ballance = ballance * 1.05 Or: ballance *= 1.05

OpenStudy (e.mccormick):

And that is one operator...

OpenStudy (ellecullen):

okay :) Thank you so much for the help. Will you be online later today?

OpenStudy (ellecullen):

I have a homework piece with fill in the blank coding. If I need any help, I will be online.

OpenStudy (e.mccormick):

Well, I may or may not. Never know.

OpenStudy (ellecullen):

okay:) see you next time! :)

OpenStudy (lyrae):

A quick note on: "so, int rounded = (int) given +0.5 I also found a few other things that we can try: Math.ceil (x) (int) round (float a)" Math.ceil(double x) works if you subtract five (half down rounding). To get a similar effect of casting to int you have to use Math.floor() (half up rounding). This won't however help you as the java floor, ceil and round functions returns doubels and not ints. If you try to substitute ``` int rounded = (int) given +0.5 ``` with ``` int rounded = Math.floor(given +0.5); ``` the compiler will most likely say something like ``` Main.java:6: error: possible loss of precision int rounded = Math.floor(given+0.5); ^ required: int found: double ``` The compiler is essentially hinting that assigning a double to int will result in loss of decimals. To fix this you have cast the result to an int before assigning it. ``` int rounded = (int) Math.floor(given +0.5); ``` However since the result is same as if you didn't use Math.floor(), I don't see any reason to to use it. The best option (if possible) is to use the built in Math.round(double x) ``` int rounded = (int) Math.round(given); ``` it deals with some special cases, not covered by the casting trick.

OpenStudy (ellecullen):

Thank you, :) Thank you Lyrae and emccormick :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!