6/30/2021
JAVA
PAATHSHALA
2021
Assignment/
Questions
Mentor: Kartikeya Khanna
TOPIC: CLASSES, OBJECTS AND METHODS
1. Define a class Calculator in Java with the following
description:
Data members:
digit1 of type int
digit2 of type int
Methods:
1. A constructor which takes the values from main method
and assign the values to respective data members.
2. A method named add() which adds the values and
returns the main to main.
3. A method named sub() which subtract the values and
returns the result to main.
4. A method named pro() which multiply the values and
returns the result to main.
5. A method named div() which divide the values and
returns the result to main.
2. Find the output of the following code:
class Output {
int number;
Output(int number) {
this.number = number;
}
void one() {
int i, res = 1;
1|
for (i = 1; i <= number; i++) {
res = res * i;
}
System.out.println(+res);
}
void two() {
int n1 = 0, n2 = 1, n3, i;
System.out.print(n1 + " " + n2);
for (i = 2; i < number; ++i) {
n3 = n1 + n2;
System.out.print(" " + n3);
n1 = n2;
n2 = n3;
}
}
void three(int a, int d) {
int curr_term;
curr_term = a;
for (int i = 1; i <= number; i++) {
System.out.print(curr_term + " ");
curr_term = curr_term + d;
}
2|
System.out.println();
}
}
public class MainClass {
public static void main(String[] args) {
Output obj = new Output(10);
obj.three(4, 8);
obj.one();
obj.two();
}
}
3. Define a class Ele_Bill in Java with the following
descriptions:
Data members:
Cname of type character array
Pnumber of type long
No_of_units of type integer
Amount of type float.
Amount can be calculated according to the following conditions:
No_of_units Cost
First 50 units Free
Next 100 units 0.80 @ unit
3|
Next 200 units 1.00 @ unit
Remaining units 1.20 @ unit
Methods:
1. A constructor which takes the values from main method and
assign the values to respective data members.
2. Calc_Amount( ) This method should calculate the
amount as No_of_units*Cost .
3. A method Display( ) to display the values of all the data
members
on the screen.
Take the values of Cname,Pnumber, No_of_units at the runtime.
4. Find the output of the following code:
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x= " + x);
System.out.println("a= " + a);
System.out.println("b= " + b);
}
static {
System.out.println("Static block initialized.");
4|
b = a * 4;
}
}
public class MainClass {
public static void main(String args[]) {
UseStatic.meth(42);
}
}
5. Find the output of the following code:
class Output {
void one() {
System.out.print("Paathshala ");
this.three();
}
void two() {
System.out.print("Java ");
this.one();
}
void three() {
System.out.print("2021");
}
}
5|
public class MainClass {
public static void main(String[] args) {
Output obj = new Output();
for (int i = 0; i < 5; i++) {
obj.two();
System.out.println();
}
}
}
6. Find the output of the following code:
class MainClass {
public static void main(String[] args)
{
int x = 20;
System.out.println(x);
}
static
{
int x = 10;
System.out.print(x + " ");
}
}
6|