Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


LAB 0

CSC172 LAB 0

GENERICS REVIEW


1 Introduction

The labs in CSC172 encourages a pair programming paradigm where every student to has a lab partner. Labs will typically have an even number of components. The two partners in a pair programming environment take turns at the keyboard. This paradigm facilitates code improvement through collaborative efforts, and exercises the programmers cognitive ability to understand and discuss concepts fundamental to computer programming. The use of pair programming is optional in CSC172. It is not a requirement. You can learn more about the pair programming paradigm, it's methods, benefits, underpinnings, and scientific validation at http://en.wikipedia.org/wiki/Pair_programming .

Every student must hand in their own work, but every student must list the name of their lab partner on all labs.

This lab has four parts. You and your partner(s) should switch off typing each part. As one person types the lab, the other should be watching over the code and offering suggestions. Each part should be in addition to the previous parts, so do not erase any previous work when you switch. This can work well on a remote shared screen environment such as ZOOM. Traditionally, it involves two people physically sitting at a workstation – however please observe all officially mandated social distancing regulations.

Collaboration is allowed. You and your lab partner may discuss the lab with other pairs in the lab. It is ok to write code on the white board for the benefit of other lab pairs, but you are not allowed to electronically copy and/or transfer files between groups.


2 Generics

Consider the following code, where the 'printarray()' method is used to print out arrays of different types:

Integer [] intArry = {1, 2, 3, 4, 5 };

Double [] doubArry = {1.1, 2.2, 3.3, 4.4};

Character [] charArray = {'H','E','L', 'L', 'O' };

String [] strArray = {“once”, “upon”, “a”, “time” };

printarray(intArry);

printarray(doubArry);

printarray(charArray);

printarray(strArray);


1. Write a JAVA program with a main method that includes the example code, above. Implement the static printarray() method using an array of Objects. Compiler and run your code. 


2. Comment out, but do not delete, the previous implementation of the printarray() method and implement the printarray() method using method overloading. Write four versions of printarray(), one with the appropriate array type. Compile and run your code.


3. Comment out, but do not delete, the previous implementation of printarray(). Now write a single method using Generic programming technique so that you have a single method supporting all the types and maintain type safety.


4. Using non-generic techniques. Write a method that takes an array of type Comparable and returns the maximum element in the array. [i.e. “public static Comparable getMax(Comparable [] a)”]. Add the following code to your main routine.

System.out.println(“max Integer is : “ + getMax(intArry);

System.out.println(“max Double is : “ + getMax(doubArry);

System.out.println(“max Character is : “ + getMax(charArry);

System.out.println(“max String is : “ + getMax(strArry);

Compile your code. Copy and paste any compiler warning you get into a comment block at the head of your implementation of getMax(). Run your code.


5. Comment out, but do not delete, your previous implementation of getMax(). Using the generic techniques to specify super-class relationships. Implement a type safe version of getMax();


6. Consider (copy, compile, run) the code example below. The findMax method is generic and it takes a functor (“function object”). If you don’t like the way strings are sorted, then you can define your own way to compare strings. Define, code, and test two new comparison methods using the functor technique.

1. Write a functor that compares Stings by length rather than alphanumerically.

2. Write a functor that compares Strings by reverse alphanumerically ordering (“z” comes before “a”).

3. Define a new Java Class “Student”. Students have first names, last names and grade point averages. Write two function objects so that the findMax code below works on an array of your Student class. Demonstrate that your comparator works with findMax

1. Compare students by last name, alphabetically

2. Compare students by GPA




3 Hand In

        Hand in the source code from this lab at the appropriate location on the Blackboard system. You should hand in a single compressed/archived (i.e. “zipped” file that contains the following.) Typically, the zipped file contains an entire project structure built in Eclipse.

1. A plain text file named README that includes your contact information, your partner's name, a brief explanation of the lab (A one paragraph synopsis. Include information identifying what class and lab number your files represent.). And a one sentence explaining the contents of all the other files you hand in.

2. JAVA source code files (you may decide how many you need) representing the work accomplished in this lab. All source code files should contain author and partner identification in the comments at the top of the file.

3. A file named OUTPUT that includes author information at the beginning and shows the compile and run steps of your code. The best way to generate this file is to cut and paste from the command line or use a screenshot of your Eclipse IDE.


4 Grading Rubric

Read me file – 10%

Output file – 10%

Sections 1, 2, 3, 4, 5 – 10% each

Section 6 - 30 %