Labsheet 2.2 CSE1006-Problem Solving Using JAVA Module-2
Labsheet 2.2 CSE1006-Problem Solving Using JAVA Module-2
class Student {
String name;
int rollNo;
double cgpa;
Student() // default constructor
{
}
Student(String n, int r, double c) { // constructor with parameters
name = n;
rollNo = r;
cgpa = c;
}
==================================================================================
import java.util.Scanner;
public class Employee{
String name;
int empid;
double salary,tax;
void volume()
{
double vol=width * height * depth;
System.out.println("volume is "+vol);
}
public static void main(String args[]) {
Test Case:
F:\JavaPgms>java Box
volume is 125.0
volume is 24.0
// For the parameterized constructor, we can also give same name to input parameters (same as
instance variable names) in that case to access instance variables we use this key word (shown
below)
Box(double width,double height, double depth) // three parameter constructor
{
this.width= width;
this.height= height;
this.depth= depth;
}
Note: If you omit the keyword this in the above example, the output would be "0"
this key word in java:
The this keyword in Java is a reference variable that refers to the current object of a class.
Note: The most common use of the this keyword is to eliminate the confusion between class
attributes and parameters with the same name (because a class attribute is shadowed by a
method or constructor parameters).
Example:
s1.getdata(); // here s1 is current object, inside of getdata() method this references to s1
s2.display();// here s2 is current object, inside of display() method this references to s2
Activity 2: Modify the above (Box class) program using Copy constructor to apply Constructor
overloading.
4. An interviewer asked a job seeker to find the biggest of two integer numbers and also biggest of
three integer numbers by applying overloading and static context in the program and finally display
the output. Think you as a job seeker and solve the problem by satisfying the requirement given by
the interviewer.
class Biggest {
// Method to find the biggest of two numbers
static int findBiggest(int a, int b) {
return (a > b) ? a : b;
}
System.out.println("Biggest of " + num1 + " and " + num2 + " is: " +Biggest.findBiggest(num1,
num2));
System.out.println("Biggest of " + num1 + ", " + num2 + " and " + num3 + " is: " +
Biggest.findBiggest(num1, num2, num3));
}
}
Test Case:
F:\JavaPgms>java Biggest
Biggest of 10 and 20 is: 20
Biggest of 10, 20 and 35 is: 35
==================================================================================
1. Basic Implementation:
A Company keeps track of the total number of employees using a static variable.
Ensure that every time a new Employee object is created, the count increases.
2. Multiple Object Creation:
If you create three instances of the Employee class in the main method, what will be
the value of the employee count? Explain how the static variable behaves.
3. Static Method Access:
Modify the Company Name by including Land mark and city name using static
method. How can this method be accessed from the main method without using an
object?
Activity 3 : Students have to execute the below program and analyse the importance of static block
and how static block executes automatically
public class StaticBlock {
// static block
static{
System.out.println("Hello this is a static block");
}
public static void main(String args[])
{
System.out.println("This is main method");
}}
Activity 4: Students should analyse the logic for below program and develop java code.
Problem:
Assume our Presidency University uses a common water tank to supply water to every block. Each
time the water level of the tank reduces based on the usage of each block. The facility manger fills
the tank thrice with 500 litres each time when request arrives. D block uses 200litres, E Block uses
300 litres and so on. Display the current water level in the tank each time when the blocks are raised
their usage and decide when to call the facility manager to fill the tank.
Activity 5 : Students should analyse the logic for below problem and develop java code.
Develop a java program with two classes an Outer and Inner class. Each of the class have one data
member, the inner class has member function which displays the data members. Demonstrate
program to access of both inner and outer class members.
==================================================================================