Discount Program

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   go to next page        

Answer:

Nothing. A file named output.txt in the current subdirectory will have its contents replaced with the output of the program. But files in other subdirectories are safe.

Discount Program

Here is a (slightly) more realistic program that computes the discounted price of an item, given the list price and the percentage discount:

import java.util.Scanner;
class Discount
{
  public static void main ( String[] args ) 
  {
    int listPrice;
    int discount;
    int discountPrice;

    Scanner scan = new Scanner( System.in );

    System.out.print("Enter list price in cents: ");
    listPrice = scan.nextInt();

    System.out.print("Enter discount in percent: ");
    discount = scan.nextInt();

    discountPrice = listPrice - (listPrice*discount)/100 ;

    System.out.println( "Discount Price: " + discountPrice );
  }
}

Here is an example of the normal operation of this program:

C:/users/default/JavaLessons>java Discount
Enter list price in cents: 100
Enter discount in percent: 10
Discount Price: 90

C:/users/default/JavaLessons>

QUESTION 11:

Can the output of this program be redirected to a file?