0% found this document useful (0 votes)
13 views4 pages

Java Generic Class Example: Cell

The document presents a Java implementation of a generic class named 'Cell' that can hold two values of different types. It includes methods to set and retrieve these values, as well as a 'toString' method for representation. Additionally, a 'CellDriver' class demonstrates the usage of the 'Cell' class with examples of integer and string types.
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)
13 views4 pages

Java Generic Class Example: Cell

The document presents a Java implementation of a generic class named 'Cell' that can hold two values of different types. It includes methods to set and retrieve these values, as well as a 'toString' method for representation. Additionally, a 'CellDriver' class demonstrates the usage of the 'Cell' class with examples of integer and string types.
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

Lenguaje de Programacion II (Silverio Del Orbe Abad)

Heriberto perdomo
Matricula 100459918

ASIGNACION UNIDAD 3
Unidad 3 -4 JP Oracle Academic

Generic

Exception

asertion

public class Cell<T1, T2> {


private T1 t1Value;
private T2 t2Value;

public void setValue(T1 t1, T2 t2) {


this.t1Value = t1;
this.t2Value = t2;
}

public T1 getT1Value() {
return t1Value;
}

public T2 getT2Value() {
return t2Value;
}

@Override
public String toString() {
return "Cell{" +
"t1Value=" + t1Value +
", t2Value=" + t2Value +
'}';
}
}

Clase final de CellDriver:


public class CellDriver {
public static void main(String[] args) {
Cell<Integer, String> mixCell = new Cell<>();
Cell<Integer, Integer> integerCell = new Cell<>();

[Link](1, "4");
[Link](45, 60);

int mcType1 = mixCell.getT1Value();


String mcType2 = mixCell.getT2Value();
int icType1 = integerCell.getT1Value();
int icType2 = integerCell.getT2Value();

[Link](mixCell);
[Link](integerCell);
[Link]("The numerical value is: " + mcType1 + ". The text value is: " + mcType2);
[Link]("The first numerical value is: " + icType1 + " and the second is: " + icType2);
} // end main
} // end class CellDriver

You might also like