A Story Problem

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.
go to previous page   go to home page   hear noise   go to next page

Answer:

Yes. The last line could be written

    System.out.print("Sum: " + (first + second) );

Now the declaration of sum and the statement that calculates it can be removed. The parentheses in (first + second) are necessary to show that the addition first + second should be done first, followed by conversion to characters and concatenation to "Sum: "

A Story Problem

Say that you want a program that calculates the total bill for a restaurant meal. The input to the program will be the cost of the meal. To this is added 6% sales tax and a 20% tip on the before-tax cost. Arithmetic will be done with type double. Here is the program, with some parts left out:

import java.io.*;
import java.util.Scanner;

class RestaurantBill
{
  public static void main (String[] args)  

  {
    
    double basicCost;

    
    

    System.out.println("basic cost: " +  basicCost + " total cost: " + 
         );
  }
}

Here are the missing parts, but not in order:

(basicCost + basicCost*0.06 + basicCost*0.20)

System.out.print("Enter the basic cost: ");

Scanner scan = new Scanner( System.in );

basicCost = scan.nextDouble();

Copy these missing parts and paste into the blanks to complete the program. (Copy by highlighting a part with the mouse and hitting control-C. Paste by clicking in a box and hitting control-V.)

QUESTION 8:

Fill in the missing parts.