Check Your Understanding
Defining Classes
-
What is encapsulation? Why is it useful?
-
Instance variables are a part of the hidden implementation of a class, but they aren’t actually hidden from programmers who have the source code of the class. Explain to what extent the private reserved word provides information hiding.
-
Consider a class
Grade
that represents a letter grade, such as A+ or B. Give two choices of instance variables that can be used for implementing theGrade
class. -
Consider a class
Time
that represents a point in time, such as 9 a.m. or 3:30 p.m. Give two different sets of instance variables that can be used for implementing theTime
class. -
Show that the BankAccount(double initialBalance) constructor is not strictly necessary. That is, if we removed that constructor from the public interface, how could a programmer still obtain BankAccount objects with an arbitrary balance?
Conversely, could we keep only the BankAccount(double initialBalance) constructor and remove the BankAccount() constructor? -
What is the this reference? Why would you use it?
-
Which of the methods in the
CashRegister
class are accessor methods? Which are mutator methods? - Write declarations for storing the following quantities. Choose between integers and floating-point numbers. Declare constants when appropriate.
- The number of days per week
- The number of days until the end of the semester
- The number of centimeters in an inch
- The height of the tallest person in your class, in centimeters
-
What is the value of
mystery
after this sequence of statements?int mystery = 1; mystery = 1 - 2 * mystery; mystery = mystery + 1;
-
What is wrong with the following sequence of statements?
int mystery = 1; mystery = mystery + 1; int mystery = 1 - 2 * mystery;
- Write the following Java expressions in mathematical notation.
dm = m * (Math.sqrt(1 + v / c) / Math.sqrt(1 - v / c) - 1);
volume = Math.PI * r * r * h;
volume = 4 * Math.PI * Math.pow(r, 3) / 3;
z = Math.sqrt(x * x + y * y);
-
What are the values of the following expressions? In each line, assume that
double x = 2.5; double y = -1.5; int m = 18; int n = 4;
x + n * y - (x + n) * y
m / n + m % n
5 * x - n / 5
1 - (1 - (1 - (1 - (1 - n))))
Math.sqrt(Math.sqrt(n))
-
What are the values of the following expressions, assuming that n is 17 and m is 18?
n / 10 + n % 10
n % 2 + m % 2
(m + n) / 2
(m + n) / 2.0
(int) (0.5 * (m + n))
(int) Math.round(0.5 * (m + n))
-
What are the values of the following expressions? In each line, assume that
String s = "Hello"; String t = "World";
s.length() + t.length()
s.substring(1, 2)
s.substring(s.length() / 2, s.length())
s + t
t + s
-
Given a string
s
, write expressions for:
• The string consisting of the first letter
• The string consisting of the first and last letter
• The string consisting of all but the first and last letter
• The first half of the string (discarding the middle letter if the string has odd length) • The second half of the string (discarding the middle letter if the string has odd length) • The string consisting of the middle letter if the string has odd length, or the middle two letters otherwise -
Find at least five compile-time errors in the following program.
public class HasErrors { public static void main(); { System.out.print(Please enter two numbers:) x = in.readDouble; y = in.readDouble; System.out.printline("The sum is " + x + y); } }
-
Find three run-time errors in the following program.
public class HasErrors { public static void main(String[] args) { Scanner in = new Scanner("System.in"); System.out.print("Please enter an integer:"); x = in.readInt(); System.out.print("Please enter another integer: "); x = in.readInt(); System.out.println("The sum is " + x + y); int x = 0; int y = 0; } }
-
Consider the following code:
CashRegister register = new CashRegister(); register.recordPurchase(19.93); register.receivePayment(20, 0, 0, 0, 0); System.out.print("Change: "); System.out.println(register.giveChange());
The code segment prints the total as 0.07000000000000028. Explain why. Give a recommendation to improve the code so that users will not be confused.
-
Explain the differences between 2, 2.0, ‘2’, “2”, and “2.0”.
- Explain what each of the following program segments computes.
x = 2;
y = x + x;
s = "2";
t = s + s;