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

Check Order of Generic Items

Intro to OOP Lab 9

Uploaded by

Hezarfen Fly
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)
54 views4 pages

Check Order of Generic Items

Intro to OOP Lab 9

Uploaded by

Hezarfen Fly
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

CS201 – Lab 9 (Generic Methods and Classes)

Part I
Define a generic method called checkOrder() that checks if four items are in ascending,
neither, or descending order. The method should return -1 if the items are in ascending
order, 0 if the items are unordered, and 1 if the items are in descending order.

The program reads four items from input and outputs if the items are ordered. The items
can be different types, including integers, Strings, characters, or doubles.

Ex. Input

bat hat mat sat


63.2 96.5 100.1 123.5
Output
Order: -1
Order: -1
import [Link];
public class WhatOrder {
// TODO: Define a generic method called checkOrder() that
// takes in four variables of generic type as arguments.
// The return type of the method is integer

// Check the order of the input: return -1 for ascending,


// 0 for neither, 1 for descending

public static void main(String[] args) {


Scanner scnr = new Scanner([Link]);

// Check order of four strings


[Link]("Order: " + checkOrder([Link](), [Link](), [Link](), [Link]()));

// Check order of four doubles


[Link]("Order: " + checkOrder([Link](), [Link](), [Link](), scnr.
nextDouble()));
}
}
CS201 – Lab 9 (Generic Methods and Classes)

Sol:
import [Link];
public class WhatOrder {
public static <T extends Comparable<T>> Integer checkOrder(T i1, T i2, T i3, T i4) {

if (([Link](i2) < 0) && ([Link](i3) < 0) && ([Link](i4) < 0)) {


return -1;
}
else if (([Link](i3) < 0) && ([Link](i2) < 0) && ([Link](i1) < 0)) {
return 1;
}
else {
return 0;
}

public static void main(String[] args) {

Scanner scnr = new Scanner([Link]);

[Link]("Order: " + checkOrder([Link](), [Link](), [Link](), [Link]()));

[Link]("Order: " + checkOrder([Link](), [Link](),


[Link](), [Link]()));

}
CS201 – Lab 9 (Generic Methods and Classes)

Part II
 Write a Java method (generic) that checks the following properties in a array:
o How many: odd integers in the array?
o How many prime numbers in the array?
o Is the content of the array correspond to a palindromes array?

public final class Algorithm {


public static <T> int countIf(Collection<T> c, UnaryPredicate<T> p) {

int count = 0;
for (T elem : c)
if ([Link](elem))
++count;
return count;
}
}
where the generic UnaryPredicate interface is defined as follows:
public interface UnaryPredicate<T> {
public boolean test(T obj);
}
For example, the following program counts the number of odd integers in an integer list:
import [Link].*;

class OddPredicate implements UnaryPredicate<Integer> {


public boolean test(Integer i) { return i % 2 != 0; }
}

public class Test {


public static void main(String[] args) {
Collection<Integer> ci = [Link](1, 2, 3, 4);
int count = [Link](ci, new OddPredicate());
[Link]("Number of odd integers = " + count);
}
}
The program prints:
Number of odd integers = 2

 Write a Java method (generic) that swaps positions of two items in an array?
public static <T> void swap(T[] a, int x, int y) {
T temp = a[x];
a[x] = a[y];
a[y] = temp;
}
CS201 – Lab 9 (Generic Methods and Classes)

 Write a generic method to find the maximal element in the range [begin, end) of a
list.
public static <T extends Comparable> T maximalElement (List<T> list, int from, int to) {
T max = [Link](from);
for (int i = from + 1; i < to; i++) {
T elem1 = [Link](i);
if ([Link](max) > 0) {
max = elem1;
}
}
return max;
}
 Consider this class:
class Node<T> implements Comparable<T> {
public int compareTo(T obj) { /* ... */ }
// ...
}
Will the following code compile? If not, why?
Node<String> node = new Node<>();
Comparable<String> comp = node;

Part III
Write a generic class Pair which has two type parameters: F and S, each representing the
type of the first and second element in the pair, respectively. Add get and set method for
the first and second elements of the pair.

You might also like