0% found this document useful (0 votes)
11 views4 pages

Assignment2 Questions

Uploaded by

mainak das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Assignment2 Questions

Uploaded by

mainak das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

J2SE Lab Sessions – PG-DAC 2013

Use the below command to upload your assignments

scp -r dac02001_11 [email protected]:/course/submit/assignment2

dac02001_11 is a folder which contains the java source code files.

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)

Here's a sample run

Enter the menu item number to select an operation.


3
OK, you have entered 3.
Enter the two numbers
10 20
The output is as follows:
You have selected 3. Product
Numbers entered are 10 and 20
Result is 200

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) {

String searchMe = "Look for a substring in me";


String substring = "sub";
boolean foundIt = false;

int max = searchMe.length() -


substring.length();

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.

5. Following is a list of keywords.Create a class diagram showing the appropriate inheritance


hierachy.Make the required classes abstract wherever necessary.
Lion
Dog
Canine
Cat
Feline
Hippo
Tiger
Animal
Wolf
Place the following animal traits in appropriate classes obtained from the above inheritance
hierarchy.
makeNoise()
eat()
sleep()
roam()

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.

6. Create a class OuterClass with the following:


• a private integer variable outerX with value 10
• printOuterX()
• sayHello() with a local inner class named LocalInnerClass and an annonymous inner
class.
The LocalInnerClass is as shown below:
class LocalInnerClass {
public void greetInLocal() {
System.out.println("Hello World in LocalInnerClass!!");
}
public void printXInLocal() {
System.out.println("outerX :" + outerX + " in LocalInnerClass");
}
}
The anonymous inner class AnonymousInnerClass as shown below:
class AnonymousInnerClass {
public void printHelloWorld() {
System.out.println("Hello World in AnonymousInnerClass!!");
}

}
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

7. Implement a class EmployeeDemo which contains


• an inner class Employee.An employee has a name and salary.Write a default
constructor and parametrised constructor with parameters(name and salary).Also,
write a member method printEmpDetails() which prints name and salary.
• main() which creates an Employee object e with the following details
Name : Harry Smith
Salary : 10000
using parametrised constructor and prints employee details using printEmpDetails().

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.

You might also like