Full file at

Similar documents
Console Input and Output

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Repetition, Looping. While Loop

COMP 202 Java in one week

Full file at

STUDENT LESSON A12 Iterations

Loops. CSE 114, Computer Science 1 Stony Brook University

Full file at

Chapter 1 Lab Algorithms, Errors, and Testing

Chapter 3. Selections

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Absolute C++ 6th Edition Savitch TEST BANK Full download at:

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Conditional Programming

Program Control Flow

Program Control Flow

COMP 202. Java in one week

Decisions: Logic Java Programming 2 Lesson 7

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Java Coding 3. Over & over again!

Over and Over Again GEEN163

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Logic & program control part 2: Simple selection structures

CMPT 125: Lecture 4 Conditionals and Loops

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Flow of Control. Chapter 3

Chapter 17. Iteration The while Statement

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Chapter 4: Conditionals and Recursion

Chapter 5 Lab Methods

CSC 1051 Data Structures and Algorithms I

Flow of Control: Loops. Chapter 4

Give one example where you might wish to use a three dimensional array

Programming with Java

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Chapter Goals. Contents LOOPS

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

Introduction to Java Applications

Algorithms and Conditionals

Lectures 3-1, 3-2. Control Structures. Control Structures, Shimon Schocken IDC Herzliya, slide 1

Problem Solving with C++

Repetition. Chapter 6

Repetition. Chapter 6

Mr. Monroe s Guide to Mastering Java Syntax

CS111: PROGRAMMING LANGUAGE II

Introduction to the Java Basics: Control Flow Statements

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Motivations. Chapter 5: Loops and Iteration. Opening Problem 9/13/18. Introducing while Loops

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Control Structures: if and while A C S L E C T U R E 4

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Control Statements: Part 1

CS1150 Principles of Computer Science Loops (Part II)

2.8. Decision Making: Equality and Relational Operators

Oct Decision Structures cont d

Flow of Control. Chapter 3

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

1 Short Answer (10 Points Each)

Absolute C++ Walter Savitch

Introduction to Computer Science Unit 2. Notes

Flow of Control. Chapter 3. Chapter 3 1

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

St. Edmund Preparatory High School Brooklyn, NY

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

AP CS Unit 3: Control Structures Notes

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Example: Monte Carlo Simulation 1

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Task #1 The if Statement, Comparing Strings, and Flags

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Chapter 3. More Flow of Control

Java I/O and Control Structures

The action of the program depends on the input We can create this program using an if statement

What methods does the String class provide for ignoring case sensitive situations?

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Chapter 5 Lab Methods

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

CONDITIONAL EXECUTION: PART 2

Pace University. Fundamental Concepts of CS121 1

Introduction to C Programming

Repetition CSC 121 Fall 2014 Howard Rosenthal

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Chapter 3 Lab Decision Structures

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Chapter 3. Ch 1 Introduction to Computers and Java. Selections

Transcription:

Chapter 3 Flow of Control Key Terms parentheses, p. 95 if-else, p. 95 if-else with multiple statements, p. 96 compound statements, p. 96 indenting, p. 98 multiway if-else, p. 98 switch statement, p. 101 controlling expression, p. 102 case labels, p. 102 break, p. 102 default, p. 102 conditional operator, p. 106 Boolean expression, p. 107 lexicographical ordering, p. 110 compareto, p. 110 comparetoignorecase, p. 111 && means and, p. 113 means or, p. 113 truth tables, p. 115 Boolean variables in assignments, p. 116 short-circuit evaluation, p. 118 lazy evaluation, p. 118 complete evaluation, p. 118 precedence rules, p. 119 associativity rules, p. 119 higher precedence, p. 119 binding, p. 122 side effects, p. 123 while and do-while compared, p. 126 executing the body zero times, p. 128 algorithm, p. 128 pseudocode, p. 128 sentinel values, p. 131 for statement, p. 132 empty statement, p. 137 infinite loop, p. 138 nested loop, p. 139 break statement, p. 142 continue statement, p. 142 label, p. 142

off-by-one error, p. 144 incremental development, p. 150 cod review, p. 150 pair programming, p. 150 assertion, p. 151 assert, p. 151 assertion check, p. 152 Brief Outline 3.1 Branching Mechanism If-else statements Omitting the else Compound Statements Nested Statements Multiway if-else Statement The switch Statement The Conditional Operator 3.2 Boolean Expressions Simple Boolean Expressions Lexicographic and Alphabetic Order Building Boolean Expressions Evaluating Boolean Expressions Short-Circuit and Complete Evaluation Precedence and Associativity Rules 3.3 Loops While statement and do-while statement Algorithms and Pseudocode The for Statement The Comma in for Statements Nested Loops The break and continue Statements The exit Statement 3.4 Debugging Loop Bugs Tracing Variables General Debugging Techniques Preventive Coding Assertion Checks Teaching Suggestions This chapter discusses flow of control using both selection and iteration. The if-statement and loops are introduced both in this chapter. With both topics in one chapter, it is conceivable that this chapter will take longer for students to work through than many of the others in the text.

Branching, or selection, is introduced using the if-statement, the if-else statement, and the switch statement. Multiple paths and therefore, nested if-else statements are introduced. As the last form of selection available, the conditional operator is introduced at the end of the chapter. The coverage of the conditional operator is optional and can be omitted. The section on Boolean expressions follows the introduction of selection. However, some instructors might choose to put the second section before the first so that more complicated conditions on if-statements can be introduced right away. The third section on looping introduces all three looping constructs available inside Java. Loops can pose many challenges for students as illustrated by the Pitfall sections in the chapter. Having all of these tools available to them, students can write some fairly complex programs at the end of this chapter. However, what usually begins to happen is that students will write code that has errors. Finding these errors can be tedious and time-consuming. The last section on tracing loops, common loop bugs, and debugging is included as a way to help students begin to understand how to track down their errors in code. An option that would be useful to them if it is available would be to introduce and discuss the use of your computing environment s debugger at this time. Key Points If-else statement & Multiway if-else Statement. There are many ways to give the program a sense of choice or branching. First, we can use an if-statement by itself without an else. This allows us the option to do something or skip over it. We can also use an if-else statement, which allows us to take one path or another. Lastly, we can use combinations of these to have more than two choices for execution. As the number of paths increase, so does the complexity of code for the students. Students should be able to follow as well as write these more complicated branching code segments. The switch Statement. The switch also allows for branching, but it has limitations as to what the condition for branching can be. Also, the syntax contains more keywords and is more structured than the if-else. Discussion of the break statement is needed here as the switch will not function properly without the correct use of break between the cases. The Methods equals and equalsignorecase. Since the equality operator cannot be used on objects, we have the equals method. On Strings, we also have the equalsignorecase method when we are concerned with the content of the String, not necessarily that all of the same words are capitalized within the String. The equalsignorecase can be important especially when taking user input. If you ask the user to input a certain letter as a choice, some of them might instinctively make that letter capital. If you are not ignoring case, equals may return false even if the user has pressed the correct key. The and Operator &&. In the section on Boolean Expressions, we introduce the Boolean operators && and. It is important for students to realize when the && operator returns true and when it returns false.

The or Operator. The introduction of the operator once again compels students to understand when it will return true and when it will return false in their programs. The Boolean Values are true and false. In Java, the values for the boolean data type are constants that are defined within the system. Therefore, they must always be written in all lower case as true or false. true and false are not Numbers. This point coincides with the last one because in some other languages, true and false can be represented as the numbers 1 and 0. However, in Java this is not the case and trying to use a number where the system expects a boolean value will cause a compiler error. You also can not typecast a number to a boolean or a boolean to a number. Rules for Evaluating Expressions. The precedence and associativity rules for evaluating expressions are given in charts within this chapter. Understanding how to evaluate a boolean expression is as important as how to evaluate the arithmetic expressions in Chapter 1. Syntax for while and do-while Statements. The while and do-while loops are the indefinite loops supported by Java. They also illustrate the differences between an entry-test and an exit-test loop. The for Statement. The for loop is a definite loop or counting loop that is also an entry-test loop. The syntax for the for loop is different from the other two loops and has a loop counter built right into the construct. However, in Java, we can have more than one statement inside the parts of the for-loop separated by commas and we can also leave parts empty, which can create many different results when using a for-loop. Assertion Checking. The assert statement can help you ensure that a particular condition is true at a given time in a program. You can use this feature to make sure that a variable is positive at a given point in the program, for example. You need to turn on assertion checking to make Java execute the assert statements in the program. If at any time, an assertion fails, the program will exit with an error. Tips Placing of Braces. The book introduces its way of placing braces for an if-statement. There are other options, which are presented. Style will vary on what an instructor/programmer wants. There is no correct way to place your braces. However, having braces or not definitely makes a difference in the code. Naming boolean Variables. It is helpful to name boolean variables with names that will indicate what they are holding the values for. Names like isfull, isempty, etc will help distinguish what it is that you are holding the value for and make the code that is written easier for others to read and understand. End of Input Character. This tip is optional and discusses how the user can indicate the end of input to the program. This involves the introduction of control characters in the system and uses the ConsoleIn classes in the example. Therefore, instructors that did not cover ConsoleIn will

not be able to cover this point to its entirety, but could discuss how the program will know when the user is done entering input to the program. Repeat N Times Loops. The easiest way to repeat something a definitive number of times is to use the for-loop. The loop counter does not have to be referenced in the loop body, so it can simply just keep track of how many times the loop has been executed. Pitfalls Forgetting a break in a Switch Statement. This is an important pitfall because of the fact that the compiler will not tell you that you have forgotten it. Simply, the program will not execute correctly. Therefore, it is important to show what will happen if the break statements are not properly inserted in the switch statement. Using = Place of ==. Now that we have introduced the notion of equality, one element that is of confusion is that the double equals sign is what actually tests for equality, while the single equals is assignment. This is different than in mathematics and can be confusing for students. In some instances, the confusion of the two may not even cause a compiler error, but once again the program will not run correctly. Using == with Strings. Since Strings are objects, the equality operator does not always give the correct result and should not be used to check for two Strings that contain the same character sequence. The equals method is introduced as the way to achieve the correct answer always to this problem. Strings of Inequalities. Unlike in mathematics, where x < 10 < y is understood, Java will not be able to understand the previous statement. Therefore, it needs to be broken up into two parts, x < 10 && 10 < y. Students who fail to do this will usually receive a compiler error. Extra Semicolon in a for Statement. The extra semicolon at the end of the for loop will not cause a compiler error, but will cause the loop to execute the proper amount of times with an empty loop body. This can also arise with the while loop. However, the do-while needs the semicolon at the end of its condition to work properly. Infinite Loops. It is quite possible and quite common to accidentally write a loop that never ends because the condition on the loop will never become false. These infinite loops will generally cause errors to be generated when the program is run and can be difficult to spot. It is helpful in these cases to use the technique of tracing variables to find the problem. Examples State Income Tax. In this example we see how to use multiway if-else statements with the example of computing how much state income tax one owes. Depending on your level of income, the program gives the amount of tax that should be paid. This problem s solution is given in the text in Display 3.1.

Averaging a List of Scores. The second example in this chapter looks at a program that will average a list of scores inputted by the user. This example brings together the input/output from last chapter and the looping that was introduced in this chapter. The code is once again presented in Display 3.8. Programming Projects Answers 1. / Question1.java This program uses the Babylonian algorithm, and keeps looping until the current guess is within 1% of the previous guess value to estimate the square root of a number n. Created: Sat Mar 05, 2005 @author Kenrick Mock @version 1 / import java.util.scanner; public class Question1 public static void main(string[] args) // Variable declarations Scanner scan = new Scanner(System.in); double guess, previousguess; int n; double r; System.out.println("This program estimate square roots."); System.out.println("Enter an integer to estimate the square root of: "); n = scan.nextint(); // Initial guess guess = (double) n/2; previousguess = n; // Keep looping as long as (oldguess - guess) / old guess is > 0.01. // The ratio should always be positive since we approach the // square root starting at n/2 and work our way down to the actual square root. while (((previousguess-guess)/previousguess) > 0.01)

r = (double) n / guess; previousguess = guess; guess = (guess+r)/2; System.out.println("Current guess: " + guess); System.out.printf("\nThe estimated square root of %d is %6.2f\n", n, guess); // Question1 2. / Question2.java This program simulates 10,000 games of craps. It counts the number of wins and losses and outputs the probability of winning. Created: Sat Mar 05, 2005 @author Kenrick Mock @version 1 / public class Question2 private static final int NUM_GAMES = 10000; / This is the main method. It loops 10,000 times, each simulate a game of craps. Math.random() is used to get a random number, and we simulate rolling two dice (Math.random() 6 + 1 simulates a single die). / public static void main(string[] args) // Variable declarations int numwins = 0; int numlosses = 0; int i; int roll; int point; // Play 10,000 games for (i=0; i<num_games; i++)

// Simulate rolling the two dice, with values from 1-6 roll = (int) (Math.random() 6) + (int) (Math.random() 6) + 2; // Check for initial win or loss if ((roll == 7) (roll == 11)) numwins++; else if ((roll==2) (roll==3) (roll==12)) numlosses++; else // Continue rolling until we get the point or 7 point = roll; do roll = (int) (Math.random() 6) + (int) (Math.random() 6) + 2; if (roll==7) numlosses++; else if (roll==point) numwins++; while ((point!= roll) && (roll!= 7)); // Output probability of winning System.out.println("In the simulation, we won " + numwins + " times and lost " + numlosses + " times, " + " for a probability of " + (double) (numwins) / (numwins+numlosses)); // Question2 3. / Question3.java This program estimates the height of a child using the formula: H(male) = (H(mother)13/12 + H(father))/2

H(female) = (H(father)12/13 + H(mother))/2 All heights are in inches. A function takes as input parameters the heights in inches and outputs the height in inches. Conversions are made by allowing the user to input the height in feet and inches. Created: Sat Mar 05, 2005 @author Kenrick Mock @version 1 / import java.util.scanner; public class Question3 / This is the main method. It loops repeatedly until the user stops by not entering "Y" to continue using a do-while loop. Input is accomplished through the Scanner class. / public static void main(string[] args) // Variable declarations int gender; // 0=male, 1=female int mom_feet, mom_inches; int dad_feet, dad_inches; int child_total_inches; String doagain; // Set to "Y" if user wants to try again Scanner scan = new Scanner(System.in); do System.out.println("Enter the gender of your future child. " + "Use 1 for female, 0 for male."); gender = scan.nextint(); System.out.println("Enter the height in feet then the height " + "in inches of the mom."); mom_feet = scan.nextint(); mom_inches = scan.nextint(); System.out.println("Enter the height in feet then the height " + "in inches of the dad."); dad_feet = scan.nextint();

dad_inches = scan.nextint(); // Convert input to all inches and get the estimate for the child int mother_height = (mom_feet12)+mom_inches; int father_height = (dad_feet12)+dad_inches; if (gender==0) // Male child formula child_total_inches = ((mother_height 13 / 12) + father_height)/2; else // Female child formula child_total_inches = ((father_height 12 / 13) + mother_height)/2; // Output the estimated height System.out.println("Your future child is estimated to grow to " + child_total_inches / 12 + " feet and " + child_total_inches % 12 + " inches."); System.out.println(); System.out.println("Enter 'Y' to run again, anything else to exit."); scan.nextline(); // Skip newline remaining from nextint doagain = scan.nextline(); while (doagain.equals("y")); // Question3 4. / Question4.java Created: Sun Nov 09 16:00:30 2003 Modified: Sat Mar 05 2005, Kenrick Mock @author Adrienne Decker @version 2 / import java.util.scanner; import java.text.numberformat;

public class Question4 public static void main (String[] args) Scanner keyboard = new Scanner(System.in); System.out.println("Enter the cost of the item " + "in the present as a decimal with" + " no dollar sign:"); double price = keyboard.nextdouble(); System.out.println("Enter the number of years from now" + " you will be purchasing the item:"); int years = keyboard.nextint(); System.out.println("Enter the rate of inflation as " + "a decimal number.\nfor " + "example, enter 5.6 for 5.6%"); double inflation = keyboard.nextdouble() / 100; double newprice = price; for ( int i = 0; i < years; i++ ) newprice = newprice + (newprice inflation); // end of for () NumberFormat moneyformater = NumberFormat.getCurrencyInstance(); System.out.println("in " + years + " years, your item that costs " + moneyformater.format(price) + " now, will cost " + moneyformater.format(newprice)); // end of main () // Question4 5. / Question5.java Created: Sun Nov 09 16:14:44 2003 Modified: Sat Mar 05 2005, Kenrick Mock @author Adrienne Decker @version 2 /

import java.text.numberformat; public class Question5 public static final int STEREO_COST = 1000; public static final double YEARLY_INTEREST = 0.18; public static final double MONTHLY_INTEREST = 0.015; public static final int MONTHLY_PAYMENT = 50; public static void main(string[] args) NumberFormat moneyformater = NumberFormat.getCurrencyInstance(); int monthnumber = 1; double amountremaining = STEREO_COST; double interestaccrued = 0; double diff = 0; double totalinterestpaid = 0; while ( amountremaining > 0.0) //System.out.println("For month number " + monthnumber); /System.out.println("You will pay " + moneyformater.format(monthly_payment));/ interestaccrued = amountremaining MONTHLY_INTEREST; totalinterestpaid = totalinterestpaid + interestaccrued; /System.out.println(moneyFormater.format(interestAccrued) + " will go towards interest.");/ diff = MONTHLY_PAYMENT - interestaccrued; /System.out.println(moneyFormater.format(diff) + " will go towards principle.");/ amountremaining = amountremaining - diff; /System.out.println("You still have " + moneyformater.format(amountremaining) + " to pay.");/ monthnumber++; // end of while () System.out.println("It will take you " + monthnumber + " months to pay off the stereo.");

System.out.println("You will have paid " + moneyformater.format(totalinterestpaid) + " in interest."); // Question5 6. / Question6.java Created: Sun Nov 09 16:14:44 2003 Modified: Sat Mar 05 2005, Kenrick Mock @author Adrienne Decker @version 2 / import java.util.scanner; public class Question6 public static void main(string[] args) Scanner keyboard = new Scanner(System.in); while ( true ) System.out.println("Enter the initial size of the green crud" + " in pounds.\nif you don't want to " + "calculate any more enter -1."); int initialsize = keyboard.nextint(); if ( initialsize == -1) break; // end of if () System.out.println("Enter the number of days: "); int numdays = keyboard.nextint(); int numofrepcycles = numdays / 5; int prevprevgen = 0; int prevgen = initialsize;

int finalanswer = initialsize; for ( int i = 0; i < numofrepcycles; i++ ) finalanswer = prevprevgen + prevgen; prevprevgen = prevgen; prevgen = finalanswer; // end of for () System.out.println("The final amount of green crud will be: " + finalanswer + " pounds.\n"); // end of while () // Question6 7. / Question7.java Created: Sun Nov 09 16:14:44 2003 Modified: Sat Mar 05 2005, Kenrick Mock @author Adrienne Decker @version 2 / import java.util.scanner; public class Question7 public static void main(string[] args) Scanner keyboard = new Scanner(System.in); String line; do System.out.println("Enter the value of X for this calculation."); double x = keyboard.nextdouble(); double sum = 1.0; double temp = 1.0; for ( int n = 1; n <= 10; n++)

System.out.print("For n equal to: " + n + ", the result of calculating e^x is: "); for ( int inner = 1; inner <= n; inner++) temp = 1.0; for ( double z = inner; z > 0; z--) temp = temp (x/z); // end of for () sum += temp; // end of for () System.out.println(sum); sum = 1.0; // end of for () System.out.print("For n equal to 50, the result of " + "calculating e^x is: "); for ( int n = 1; n <= 50; n++) temp = 1.0; for ( double z = n; z > 0; z--) temp = temp (x/z); // end of for () sum += temp; // end of for () System.out.println(sum); sum = 1; System.out.print("For n equal to 100, the result of " + "calculating e^x is: "); for ( int n = 1; n <= 100; n++) temp = 1.0; for ( double z = n; z > 0; z--) temp = temp (x/z); // end of for () sum += temp; // end of for () System.out.println(sum);

System.out.println("\nEnter Q to quit or Y to go again."); keyboard.nextline(); // Clear buffer line = keyboard.nextline(); while (line.charat(0)!='q' && line.charat(0)!='q'); // end of while () // Question7 8. / Question8.java This program calculates the solution to the cryptarithmetic puzzle TOO + TOO + TOO + TOO = GOOD where each letter represents a single digit with no duplication. It loops over all possible values for each digit, ensures that the digits are unique, computes the sum, and if the equation is satisfied outputs the values for each digit. We must make sure to account for the possibility of carries when adding digits. Created: Thu Mar 22 2007 @author Kenrick Mock @version 1 / public class Question8 public static void main(string[] args) // Variable declarations int t, o, g, d; // Loop over all values for "T", "O", "G", and "D" for (t = 0; t <= 9; t++) for (o = 0; o <=9; o++) for (g = 0; g <= 9; g++) for (d = 0; d <= 9; d++) // Ensure uniqueness for each digit if ((t!= o) && (t!= g) && (t!= d) && (o!= g) && (o!= d) && (g!= d))

System.out.println(); // Question 8 // Compute rightmost carry and digit int carry0 = (o + o + o + o) / 10; int digit0 = (o + o + o + o) % 10; // Compute second digit from right int carry1 = (carry0 + o + o + o + o) / 10; int digit1 = (carry0 + o + o + o + o) % 10; // Compute third digit from right int carry2 = (carry1 + t + t + t + t) / 10; int digit2 = (carry1 + t + t + t + t) % 10; // Check if equation matches7 if ((carry2 == g) && (digit2 == o) && (digit1 == o) && (digit0 == d)) System.out.println("The values are: T = " + t + " O = " + o + " G = " + g + " D = " + d);