Assignment2 Questions
Assignment2 Questions
Assignment 2
1. Write a program that displays a menu as shown below.The user is then prompted to enter
his choice and two integers.Based on his choice, a particular function will be performed on
the two integers.
USER MENU
==========
Sum
Difference
Product
Average
Maximum (larger of two numbers)
Minimum (Smaller of two numbers)
2. Complete the following code snippet by adding a continue statement(with label) and a break
statement(with label)) in appropriate places .Finally,the below code should search for a
substring in a given string.Here the given string is Look for a substring in me, substring to
be searched is sub and the label-name is test.
class ContinueWithLabelDemo {
public static void main(String[] args) {
test:
for (int i = 0; i <= max; i++) {
int n = substring.length();
int j = i;
int k = 0;
while (n-- != 0) {
if (searchMe.charAt(j++) != substring.charAt(k++)) {
}
}
foundIt = true;
}
System.out.println(foundIt ? "Found it" : "Didn't find it");
}
}
Note:
• The unlabeled continue statement skips the current iteration of the innermost for, while ,
or do-while loop and evaluates the boolean expression that controls the loop. A
labeled continue statement skips the current iteration of an outer loop marked with the given
label.
• An unlabeled break statement terminates the innermost switch, for, while, or do-while
statement, but a labeled break terminates an outer statement. The break statement terminates
the labeled statement; it does not transfer the flow of control to the label. Control flow is
transferred to the statement immediately following the labeled (terminated) statement.
3. Write a program that reads an unspecified number of integer arguments from the command
line and adds them together to print the result.
For example, suppose that you enter the following:
java Adder 1 3 2 10
The program should display 16 and then exit. The program should display an error message
if the user enters only one argument.
4. Create two java classes SCounter (with static integer count variable ) and NSCounter (with
integer count variable).
• Implement constructors which increments count variable by 1 and prints the same (in
both classes).Use this keyword to refer to count in constructor.
• Write a CounterDemo class which contains the main() and tests the same by creating 3
objects each for SCounter and NSCounter classes.
• Write a static block which prints
Static block invoked in CounterDemo in CounterDemo Class.
Static block invoked in SCounter in SCounter Class.
Compile and run the program to check the same.
food
hunger_level
Note: Canine means dog family and they always roam in packs/groups.Feline means cat
family and they tend to avoid others of their kind while roaming.
}
The main() should make a call to OuterClass's sayHello() method.
Modify the sayHello() such that the following output is obtained:
Hello World in LocalInnerClass!!
outerX :10 in LocalInnerClass
Hello World in sayHello!!
outerX :10 in AnonymousInnerClass
8. Write a program that gets an array of integers from user and prints their square root.
Implement a class SquareRootDemo with two static methods main() and
findSquareRoot(double a).The findSquareRoot method returns a double value.
Note: Find square root without using any java in-built methods.