17
answers
0
watching
471
views
26 Jul 2018
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.
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.
29 Jun 2023
Already have an account? Log in
marcusnicole284Lv10
16 Jun 2023
Already have an account? Log in
celluarsolLv10
7 Mar 2023
Already have an account? Log in
16 Feb 2023
Already have an account? Log in
papayaprofessorLv10
5 Sep 2022
Already have an account? Log in
4 Dec 2021
Already have an account? Log in
Beverley SmithLv2
28 Jul 2018
Already have an account? Log in