0% found this document useful (0 votes)
6 views5 pages

Experiment 6 24

The document outlines a Java laboratory experiment focused on Object Oriented Programming, specifically implementing an array of objects for student records and performing mathematical operations on complex numbers. It includes code examples for arranging student names based on total marks and for adding, subtracting, and multiplying complex numbers using methods that return new objects. The document serves as a practical guide for students to understand object manipulation in Java.

Uploaded by

Mohammad Shaikh
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)
6 views5 pages

Experiment 6 24

The document outlines a Java laboratory experiment focused on Object Oriented Programming, specifically implementing an array of objects for student records and performing mathematical operations on complex numbers. It includes code examples for arranging student names based on total marks and for adding, subtracting, and multiplying complex numbers using methods that return new objects. The document serves as a practical guide for students to understand object manipulation in Java.

Uploaded by

Mohammad Shaikh
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
You are on page 1/ 5

Object Oriented Programming using Java Laboratory (DJS23FLES201)

Academic Year 2024-25

Name : Mohammad Shaikh


SAP ID : 60003240302
Batch: I3-3
RollNo.: I169
Java Experiment 6

6. To implement array of objects and passing/ returning objects


a. 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.*; class


Student

{
int IdNo,phy,chem,maths,n,total; String name;
public Student(int id,String n,int m,int p,int c,int t)
{
IdNo=id;
name=n;
maths=m;
phy=p; chem=c;
total=t;

}
public static void main(String args[])
{
int IdNo,phy,chem,maths,n,total;
String name;
Student s[];
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of Students:");
n=sc.nextInt();
s=new Student[n];
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

System.out.println("Enter Records of "+n+" student");


for(int i=0;i<s.length;i++)
{
System.out.println("Enter Record of "+(i+1)+" Student:");
System.out.println("Enter ID Number of Student: ");
IdNo=sc.nextInt();
System.out.println("Enter name of Student: ");
name=sc.next();

System.out.println("Enter Marks of (Maths, Physics,Chemistry): ");


maths=sc.nextInt(); phy=sc.nextInt(); chem=sc.nextInt();
total=phy+chem+maths; s[i]=new
Student(IdNo,name,maths,phy,chem,total);

}
System.out.println("Record of the Students as per Descending order of total marks: ");
for(int i=0;i<s.length-1;i++)

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

{ if(s[j].total <
s[j+1].total)

{
Student temp;
temp=s[j]; s[j]=s[j+1];
s[j+1]=temp;

}
}
}

System.out.println("ID Number\tName\tMaths\tPhysics\tChemistry\tTotalMarks");
for(int i=0;i<s.length;i++)

{
System.out.print(s[i].IdNo+"\t\t"+s[i].name+"\t"+s[i].maths+"\t"+s[i].phy+"\t"+s[i].chem+"\t\t"+s[i].t
otal);
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

System.out.println();
}
}}

Output:

b. WAP to perform mathematical operations on 2 complex numbers by passing and returning


object as argument. Show the use of this pointer.
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

Code:

class Complex {
double real;
double imaginary;

public Complex(double real, double imaginary) {


this.real = real; this.imaginary = imaginary;
}

public Complex add(Complex other) { double resultReal


= this.real + other.real; double resultImaginary =
this.imaginary + other.imaginary; return new
Complex(resultReal, resultImaginary);
}

public Complex subtract(Complex other) { double


resultReal = this.real - other.real; double resultImaginary =
this.imaginary - other.imaginary; return new
Complex(resultReal, resultImaginary);
}

public Complex multiply(Complex other) { double resultReal = this.real *


other.real - this.imaginary * other.imaginary; double resultImaginary = this.real *
other.imaginary + this.imaginary * other.real; return new Complex(resultReal,
resultImaginary);
}

public void display() {


System.out.println(this.real + " + " + this.imaginary + "i");
}
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

public class ComplexOperations { public static


void main(String[] args) { Complex complex1
= new Complex(5.0, 9.0);
Complex complex2 = new Complex(8.0, 3.0);

Complex sum = complex1.add(complex2);


Complex difference = complex1.subtract(complex2);
Complex product = complex1.multiply(complex2);

System.out.println("Sum:");
sum.display();

System.out.println("Difference:");
difference.display();

System.out.println("Product:");
product.display();
}
}
Output:

You might also like