0% found this document useful (0 votes)
8 views18 pages

05.AV-GenerickoProgramiranje Auditorne

Uploaded by

Din Sadović
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)
8 views18 pages

05.AV-GenerickoProgramiranje Auditorne

Uploaded by

Din Sadović
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/ 18

Generičko

programiranje u Javi
AUDITORNE VJEŽBE
Sadržaj
Primjer metode koja vraća generički tip
podatka
Primjer parametrizirane klase
Primjer korištenje zamjenskih simbola
Pitanja s certifikata
Primjer metode koja vraća generički tip
podatka
• Klase koje implementiraju sučelje „Comparable” pružaju mogućnost svojim
objektima da se međusobno uspoređuju korištenjem metode „compareTo”
• Ako je potrebno napisati generičku metodu koja može primati samo takve klase,
onda je to moguće navesti kod same deklaracije klase korištenjem preduvjeta
„extends Comparable”
• Sučelje „Comparable” također prima generički tip pa je u tom slučaju moguće
koristiti i isti tip T:
public static <T extends Comparable<T>> T maximum(T x, T y, T z)
Primjer metode koja vraća generički tip
podatka
public static <T extends Comparable<T>> T maximum(T x, T y, T z)
{
T max = x;

if (y.compareTo(max) > 0)
max = y;

if (z.compareTo(max) > 0)
max = z;

return max;
}
Primjer metode koja vraća generički tip
podatka
public static void main(String[] args)
{
System.out.printf("Maximum of %d, %d and %d is %d%n%n", 3, 4, 5,
maximum(3, 4, 5));
System.out.printf("Maximum of %.1f, %.1f and %.1f is %.1f%n%n",
6.6, 8.8, 7.7, maximum(6.6, 8.8, 7.7));
System.out.printf("Maximum of %s, %s and %s is %s%n", "pear",
"apple", "orange", maximum("pear", "apple", "orange"));
}

Maximum of 3, 4 and 5 is 5

Maximum of 6.6, 8.8 and 7.7 is 8.8

Maximum of pear, apple and orange is pear


Primjer parametrizirane klase
public class Stack<T>
{
private List<T> elements;

public Stack() {
this(10);
}

public Stack(int capacity)


{
int initCapacity = capacity > 0 ? capacity : 10;
elements = new ArrayList<T>(initCapacity);
}
Primjer parametrizirane klase
public void push(T pushValue) {
elements.add(pushValue);
}

public T pop()
{
if (elements.isEmpty()) {
throw new EmptyStackException("Stack is empty, cannot pop");
}
return elements.remove(elements.size() - 1);
}
}
Primjer parametrizirane klase
public static void main(String[] args) {
double[] doubleElements = {1.1, 2.2, 3.3, 4.4, 5.5};

Stack<Double> doubleStack = new Stack<>(5);

testPushDouble(doubleStack, doubleElements);
testPopDouble(doubleStack);
}

private static void testPushDouble(Stack<Double> stack, double[] values) {


System.out.printf("%nPushing elements onto doubleStack%n");
for (double value : values) {
System.out.printf("%.1f ", value);
stack.push(value);
}
}
Primjer parametrizirane klase
private static void testPopDouble(Stack<Double> stack) {
try {
System.out.printf("%nPopping elements from doubleStack%n");
double popValue;
while (true) {
popValue = stack.pop();
System.out.printf("%.1f ", popValue);
}
}
catch(EmptyStackException emptyStackException)
{
System.err.println();
emptyStackException.printStackTrace();
}
}
Primjer korištenja zamjenskih simbola
public static void main(String[] args)
{
Integer[] integers = {1, 2, 3, 4, 5};
List<Integer> integerList = new ArrayList<>();

for (Integer element : integers)


integerList.add(element);

System.out.printf("integerList contains: %s%n", integerList);


System.out.printf("Total of the elements in integerList: %.0f%n%n",
sum(integerList));
Primjer korištenja zamjenskih simbola
Double[] doubles = {1.1, 3.3, 5.5};
List<Double> doubleList = new ArrayList<>();

for (Double element : doubles)


doubleList.add(element);

System.out.printf("doubleList contains: %s%n", doubleList);


System.out.printf("Total of the elements in doubleList: %.1f%n%n",
sum(doubleList));

Number[] numbers = {1, 2.4, 3, 4.1};


List<Number> numberList = new ArrayList<>();

for (Number element : numbers)


numberList.add(element);
Primjer korištenja zamjenskih simbola
System.out.printf("numberList contains: %s%n", numberList);
System.out.printf("Total of the elements in numberList: %.1f%n",
sum(numberList));
}

public static double sum(List<? extends Number> list) {


double total = 0;

for (Number element : list)


total += element.doubleValue();

return total;
}
Pitanja s certifikata (1)
Pitanja s certifikata (2)
Pitanja s certifikata (3)
Pitanja s certifikata (4)
Pitanja s certifikata (5)
Pitanja?

You might also like