Java puzzle 27: Implementing interfaces and casting

  Quizzes     Assignments     Puzzles     Easy exercises     Required knowledge  
< Java Puzzles   Green = Easy,  Blue = Normal,  Red = Hard
Select    01   02   03   04   05   06   07   08   09   10   11   12   13   14   15 
 16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   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

  1. The statement should assign both values 3100.0 and 1350.0 respectively to th variables wage and tax.
  2. 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.
  3. So, the statement is fr = (Freelancer) pay;





Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

2 Comments

Add a Comment
  1. Your “Answer explanation” line #3 is not correct, it should be:

    fr = (Freelancer) pay;

    1. Thanks Greg, you’r right the “freelance” object doesn’t exist it is “fr” object. I have fixed it.

Leave a Reply

Your email address will not be published. Required fields are marked *