JAVA
Answer the following questions in the text box below 1. Describe the difference between the public and private modifiers for member variables. 2. Assume the following class definition and static member declaration: public class Constants { public static final long SPEED_OF_LIGHT = 299792458; // meters per second How would you access this member variable outside of the Constants class? 3. Study the following code and guess what the output will be then run it to check your answer. Include your guess and the actual output. public class CardGame { private static int dealCount = 0; public static void main(String[] args) { for (int i = 0; i <10; i++) deal(); System.out.printf("%d hands dealt\n", dealCount); } public static void deal() { System.out.println("Dealing the cards..."); dealCount++; } 4. Unlike "local" variables, static member variables will always get a default value when they are declared even if they haven't been initialized. What default values will be assigned to the following member variables? /* A */ private static int studentCount; /* B */ private static double balance; /* C */ private static boolean isComplete; /* D */ private static String error; 5. What is wrong with the following code? public class CardGame { private static int dealCount; dealCount = 0; // ... } Be specific about the reason for the error, don't just state what the error is (hint: see section 4.8.1)
JAVA
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am