0% found this document useful (0 votes)
23 views10 pages

Ajp Exp5

The document contains 4 programs that demonstrate different aspects of generics in Java. Program 1 shows a generic Box class that can hold objects of any type. Program 2 demonstrates a generic MyList class with multiple type parameters. Program 3 shows methods and constructors in a generic Triple class. Program 4 demonstrates inheritance with generic Patient subclasses for inpatients and outpatients.
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)
23 views10 pages

Ajp Exp5

The document contains 4 programs that demonstrate different aspects of generics in Java. Program 1 shows a generic Box class that can hold objects of any type. Program 2 demonstrates a generic MyList class with multiple type parameters. Program 3 shows methods and constructors in a generic Triple class. Program 4 demonstrates inheritance with generic Patient subclasses for inpatients and outpatients.
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/ 10

EXPERIMENT 5

PROGRAM 1: Write a java program to demonstrate generic class

import java.util.*;

import java.io.*;

import javax.swing.*;

class Box<T> {

public T contents;

Box(T c){

contents =c;

public T what_is_inside(){

return contents;

public void put_something(T t){

contents =t;

class Exp5_1{
public static void main(String [] args){

Box <String> b1 = new Box<>("Books");

System.out.println("Initially the box contains :"+b1.what_is_inside());

b1.put_something("Weapons");

System.out.println("Now the Box contains "+b1.what_is_inside());

OUTPUT:

D:\Js-projects\Advanced java>java Exp5_1

Initially the box contains :Books

Now the Box contains Weapons

--------------------------------------------------------------------------------------------------------------------------------------

PROGRAM 2: Write a java program to demonstrate multiple type parameters in generic classes

import java.io.*;

import java.util.*;

class MyList<T> {

private ArrayList<T> list;


MyList() {

list = new ArrayList<>();

void add(T element) {

list.add(element);

T get(int index) {

return list.get(index);

int size() {

return list.size();

boolean contains(T element) {

return list.contains(element);

class Exp5_2{

public static void main(String [] args){

MyList<String> Fruits = new MyList<>();

Fruits.add("Mango");

Fruits.add("Custard apple");

Fruits.add("Banana");

Fruits.add("Apple");
System.out.println("Total number of the fruits :"+Fruits.size());

System.out.println("List of Fruits we have: ");

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

System.out.println(Fruits.get(i));

OUTPUT:

D:\Js-projects\Advanced java>java Exp5_2

Total number of the fruits :4

List of Fruits we have:

Mango

Custard apple

Banana

Apple

--------------------------------------------------------------------------------------------------------------------------------------

PROGRAM 3: Write a java program to demonstrate methods and constructors in generics

import java.util.*;

import java.io.*;

class Triple<A, B, C> {

A first;

B second;

C third;
Triple(A first, B second, C third) {

this.first = first;

this.second = second;

this.third = third;

A getFirst() {

return first;

B getSecond() {

return second;

C getThird() {

return third;

void setFirst(A first) {

this.first = first;

void setSecond(B second) {

this.second = second;

void setThird(C third) {

this.third = third;

String tostring() {
return "(" + first + ", " + second + ", " + third + ")";

public class Exp5_3 {

public static void main(String[] args) {

Triple<String, Integer, Boolean> myTriple = new Triple<>("Hemanth", 46, true);

System.out.println("myTriple = " + myTriple);

String first = myTriple.getFirst();

int second = myTriple.getSecond();

boolean third = myTriple.getThird();

System.out.println("The first element is a string: " + first);

System.out.println("The second element is an integer: " + second);

System.out.println("The third element is a boolean: " + third);

myTriple.setFirst("God Boy");

myTriple.setSecond(77);

myTriple.setThird(false);

System.out.println("myTriple after setting new values = " + myTriple.tostring());

OUTPUT:

D:\Js-projects\Advanced java>java Exp5_3

myTriple = Triple@452b3a41

The first element is a string: Hemanth

The second element is an integer: 46


The third element is a boolean: true

myTriple after setting new values = (God Boy, 77, false)

--------------------------------------------------------------------------------------------------------------------------------------

PROGRAM 4: Write a java program to demonstrate inheritances in generics

import java.util.*;

import java.io.*;

abstract class Patient<T> {

String name;

int age;

T treatment;

Patient(String name, int age, T treatment) {

this.name = name;

this.age = age;

this.treatment = treatment;

String getName() {

return name;

int getAge() {

return age;

T getTreatment() {

return treatment;

}
void setTreatment(T treatment) {

this.treatment = treatment;

abstract void printPatientInfo();

class InPatient<T> extends Patient<T> {

int roomNumber;

InPatient(String name, int age, T treatment, int roomNumber) {

super(name, age, treatment);

this.roomNumber = roomNumber;

int getRoomNumber() {

return roomNumber;

@Override

void printPatientInfo() {

System.out.println("Name: " + getName() + ", Age: " + getAge() + ", Treatment: " +
getTreatment() + ", Room Number: " + getRoomNumber());

class OutPatient<T> extends Patient<T> {

String appointmentTime;

OutPatient(String name, int age, T treatment, String appointmentTime) {


super(name, age, treatment);

this.appointmentTime = appointmentTime;

String getAppointmentTime() {

return appointmentTime;

void printPatientInfo() {

System.out.println("Name: " + getName() + ", Age: " + getAge() + ", Treatment: " +
getTreatment() + ", Appointment Time: " + getAppointmentTime());

class Exp5_4{

public static void main(String [] args){

InPatient<String> l1 = new InPatient<>("Uday",2,"OverUnderstanding",100);

l1.printPatientInfo();

OutPatient<String> o1 = new OutPatient<>("Jayasurya",1,"Unable to eat","26:00");

o1.printPatientInfo();

OUTPUT:
D:\Js-projects\Advanced java>java Exp5_4

Name: Uday, Age: 2, Treatment: OverUnderstanding, Room Number: 100

Name: Jayasurya, Age: 1, Treatment: Unable to eat, Appointment Time: 26:00

--------------------------------------------------------------------------------------------------------------------------------------

You might also like