5. Consider the following class:
import java.util.Scanner;
public class Money
private int dollars;
private int cents;
public Money (int d, int c) {
dollars = d;
cents = c;
/* Point 1 */
public String toString() {
String result;
if (cents > 100)
int totalCents = dollars * 100 + cents;
dollars = totalCents / 100;
cents = total Cents % 100;
/* Point 2 */
/* Point 3 */
result = "$" + dollars + ", " + cents + "C";
return result;
public static void main(String[] yourGrandfather)
Scanner keyboard - new Scanner (System.in);
Money amount;
int C;
/* Point 4 */
System.out.print("Enter an amount in cents: ");
C = keyboard.nextInt ();
amount = new Money (c/ 100, CX 100);
System.out.println("You have: " + amount.toString());
The above program compiles and runs without error.
Which variables are in scope at each of the points marked by comments in the above class? In other
words, if each of these comments were replaced by an actual Java statement, which variables could
be used in this statement without a prefix (that is, without having to write the name of a class or other
variable in front of the variable name) and without the compiler reporting an unknown variable error?
List each variable that is in scope at each of the points marked by comments in the above class, and
indicate whether it is an instance variable, a formal parameter, or a local variable. Note that you will
be penalized for every variable that is out of scope at a given point but that you list as being in scope
at that point.