Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

Object Oriented Programming using Java Laboratory (DJS23FLES201)

Academic Year 2023-24

EXPERIMENT NO. 1

ROLL NO. : I005 SAP ID: 60003230242

NAME: Aaryan Khanna BATCH: I1-1

AIM / OBJECTIVE:

To implement Java control statements, loops, and command line argument

PROGRAM/CODE:

Problem Statement 1:

Given an integer, n, perform the following conditional actions:

· If n is odd, print Weird

· If n is even and in the inclusive range of 2 to 5, print Not Weird

· If n is even and in the inclusive range of 6 to 20, print Weird

· If n is even and greater than 20, print Not Weird

Code:

import java.util.Scanner;

class Main {
public static void main(String args[]){
Scanner scanner= new Scanner(System.in);
System.out.println("Enter a number:");
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
int n = scanner.nextInt();
if(n%2!=0)

System.out.println("Weird");
else if(n>=2 && n<=5)
System.out.println("Not Weird");
else if(n>=20 && n<= 6)
System.out.println("Weird");
else if(n>20)
System.out.println("Not Weird");
}
}

OUTPUT:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24

Problem Statement 2:

WAP to find largest of 3 numbers using nested if else and nested ternary operator.

Code:

(a)

import java.util.Scanner;

class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.println("Enter three numbers:");
int a = s.nextInt();
int b = s.nextInt();
int c = s.nextInt();

if (a > b) {
if (a > c)
System.out.println("The greatest is " + a);
else
System.out.println("The greatest is " + c);
} else {
if (b > c)
System.out.println("The greatest is " + b);
else
System.out.println("The greatest is " + c);
}
}
}

(b)

import java.util.Scanner;

class Main{
public static void main(String args[]){
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Scanner s = new Scanner(System.in);
System.out.println("Enter three numbers:");
int a = s.nextInt();
int b = s.nextInt();
int c = s.nextInt();

int largest = (a>b)?(a>c)?a:c:(b>c)?b:c;


System.out.println("The greatest is " + largest);
}
}

Output:

(a)

(b)
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24

Problem Statement 3:

Write a Java program that reads a positive integer from command line and count
the number of digits the number (less than ten billion) has.

Code:

class Main{
public static void main(String args[]){
long n = Long.parseLong(args[0]);
int count = 0;
while(n!=0){
count++;
n/=10;
}

System.out.println("The number of digits is " + count);


}

Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24

Problem Statement 4:

Write a menu driven program using switch case to perform mathematical


operations.

Code:

import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.println("Enter two numbers:");
int a = s.nextInt();
int b = s.nextInt();
System.out.println("Enter the operation:");
System.out.println("1 for Addition");
System.out.println("2 for Subtraction");
System.out.println("3 for Multiplication");
System.out.println("4 for Division");
char choice = s.next().charAt(0);
String result = "The result of the operation is ";
switch(choice){
case '1':
result+= (a+b);
break;
case '2':
result += (a-b);
break;
case '3':
result += (a*b);
break;
case '4':
result += ((float)a/b);
break;
default:
result = "Invalid Input";
}
System.out.println(result);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}

Output:

Problem Statement 5:

WAP to find grade of student from input marks using if else ladder and switch case.

Code:

(a)

import java.util.Scanner;

class Main {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter marks:");
int marks = scanner.nextInt();
String output = "Your grade is ";
if(marks>100)
output = "Invalid marks";
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
else if(marks>=90)
output+= "A";
else if(marks>=80)
output += "B";
else if(marks>=70)
output += "C";
else if(marks>=60)
output+= "D";
else if(marks>=50)
output+= "E";
else
output+= "F";
System.out.println(output);
}
}

(b)

import java.util.Scanner;

class Main{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter marks:");
int marks = scanner.nextInt();
String output = "Your grade is ";
switch(marks/10){
case 10:
case 9:
output += "A";
break;
case 8:
output += "B";
break;
case 7:
output += "C";
break;
case 6:
output += "D";
break;
case 5:
output += "E";
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
break;
case 4:
case 3:
case 2:
case 1:
case 0:
output += "F";
break;
default:
output = "Invalid marks";
}
System.out.println(output);
}
}

Output :

(a)

(b)
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24

Problem Statement 6:

WAP to print the sum of following series 1+1/2^2+1/3^2+1/4^2……+1/n^2

Code:

import java.util.Scanner;

class Main{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter n:");
int n = scanner.nextInt();
double sum = 0;
for(int i = 1;i<=n;i++){
sum += 1.0/(i*i);
}
System.out.println("The value of the series is " + sum);
}
}

Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24

Problem Statement 7:

WAP to display the following patterns:

21

123

4321

12345

654321

1234567

CB

FED
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
JIHG

Code:

(a)

class Main{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int i,j;
for(i = 1;i<=n;i++){
if(i%2==0)
for(j = i;j>=1;j--)
System.out.print(j + " ");
else
for(j = 1;j<=i;j++)
System.out.print(j + " ");
System.out.println();
}
}
}
(b)

class Main{

public static void main(String args[]){


int n = Integer.parseInt(args[0]);
int i,j;
char c = 'A';
for(i = 1;i<=n;i++){
for(j = 1;j<=n-i;j++)
System.out.print(" ");
for(j = 1;j<=i;j++)
System.out.print((char)(c-j+1));

c=(char)((i+1)*(i+2)/2+64);
System.out.println();
}
}
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Output:

(a)

(b)

CONCLUSION:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
We have successfully studied and implemented java control statement, loops and command
line arguments

Website References: (Give references to the cited material)

EXPERIMENT NO. 2

ROLL NO. : I005 SAP ID: 60003230242

NAME: Aaryan Khanna BATCH: I1-1

Aim/ Objective :

To implement Arrays

Program/ Code:

Problem Statement 1:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
You have been given an array of positive integers A1, A2,...,An with length N and
you have to print an array of same length (N) where the values in the new array are
the sum of every number in the array, except the number at that index.

i/p 1 2 3 4

For the 0th index, the result will be 2+3+4= 9, similarly for the second, third and
fourth index the corresponding results will be 8, 7 and 6 respectively.

i/p 4 5 6

o/p 11 10 9

Code:

import java.util.Scanner;

class Main{

public static void main(String args[]){

Scanner s = new Scanner(System.in);

System.out.print("Enter the length of array:");

int nums[] = new int[s.nextInt()];

System.out.println("Enter the elements:");

int sum = 0;

for(int i = 0;i<nums.length;i++){

nums[i] = s.nextInt();
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
sum += nums[i];

int sumarray[] = new int[nums.length];

for(int i = 0;i<nums.length;i++)

sumarray[i] = sum-nums[i];

System.out.println("The output Array:");

for(int n : sumarray)

System.out.print(n + " ");

System.out.println();

Output:

Problem Statement 2:

The annual examination results of 5 students are tabulated as follows:


Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24

WAP to read the data and determine the following

Total marks obtained by each student

The student who obtained the highest total marks

Code:

import java.util.Scanner;

class Main{

public static void main(String args[]){

Scanner s = new Scanner(System.in);

int size = 5;

int highest = 0;

int rollno[] = new int[size];

int sub1[] = new int[size];

int sub2[] = new int[size];


Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
int sub3[] = new int[size];

int total[] = new int[size];

for(int i = 0;i<size;i++){

System.out.println("Enter Rollno and marks of Subject1, Subject2,


Subject3:");

rollno[i]=s.nextInt();

sub1[i]=s.nextInt();

sub2[i]=s.nextInt();

sub3[i]=s.nextInt();

for(int i = 0;i<size;i++){

total[i] = sub1[i]+sub2[i]+sub3[i];

if(total[i]>total[highest])

highest = i;

System.out.println("\nData entered");

System.out.println("Rollno\tSub1\tSub2\tSub3\tTotal");

for(int i = 0;i<size;i++)

System.out.println(rollno[i] + "\t" + sub1[i] + "\t" + sub2[i] + "\t" +


sub3[i] + "\t" + total[i]);

System.out.println("Rollno of student with highest total marks is " +


rollno[highest]);

System.out.println("Highest total marks " + total[highest]);


Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}}

Output:

Problem Statement 3:

WAP to display following pattern using irregular arrays (jagged arrays).

12

123…

Code:

import java.util.Scanner;

class Main{

public static void main(String args[]){


Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number if rows:");

int row = scanner.nextInt();

int arr[][] = new int[row][];

for(int i = 1;i<=row;i++){

arr[i-1] = new int[i];

for(int j = 1;j<=i;j++){

arr[i-1][j-1] = j;

for(int i = 0;i<row;i++){

for(int j = 0;j<=i;j++){

System.out.print(arr[i][j]+" ");

System.out.println();

}}

Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24

CONCLUSION:

We have successfully studied and implemented concept of java arrays and jagged arrays.

Website References: (Give references to the cited material)

EXPERIMENT NO. 3

ROLL NO. : I005 SAP ID: 60003230242

NAME: Aaryan Khanna BATCH: I1-1

AIM / OBJECTIVE:

To implement Strings
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
PROGRAM/CODE:

Problem Statement 1:

WAP to find out number of uppercase & lowercase characters, blank spaces and
digits from the string

Code:

import java.util.Scanner;

class Main{

public static void main(String args[]){

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string:");

String s = scanner.nextLine();

int lower = 0,upper = 0,blank = 0,digits=0;

for(int i = 0;i<s.length();i++){

char c = s.charAt(i);

if('A'<=c && c<='Z')

upper++;

else if('a'<=c && c<='z')

lower++;

else if('0'<=c && c<= '9')

digits++;

else if(c==' ')

blank++;
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}

System.out.println("No. of upper case char: " + upper);

System.out.println("No. of lower case char: " + lower);

System.out.println("No. of digits: " + digits);

System.out.println("No. of blank spaces: " + blank);

Output:

Problem Statement 2:

WAP to count the frequency of occurrence of a given character in a given line of


text

Code:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
import java.util.Scanner;

class Main{

public static void main(String args[]){

System.out.print("Enter a string:");

Scanner scanner = new Scanner(System.in);

String s = scanner.nextLine();

System.out.print("Enter the search character:");

char c = scanner.next().charAt(0);

int count = 0, i = 0;

while(s.indexOf(c,i) != -1){

count++;

i = s.indexOf(c,i)+1;

System.out.println("The frequency of the character is " + count);

Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24

Problem Statement 3:

WAP to check if a string is a palindrome or not using inbuild functions.

Code:

import java.util.Scanner;

class Main{

public static void main(String args[]){

System.out.print("Enter a string:");

Scanner scanner = new Scanner(System.in);

String word = scanner.next();

StringBuffer sb = new StringBuffer(word);

sb.reverse();

if(word.equalsIgnoreCase(sb.toString()))

System.out.println("The word is a palindrome");

else

System.out.println("The word is not a palindrome");

Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24

CONCLUSION:

We have successfully studied and implemented concept of java Strings and its
inbuilt functions

Website References: (Give references to the cited material)

EXPERIMENT NO. 4
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
ROLL NO. : I005 SAP ID: 60003230242

NAME: Aaryan Khanna BATCH: I1-1

AIM / OBJECTIVE:

To implement collections (Array List/ Vectors)

PROGRAM/CODE:

Problem Statement 1:

WAP to accept students name from command line and store them in vector.

Code:

import java.util.*;

class Main{

public static void main(String args[]){

Vector<String> names = new Vector<String>();

for(String name : args){

names.addElement(name);

System.out.println("Entered names:");

Enumeration en = names.elements();

while(en.hasMoreElements()){

System.out.println(en.nextElement());

}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}

Output:

Problem Statement 2:

WAP to add n strings in a vector array. Input new string and check if it is present
in the vector. If present delete it else add to the vector

Code:

import java.util.*;

class Main{

public static void main(String args[]){

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements:");

int length = scanner.nextInt();

scanner.nextLine();

Vector<String> strings = new Vector<String>();

System.out.println("Enter strings:");

for(int i = 0;i<length;i++){

strings.addElement(scanner.nextLine());
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}

System.out.print("Enter the string to be added/removed:");

String s = scanner.nextLine();

if(strings.contains(s)){

int firstindex = strings.indexOf(s);

strings.removeElementAt(firstindex);

while(strings.contains(s)){

strings.removeElementAt(strings.indexOf(s));

else{

strings.addElement(s);

System.out.println("Updated set:");

Enumeration en = strings.elements();

while(en.hasMoreElements()){

System.out.println(en.nextElement());

}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Output:

CONCLUSION:

We have successfully studied and implemented concept of java collections and


its use cases such as Array List and Vector

Website References: (Give references to the cited material)

EXPERIMENT NO. 5
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
ROLL NO. : I005 SAP ID: 60003230242

NAME: Aaryan Khanna BATCH: I1-1

AIM / OBJECTIVE:

To implement class with members and methods (static, non-static, recursive and
overloaded methods)

PROGRAM/CODE:

Problem Statement 1:

Create a class employee with data member’s empid, empname, designation and
salary. Write methods getemployee() to take user input, showgrade() to display
grade of employee based on salary, showemployee() to display details of
employee.

Code:

import java.util.Scanner;

class Main{

public static void main(String args[]){

Employee em = new Employee();

em.getEmployee();

em.showEmployee();

em.showGrade();

class Employee{
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
private String empid;

private float salary;

private String designation;

private String empname;

public void getEmployee(){

System.out.println("Enter name, ID, designation and salary of employee:");

Scanner s = new Scanner(System.in);

empname = s.nextLine();

empid = s.nextLine();

designation = s.nextLine();

salary = s.nextFloat();

public void showGrade(){

if(salary>500000)

System.out.println("Grade: A");

else if(salary>100000)

System.out.println("Grade: B");

else

System.out.println("Grade: C");

public void showEmployee(){

System.out.println("Employee details:");
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
System.out.println("Name: " + empname);

System.out.println("ID: " + empid);

System.out.println("Designation: " + designation);

System.out.println("Salary: " + salary);

Output:

Problem Statement 2:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
WAP to display area of square and rectangle using the concept of overloaded
functions

Code:

import java.util.Scanner;

class Main{

public static void main(String args[]){

Scanner s = new Scanner(System.in);

System.out.println("Enter length of square and length and breadth of


rectangle:");

float len = s.nextFloat();

float rlen = s.nextFloat();

float rbre = s.nextFloat();

displayArea(len);

displayArea(rlen,rbre);

static void displayArea(float len){

System.out.println("Area of square is " + len*len);

static void displayArea(float len, float breadth){

System.out.println("Area of rectangle is " + len*breadth);

}}

Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24

Problem Statement 3:

WAP to find value of y using recursive function (static), where y=x^n

Code:

import java.util.Scanner;

class Main{

public static void main(String args[]){

Scanner s = new Scanner(System.in);

System.out.print("Enter x and n:");

float x = s.nextFloat();

int n = s.nextInt();

System.out.println("The value of x^n is " + pow(x,n));

static float pow(float x,int y){

if(y==0)

return 1;
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
return x*pow(x,y-1);

Output:

Problem Statement 4:

WAP to count the number of objects made of a particular class using static
variable and static method and display the same.

Code:

class Main{

public static void main(String args[]){

StringWrapper strings[] = new StringWrapper[args.length];

for(int i = 0;i<args.length;i++){

strings[i] = new StringWrapper(args[i]);

strings[i].count();

}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
StringWrapper.showCount();

System.out.println("Value of StringWrappers:");

for(StringWrapper s : strings){

System.out.println(s);

class StringWrapper{

private static int count = 0;

private String string;

StringWrapper(String s){

string = s;

public static void showCount(){

System.out.println("Number of StringWrapper classes: " + count);

public String toString(){

return string;

public static void count(){

count++;

}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}

Output:

CONCLUSION:

We have successfully studied and implemented concept of java classes and


methods (static and non-static)

Website References: (Give references to the cited material)

EXPERIMENT NO. 6
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
ROLL NO. : I005 SAP ID: 60003230242

NAME: Aaryan Khanna BATCH: I1-1

AIM / OBJECTIVE:

To implement array of objects and passing/ returning objects

PROGRAM/CODE:

Problem Statement 1:

WOOP to arrange the names of students in descending order of their total marks,
input data consists of students details such as names, ID.no, marks of maths,
physics, chemistry. (Use array of objects)

Code:

import java.util.Scanner;

class Main {

public static void main(String args[]) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of students: ");

Student students[] = new Student[scanner.nextInt()];

for(int i = 0;i<students.length;i++){

students[i] = new Student();

students[i].input();

for(int i =0;i<students.length-1;i++){
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
for(int j = 0;j<students.length-1;j++){

if(students[j].getTotal()<students[j+1].getTotal()){

Student temp = students[j];

students[j] = students[j+1];

students[j+1] = temp;

System.out.println("Data Set:");

Student.printFormat();

for(Student student : students){

System.out.println(student);

class Student {

private String name;

private int idno;

private int maths, phy, chem;

private int total;

public int getTotal() {

return total;
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
}

public void input() {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter name:");

name = scanner.nextLine();

System.out.print("Enter ID no:");

idno = scanner.nextInt();

System.out.print("Enter physics marks:");

phy = scanner.nextInt();

System.out.print("Enter maths marks:");

maths = scanner.nextInt();

System.out.print("Enter chemistry marks:");

chem = scanner.nextInt();

total = phy + chem + maths;

scanner.nextLine();

System.out.println();

public static void printFormat() {

System.out.println("Name\tID\tPhy\tChem\tMaths\tTotal");

public String toString() {

String s = name + "\t" + idno + "\t" + phy + "\t" + chem + "\t" + maths + "\t" + total;
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
return s;

Output:

Problem Statement 2:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
WAP to perform mathematical operations on 2 complex numbers by passing and
returning object as argument. Show the use of this pointer.

Code:

import java.util.Scanner;

public class Main {

public static void main(String args[]) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter x,y for first number:");

Complex a = new Complex(scanner.nextFloat(), scanner.nextFloat());

System.out.println("Enter x,y for second number:");

Complex b = new Complex(scanner.nextFloat(), scanner.nextFloat());

System.out.println("Enter the operation:");

System.out.println("1 for Addition");

System.out.println("2 for Subtraction");

System.out.println("3 for Multiplication");

System.out.println("4 for Division");

switch (scanner.next().charAt(0)) {

case '1':

System.out.print("Addition: ");

System.out.println(a.add(b));
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
break;

case '2':

System.out.print("Subraction: ");

System.out.println(a.sub(b));

break;

case '3':

System.out.print("Multiplition: ");

System.out.println(a.multiply(b));

break;

case '4':

System.out.print("Division: ");

System.out.println(a.divide(b));

break;

default:

System.out.println("Invalid Input");

break;

scanner.close();

}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
class Complex {

private float x;

private float y;

Complex(float real, float img) {

this.x = real;

this.y = img;

public float getReal() {

return this.x;

public float getImaginary() {

return this.y;

public Complex add(Complex op) {

float x = this.x + op.x;

float y = this.y + op.y;

return new Complex(x, y);

public Complex sub(Complex op) {

float x = this.x - op.x;


Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
float y = this.y - op.y;

return new Complex(x, y);

public Complex multiply(Complex op) {

float x = this.x * op.x - this.y * op.y;

float y = this.x * op.y + this.y * op.x;

return new Complex(x, y);

public Complex divide(Complex op) {

float x = this.x * op.x + this.y * op.y;

float y = this.y * op.x - this.x * op.y;

x /= op.x * op.x + op.y * op.y;

y /= op.x * op.x + op.y * op.y;

return new Complex(x, y);

public Complex conjugate() {

return new Complex(x, -y);

public String toString() {

String temp;
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
if (this.y < 0) {

temp = this.x + " - " + -this.y + "i";

} else {

temp = this.x + " + " + this.y + "i";

return temp;

Output:

CONCLUSION:

We have successfully studied and implemented concept of array of objects in java

EXPERIMENT NO. 7
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
ROLL NO. : I005 SAP ID: 60003230242

NAME: Aaryan Khanna BATCH: I1-1

AIM / OBJECTIVE:

To implement Constructors and constructor overloading

PROGRAM/CODE:

Problem Statement 1:

WAOOP to count the no. of objects created of a class using constructors.

Code:

import java.util.Scanner;

class Main{

public static void main(String args[]){

Count c1 = new Count();

Count c2 = new Count();

System.out.println("Count: " + Count.count);

Count c3 = new Count();

Count c4 = new Count();

System.out.println("Count: " + Count.count);

class Count{

static int count = 0;


Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Count(){

count++;

Output:

Problem Statement 2:

WAP to display area of square and rectangle using the concept of overloaded
constructor (use parameterized, non-parameterized and copy constructor)

Code:

import java.util.Scanner;

class Main{

public static void main(String args[]){

Scanner scanner = new Scanner(System.in);

System.out.println("Enter length:");

Shape square = new Shape(scanner.nextDouble());

System.out.println("Enter length and breadth of rectangle:");

Shape rect = new Shape(scanner.nextDouble(),scanner.nextDouble());


Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
Shape copysquare = new Shape();

copysquare = new Shape(square);

System.out.println(square);

System.out.println(rect);

System.out.println("Square copy:");

System.out.println(copysquare);

class Shape{

private double length,breadth,area;

Shape(){}

Shape(Shape copy){

this.length = copy.length;

this.breadth = copy.breadth;

this.area = copy.area;

Shape(double length){

this.length = length;

area = this.length * this.length;

Shape(double length,double breadth){

this.length = length;
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2023-24
this.breadth = breadth;

area = this.length * this.breadth;

public String toString(){

return breadth==0?"Area of square is " + area:"Area of rectangle is " + area;

Output:

CONCLUSION:

We have successfully studied and implemented the concept of constructor and constructor
overloading

Website References: (Give references to the cited material)

You might also like