Yet More Practice

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

Here is one correct answer:

  public static void main ( String[] args )
  {
    Goods   toy ;
    Taxable tax = new Toy ( "Building Blocks", 1.49, 6 );

    toy = (Toy)tax;
    toy.display();
    System.out.println( "Tax: "+ ((Taxable)toy).calculateTax() );
  }

The first type cast must cast tax to a type Goods (or an ancestor). The second must cast toy to Taxable or to a class that implements Taxable.

Yet More Practice

Here is another possible answer:

  public static void main ( String[] args )
  {
    Goods   toy ;
    Taxable tax = new Toy ( "Building Blocks", 1.49, 6 );

    toy = (Goods)tax;
    toy.display();
    System.out.println( "Tax: "+ ((Toy)toy).calculateTax() );
  }

QUESTION 21:

Is this answer correct?