Aim:
To define a class, describe its constructor overload and instantiate its object.
Software Requirements:
Component Description
Programming Language Java
Compiler/Interpreter Java Development Kit (Oracle)
IDE (optional) Notepad ++
Operating System Windows
Theory:
A class is a blueprint for creating objects, encapsulating data and methods.
An object is an instance of a class, created with the new keyword.
A constructor is a special method with the same name as the class that initializes
object data automatically when an object is created.
A default constructor takes no parameters and assigns default values to instance
variables.
A method defines behaviors; here, the display() method prints object details.
Encapsulation groups related variables and methods into a class for better
organization and data protection.
The main() method is the starting point of the program, where objects are created
and methods invoked.
This program demonstrates these concepts by creating a student object with
default details and displaying them.
Algorithm for Displaying Student Details Using Constructor
1. Start
2. Define a class named ConstructorClass.
o Declare instance variables: rollno (int), name (String), and dept (String).
3. Create a default constructor inside ConstructorClass.
o Initialize rollno with 24.
o Initialize name with "john".
o Initialize dept with "DCN".
4. Define a method display() inside ConstructorClass.
o Print the values of rollno, name, and dept.
5. Define the main class mainclass.
o Inside the main() method:
Create an object io of type ConstructorClass.
Call the display() method on the object io.
6. End
Code:
class ConstructorClass
{
int rollno;
String name;
String dept;
public ConstructorClass()
{
rollno = 24;
name = "john";
dept = "DCN";
}
public ConstructorClass(int rollno, String name, String dept)
{
this.rollno = rollno;
this.name = name;
this.dept = dept;
}
public void display()
{
System.out.println("rollno:" + rollno);
System.out.println("name:" + name);
System.out.println("dept:" + dept);
}
}
public class mainclass
{
public static void main(String [] args)
{
ConstructorClass io = new ConstructorClass();
ConstructorClass ic = new ConstructorClass(45, "Alice", "CSE");
System.out.println("ConstructorClass");
io.display();
System.out.println("Overloaded ConstructorClass");
ic.display();
}
}
Sample output:
Result:
Thus we have successfully define a class, defined its conductor, overloaded the
constructor and instantiated its object.
Aim
To define a class, define instance method for setting and retrieving values of instance
variable and instantiate its object
Software Requirements:
Component Description
Programming Language Java
Compiler/Interpreter Java Development Kit (Oracle)
IDE (optional) Notepad ++
Operating System Windows
Algorithm:
Start
Import the Scanner class to read input from the user.
Define a class named MyClass.
Declare an integer instance variable value.
Define a method setValue(int v) to assign the input v to value.
Define a method getValue() to return the current value of value.
Define the main class named main.
Inside the main method:
o Create a Scanner object to take input from the user.
o Display the message "Enter a number: " to prompt the user.
o Read an integer from the user and store it in variable num.
o Create an object obj of class MyClass.
o Use obj.setValue(num) to set the value inside the object.
o Print the message "Your value is: " followed by obj.getValue() to display the
stored value.
End
Code:
import java.util.Scanner;
class MyClass
{
int value;
void setValue(int v)
{
value = v;
}
int getValue()
{
return value;
}
}
public class main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = input.nextInt();
MyClass obj = new MyClass();
obj.setValue(num);
System.out.println("Your value is: " + obj.getValue());
}
}
Sample output:
Result:
Thus we have successfully defined a class, defined instance methods for setting and
retrived values of instances variables and instantiated its object.