0% found this document useful (0 votes)
17 views7 pages

Java OOP Lab Report: ArrayList & Inheritance

Uploaded by

tabassumnoshin9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Java OOP Lab Report: ArrayList & Inheritance

Uploaded by

tabassumnoshin9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Heaven’s Light Is Our Guide

Rajshahi University of Engineering &


Technology

Lab Report

Course Name : Object Oriented Programming Sessional

Course Code : CSE 1204

Lab No. : 08

Date of Lab :

Date of Submission:

Submitted By: Submitted to:


Name:Nosin Tabassum Dr. Md. Shahid Uz Zaman
Roll: 2303079 Professor ,
Section: B Computer Science and Engineering,
Series: 23 RUET
Topic 1: [ArrayList class]:
Create an object of ArrayLIst class and implement the following methods:
i) add()
ii) get()
iii) set()
iv) remove()
v) size()
vi) toString()
vii) sort()
viii) clear()

Code:
import [Link];
import [Link];
import [Link];

public class topic1 {


public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
Scanner sc = new Scanner([Link]);

[Link]("Enter 5 fruits:");
for (int i = 0; i < 5; i++) {
String fruit = [Link]();
[Link](fruit);
}

[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");

[Link]("Total fruits:");
for (int i = 0; i < [Link](); i++) {
[Link]("Element at index " + i + ": " + [Link](i));
}

[Link](6, "Jackfruits");
[Link]("Apple");

[Link]("Size: " + [Link]());


[Link]("List: " + list);

[Link](list);
[Link]("Sorted list: " + list);

[Link]();
[Link]("Cleared list");

[Link]();
}
}

Output:

Explanation:
This Java program takes 5 fruit names from the user and stores them in an ArrayList. It
then adds three more fruits ("Apple", "Banana", and "Cherry") to the list. After that, it
prints all the fruits with their index positions. The program updates the fruit at index 6 to
"Jackfruits" and removes the first occurrence of "Apple" from the list. It displays the list
size and contents, then sorts the list alphabetically and prints the sorted version. Finally,
it clears the entire list and closes the scanner used for input.
Topic 2:[Anonymous Object]:
Write a method MidPoint() that accepts two Point objects and return their mid-point
anonymously .

Code:
import [Link];

public class topic2 {


public static Point MidPoint(Point p1, Point p2) {
return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
}

public static void main(String[] args) {


[Link]("Midpoint is: " + MidPoint(new Point(2, 4), new Point(6, 8)));
}
}

Output:

Explanation:
The MidPoint() method takes two Point objects as input and calculates the midpoint by
averaging their x and y coordinates. It returns a new Point object representing this
midpoint without storing the input points in variables—these are passed anonymously
when calling the method. In the example, two Point objects with coordinates (2, 4) and
(6, 8) are created and passed directly to MidPoint(), which returns their midpoint (4, 6),
and this result is printed.

Topic 3: [inner class]:


Suppose B is the inner class of A and ax and bx are the private data members of A and
B respectively. Now write a method to sum ax and bx using the object of class B.

Code:
public class A {
private int ax;

public A(int ax) {


[Link] = ax;
}

public class B {
private int bx;

public B(int bx) {


[Link] = bx;
}

public int sum() {


return ax + bx;
}
}

public static void main(String[] args) {


A a = new A(10);
A.B b = [Link] B(20);

[Link]("Sum of ax and bx: " + [Link]());


}
}

Output:

Explanation:
Class A has a private member ax. Inside it, class B is defined with its own private
member bx. The inner class method sum() accesses both ax (from outer class A) and
bx (its own) and returns their sum. In main(), we create an object of A and then create
an inner class B object tied to that A object. Finally, we call sum() on B to get the total of
ax + bx.

Topic 4[inheritance: use of super keyword]


Problem Statement: For the following Java program write
i) to display x of class A in class B
ii) statement to call getX() of class A in class B
iii) to call parameterized constructor of in class B
class A{
int x;
public A(){
x=0;
}

Code:
class A {
int x;

public A() {
x = 0;
}

public A(int x) {
this.x = x;
}

public int getX() {


return (x + 10);
}
}

class B extends A {
int x = 20;

public B() {
super(30);
}

public void displaySuperX() {


[Link]("x of class A: " + super.x);
}

public int callSuperGetX() {


return [Link]();
}

public int getX() {


return (x + 10);
}
}

public class A1 {
public static void main(String[] args) {
B b = new B();
[Link]();
[Link]([Link]());
[Link]([Link]());
}
}

Output:

Explanation:
This program has two classes, A and B, where B extends A. Class A has a variable x
and two constructors: one without parameters that sets x to 0, and one with a parameter
to set x to a given value. It also has a method getX() that returns x + 10. Class B has its
own x variable set to 20 and a constructor that calls A’s parameterized constructor to
set A’s x to 30. In B, the method displaySuperX() prints the value of x from class A using
super.x, and callSuperGetX() calls A’s getX() method using [Link](). B also
overrides getX() to return its own x + 10. In the main method, an object of B is created,
and these methods are called to show how super accesses members of the parent
class.

You might also like