Exercise (Solutions)

E1

Modify all the exercise programs from Logic Controls lesson and add Exception blocks where ever appropriate.

E2

Find if a given string from the Scanner input is a palindrome or not? For e.g., if the input string is ‘madam’ then it is a palindrome if the input string is ‘light’ then it is not a palindrome. The program should be in a continuous loop expecting user input. If the user does not give any input, then it should not throw an exception. You have to handle the user input mismatch and display appropriate messages to the user to provide the correct input.

E3

Consider the following class:

public class IdentifyMyParts {
    public static int x = 7;
    public int y = 3;
}

What are the class variables?

What are the instance variables?

What is the output from the following code?

IdentifyMyParts a = new IdentifyMyParts(); 
IdentifyMyParts b = new IdentifyMyParts(); 
a.y = 5; 
b.y = 6; 
a.x = 1; 
b.x = 2; 
System.out.println("a.y = " + a.y); 
System.out.println("b.y = " + b.y); 
System.out.println("a.x = " + a.x); 
System.out.println("b.x = " + b.x); 
System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);

Justify the reasoning for the values you see in the println statements.

results matching ""

    No results matching ""