Program a day Solution 4
From CompSciWiki
COMP 1010 Home > Program_a_day
IntroductionAn exercise to evaluate a skill-testing question.
|
---|
{{{Body}}}
What do you need to know?
- How to evaluate an expression.
- How to print the result of evaluating an expression.
- Where you can insert brackets into an expression.
Inputs
No inputs are required.
Outputs
- One line for each different result using brackets. E.g.,
6 x 8 - (5 + 9) = 34
- One line evaluating the expression without brackets.
Transformation
- Insert brackets into the expression in many different ways.
- Print out only one line with each different possible result.
Solution
import javax.swing.JOptionPane ; import java.util.Date ; /*******************************************************BracketedExpression * Purpose: to insert brackets to evaluate 6 x 8 - 5 + 9 in many * different ways. * COMP 1010 Section A06 * Instructor: Terry Andres * Assignment: Example #4 * @author Stew Dent * @version 2009-Sep-22 */ public class BracketedExpression { //*******************************************************************main public static void main(String[] args) { System.out.println( "(6 x 8) - 5 + 9 = " + ((6 * 8) - 5 + 9) ) ; // System.out.println( "(6 x 8 - 5) + 9 = " + ((6 * 8 - 5) + 9) ) ; // System.out.println( "(6 x 8 - 5 + 9) = " + ((6 * 8 - 5 + 9)) ) ; System.out.println( "6 x (8 - 5) + 9 = " + (6 * (8 - 5) + 9) ) ; System.out.println( "6 x (8 - 5 + 9) = " + (6 * (8 - 5 + 9)) ) ; System.out.println( "6 x 8 - (5 + 9) = " + (6 * 8 - (5 + 9)) ) ; // System.out.println( "((6 x 8) - 5) + 9 = " + (((6 * 8) - 5) + 9) ) ; // System.out.println( "((6 x 8) - 5 + 9) = " + (((6 * 8) - 5 + 9)) ) ; // System.out.println( "((6 x 8 - 5) + 9) = " + (((6 * 8 - 5) + 9)) ) ; // System.out.println( "(6 x (8 - 5) + 9) = " + ((6 * (8 - 5) + 9)) ) ; // System.out.println( "(6 x (8 - 5 + 9)) = " + ((6 * (8 - 5 + 9))) ) ; // System.out.println( "(6 x 8 - (5 + 9)) = " + ((6 * 8 - (5 + 9))) ) ; // System.out.println( "6 x ((8 - 5) + 9) = " + (6 * ((8 - 5) + 9)) ) ; System.out.println( "6 x (8 - (5 + 9)) = " + (6 * (8 - (5 + 9))) ) ; System.out.println( "6 x 8 - 5 + 9 = " + (6 * 8 - 5 + 9) ) ; System.out.println("\nProgrammed by Stew Dent") ; System.out.println("Date: " + new Date() ) ; // current date System.out.println("End of processing") ; } }
Template loop detected: Template loop detected: