0% found this document useful (0 votes)
2 views19 pages

Module 2 Assignment Questions

The document contains multiple Java code examples demonstrating various programming concepts such as access modifiers, object-oriented principles, method overloading, inner classes, constructors, and recursion. Each example illustrates a specific concept, including public and private access, volume calculations for boxes, method overloading, and the use of constructors. The document serves as an educational resource for understanding Java programming fundamentals.

Uploaded by

saras09845
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)
2 views19 pages

Module 2 Assignment Questions

The document contains multiple Java code examples demonstrating various programming concepts such as access modifiers, object-oriented principles, method overloading, inner classes, constructors, and recursion. Each example illustrates a specific concept, including public and private access, volume calculations for boxes, method overloading, and the use of constructors. The document serves as an educational resource for understanding Java programming fundamentals.

Uploaded by

saras09845
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

MODULE 2 ASSIGNMENT QUESTIONS

1. /* This program demonstrates the difference between public and private.


*/

class Test {
int a; // default access
public int b; // public access
private int c; // private access
// methods to access c
void setc(int i) { // set c's value
c = i;
}
int getc() { // get c's value
return c;
}
}

class AccessTest {
public static void main(String args[]) {
Test ob = new Test();
// These are OK, a and b may be accessed directly
ob.a= 10;
ob.b= 20;
// This is not OK and will cause an error
//ob.c = 100; // Error!
// You must access c through its methods
[Link] (100); // OK
[Link]("a, b, and c: " + ob.a + " " + ob.b +" " + [Link]());
}

2. class Box {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;

// assign values to mybox's instance variables

[Link] = 10;
[Link] = 20;
[Link] = 15;
// compute volume of box
vol = [Link] * [Link] * [Link];
[Link]("Volume is " + vol);
}

3. class Box {
double width;
double height;
double depth;
}

class BoxDemo2 {
public static void main(String args[]) {
Box mybox1;
mybox1 = new Box();
Box mybox2 = new Box();
double vol;

// assign values to mybox1's instance variables


[Link] = 10;
[Link] = 20;
[Link] = 15;

/* assign different values to mybox2's instance variables */


[Link] = 3;
[Link] = 6;
[Link] = 9;
// compute volume of first box
vol = [Link] * [Link] * [Link];
[Link]("Volume is " + vol);

// compute volume of second box


vol = [Link] * [Link] * [Link];
[Link]("Volume is " + vol);
}
}

4. class Box {
double width;
double height;
double depth;
// display volume of a box
void volume() {
[Link]("Volume is ");
[Link](width * height * depth);
}

class BoxDemo3 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
// assign values to mybox1's instance variables
[Link] = 10;
[Link] = 20;
[Link] = 15;

[Link] = 3;
[Link] = 6;
[Link] = 9;
[Link]();
[Link]();
}
}
5. class Box {
double width;
double height;
double depth;
// compute and return volume
double volume() {
return width * height * depth;
}

class BoxDemo4 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
[Link] = 10;
[Link] = 20;
[Link] = 15;
/* assign different values to mybox2's
instance variables */
[Link] = 3;
[Link] = 6;
[Link] = 9;
// get volume of first box
vol = [Link]();
[Link]("Volume is " + vol);
// get volume of second box
vol = [Link]();
[Link]("Volume is " + vol);
}

}
6. class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth;
}

void setDim(double w, double h, double d) {


width = w;
height = h;
depth = d;
}
}

class BoxDemo5 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
[Link](10, 20, 15);
[Link](3, 6, 9);
vol = [Link]();
[Link]("Volume is " + vol);
vol = [Link]();
[Link]("Volume is " + vol);
}

7. class Box {

double width;
double height;
double depth;
Box() {
[Link]("Constructing Box");
width = 10;
height = 10;
depth = 10;
}

double volume() {
return width * height * depth;
}
}

class BoxDemo6 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
vol = [Link]();
[Link]("Volume is " + vol);
vol = [Link]();
[Link]("Volume is " + vol);
}

8. class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
double volume() {
return width * height * depth;
}

class BoxDemo7 {
public static void main(String args[]) {

Box mybox1 = new Box(10, 20, 15);


Box mybox2 = new Box(3, 6, 9);

double vol;
vol = [Link]();
[Link]("Volume is " + vol);
vol = [Link]();
[Link]("Volume is " + vol);
}

9. // Primitive types are passed by value.


class Test {
void meth(int i, int j) {
i *= 2;
j /= 2;
}

class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
[Link]("a and b before call: " + a + " " + b);
[Link](a, b);
[Link]("a and b after call: " + a + " " + b);
}

10. class Pradeep{


Pradeep(){
[Link]("Pradeep");
}

Pradeep(int i){
[Link]("Pradeep");
}

}
class MainClass{
public static void main(String[] args){
Pradeep Pr=new Pradeep();
Pradeep Pr1=new Pradeep(1);
}

11. class Outer {


int outer_x = 100;

class Inner {
int y = 10;
void display() {
[Link]("display: outer_x = " + outer_x);
}

void test() {
Inner inner = new Inner();
[Link]();
[Link]("Inner y = " + inner.y);
}

class Demo1 {
public static void main(String[] args) {
Outer outer = new Outer();
[Link]();
}

}
12. //Non-Static Inner Class
class Outer {
int outer_x = 100;

class Inner {
void display() {
// Can access outer_x because it's non-static
[Link]("Outer_x = " + outer_x);
}
}

public class Demo2 {


public static void main(String[] args) {
Outer outer = new Outer();
[Link] inner = [Link] Inner();
[Link]();
}
}

13. //static inner class

class Outer {
static int outer_x = 100;

static class Inner {


void display() {
// Can only access static outer members
[Link]("Outer_x = " + outer_x);
}

public class Demo3 {


public static void main(String[] args) {
[Link] inner = new [Link]();
[Link]();
}
}
14. class A{
final int a=5;
void show(){
a=4;
[Link]("Hi"+a);
}

class B extends A{
void show(){
[Link]("Overriding");
}

class FinalDemo{
public static void main(String[] args) {
A a=new A();
[Link]();
}

15. // This program will not compile.


class Outer {
int outer_x = 100;

void test() {
Inner inner=new Inner();
[Link]();
}
// this is an inner class
class Inner {
int y= 10; // y is local to Inner
void display() {
[Link]("display: outer_x = " + outer_x);
}

void showy() {
[Link](y); // error, y not known here!
}

class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
[Link]();
}

16. class Outer {


int outer_x = 100;

void test() {
Inner inner = new Inner();
[Link]();
}

// this is an inner class


class Inner {
int y = 10; // y is local to Inner
void display() {
[Link]("display: outer_x = " + outer_x);
}

}
void showy() {
Inner inner = new Inner();
[Link](inner.y); // fixed: create object of Inner
}

class InnerClassDemo1 {
public static void main(String args[]) {
Outer outer = new Outer();
[Link]();
[Link]();
}
}

17. // Demonstrate method overloading.


class OverloadDemo {
void test() {
[Link]("No parameters");
}

// Overload test for one integer parameter.


void test(int a) {
[Link]("a: " + a);
}

// Overload test for two integer parameters.


void test(int a, int b) {
[Link]("a and b: " + a + " " + b);
}

// Overload test for a double parameter


double test(double a) {
[Link]("double a: " + a);
return a*a;
}
}

class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
[Link]();
[Link](10);
[Link](10, 20);
result = [Link](123.25);
[Link]("Result of [Link](123.25): " + result);
}

18. class OverloadDemo {


void test(int a) {
[Link]("No parameters");
}

class Overload1 {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
// call all versions of test()
[Link](5);
}
}

19. /* Here, Box defines three constructors to initialize


the dimensions of a box various ways.
*/
class Box {
double width;
double height;
double depth;
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}

// compute and return volume


double volume() {
return width * height * depth;
}

class OverloadCons {
public static void main(String args[]) {
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = [Link]();
[Link]("Volume of mybox1 is " + vol);
// get volume of second box
vol = [Link]();
[Link]("Volume of mybox2 is " + vol);
// get volume of cube
vol = [Link]();
[Link]("Volume of mycube is " + vol);
}

}
20. // Here, Box allows one object to initialize another.
class Box {
double width;
double height;
double depth;
// Notice this constructor. It takes an object of type Box.
Box(Box ob) { // pass object to constructor
width = [Link];
height = [Link];
depth = [Link];
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}

// compute and return volume


double volume() {
return width * height * depth;
}

class OverloadCons2 {
public static void main(String args[]) {
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
Box myclone = new Box(mybox1); // create copy of mybox1
double vol;
// get volume of first box
vol = [Link]();
[Link]("Volume of mybox1 is " + vol);
// get volume of second box
vol = [Link]();
[Link]("Volume of mybox2 is " + vol);
// get volume of cube
vol = [Link]();
[Link]("Volume of cube is " + vol);
// get volume of clone
vol = [Link]();
[Link]("Volume of clone is " + vol);
}
}

21. final class A{


final int a=5;
final public void show(){
//a=4;
[Link]("Hi");
}

/*class B extends A{
void show(){
[Link]("Overriding");
}

}*/

class PackageDemo{
public static void main(String[] args) {
A a=new A();
[Link]();
}

}
22.// Objects are passed through their references. class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}

// pass an object
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}

class PassObjRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
[Link]("ob.a and ob.b before call: " + ob.a + " " + ob.b);
[Link](ob);
[Link]("ob.a and ob.b after call: " + ob.a + " " + ob.b);
}

23. class Pradeep{


public void Pradeep(){
[Link]("Pradeep");
Pradeep();
break;
}

public static void main(String args[]){


Pradeep();
}
}
24. public class RecursionExample1 {
static void p(){
[Link]("hello");
p();
}
public static void main(String[] args) {
p();
}
}

25. public class ThisEx{


int x;

// Constructor with a parameter


public ThisEx(int x) {
this.x = x;
}

// Call the constructor


public static void main(String[] args) {
ThisEx myObj = new ThisEx(5);
[Link]("Value of x = " + myObj.x);
}

You might also like