<< |
Java Puzzles Green = Easy, Blue = Normal, Red = Hard P25 P26 P27 P28 P29 P30 By title |
>> |
---|
The output of this program is 3200.0, 1199.0. Write only one statement at line 8.
As a result of your statement, the output of this code will be 3100.0, 1350.0
What is that statement?
Java puzzle 27: Implementing interfaces and casting
Level: Hard
public class Test { public static void main(String[] args) { Payable pay = new Freelancer(3400); pay.setTax(); Freelancer fr = new Freelancer(3500); // add your statement here! System.out.print(fr.wage + ", "); System.out.print(fr.tax); } } class Freelancer implements Payable { public double wage; public double tax = 1234; public Freelancer(double wage) { this.wage = wage - 300; this.tax -= wage / 100; } public void setTax() { this.tax += 150; } } interface Payable { public void setTax(); }
Author: Sar Maroof
Answer explanation
- The statement should assign both values 3100.0 and 1350.0 respectively to th variables wage and tax.
- The reference pay = new Freelancer(3400); assigns the value 3400 – 300 = 3100.0 to the variable wage, and assigns the value (tax = 1234 – 3400/100 = 1200) 1200 to the variable tax. By invoking the method pay.setTax(); the value (this.tax += 150 = 1200 + 150 = 1350) 1350 is assigned to the variable tax.
- So, the statement is freelancer = (Freelancer) pay;
If you found this difficult, you might consider reading my book Java quizmaster for beginners. It is easy to understand, organized to learn Java in 17 days and it guides you to master Java code by solving 105 quizzes and 117 assignments. This book explains each chapter properly before starting with exercises and assignments. It is available on Amazon. See inside the book here!
Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.
Suggested Articles
Books by Sar Maroof |
![]() ![]() ![]() ![]() |
![]() ![]() ![]() |