Lab10 Generic Classes - tut work PDF

Title Lab10 Generic Classes - tut work
Course Intermediate Object-Oriented Programming
Institution La Trobe University
Pages 8
File Size 192.3 KB
File Type PDF
Total Downloads 638
Total Views 952

Summary

Intermediate Object-Oriented Programming Lab 10 Handout Introduction 1. Generic classes and generic methods in Java allow for the creation of general, reusable code. This lab will introduce you to generic classes and generic methods. 2. There are many container classes already provided for programme...


Description

Intermediate Object-Oriented Programming Lab 10 Handout

Introduction 1.

Generic classes and generic methods in Java allow for the creation of general, reusable code. This lab will introduce you to generic classes and generic methods.

2.

There are many container classes already provided for programmers in the Java APIs. The ArrayList class is one such container class. It is a generic class. In this lab, you will use the ArrayList to develop an application

Objectives    

To understand how to create generic classes and generic methods To learn how to use generic classes and generic methods To learn about the ArrayList class from the Java APIs To practise using the methods provided by the ArrayList class

1

Part 1 – Generic Classes and Generic Methods Question 1 The following class that stores a String and keeps track of how many times the String has been changed. public class StringChangeTracker { private String current; // the current String private String original; // the original String private int changes; // count number of changes public StringChangeTracker(String original) { current = original; this.original = original; changes = 0; } public void update(String newString) { if(! newString.equals(current)) { current = newString; changes++; } }

} Convert the class into a generic class that can be used to keep track of changes for an element of any class type or interface type. Write a small program to test the generic version.

2

Question 2 The following class has three sorting methods that implement the gnome sort algorithm (see description on the next page). 

The first method sorts an array of Strings



The second method (to be completed) sorts an ArrayList of Strings



The third (to be completed) is a generic method to sort an ArrayList of any type that implements the Comparable interface.

import java.util.*; public class Sorter { // Method to sort a completely filled array of Strings public static void stringGnomeSort(String[] a) { int current = 1; // current location // assume the array has at least 2 elements while(true) { if (current == a.length) { break; } else if(current == 0) { current++; } else if(a[current - 1].compareTo(a[current]) > 0) { //swap a[current-1] and a[current] String temp = a[current - 1]; a[current - 1] = a[current]; a[current] = temp; current--; } else { current++; } } } // Method to sort an ArrayList of Strings public static void stringGnomeSort(ArrayList list) { // TODO } // generic method to sort an ArrayList public static void gnomeSort( ArrayList list) { // TODO } }

3

Copy Sorter.java, and a test program SorterTester.java from LMS. Implement the second and third sorting methods and test them

Gnome sort algorithm: Start at the second position. Repeat the following: If the element on the left is bigger than the one at current position, Swap the two and then move one step to the left (case 1) If the element on the left is smaller or equal to the one at current position Move one step to the right (case 2) If the current position is the first position Move one step to the right (case 3) If the current position is beyond the last position Stop (case 4) When implementing the algorithm, cases 3 and 4 have to be tested first

4

Part 2 – Class ArrayList Question 1 Backgrounds: Television Show Ratings The Mammal Channel produces television shows for the animal-lovers demographic. Like all television stations, they review the rating performance at the end of each ratings year to determine which television shows will be renewed for the following year. You are supplied with three files: 

Class Episode.java. This class represents the screening of an episode on the channel. Copy this class from LMS or from the lab11 directory of the library area: cp /home/1st/csilib/cse1ioo/lab11/Episode.java . (The file is also reproduced at the end of the handout for your reference.) An episode has a show name, an episode name, a season number, an episode number, and the rating for that episode (how many people watched it).



The text file screenings.txt. This file contains the data of all the episodes screened in the last season. The episodes appear in the order they were screened. They are formatted one episode per line with fields separated by colons (:). The order of the fields in a line is: ‒ ‒ ‒ ‒ ‒

show name episode name season number episode number rating

Copy the screenings.txt file from LMS or from the lab11 directory of the library area. (The file is reproduced at the end of the handout for your reference.) cp /home/1st/csilib/cse1ioo/lab11/screenings.txt . 

The driver program MammalChannelTester.java, which can be copied from LMS.

a) In a program called MammalChannel.java, write a class method that takes the name of a text file and a ArrayList of Episode objects and adds all the episodes in the specified file in sequence to the end of the list. See MammalChannelTester for the signature of this method. Do this for every method you are to write for MammalChannel. b) Write a class method that outputs to the screen the objects in an ArrayList, whose objects can be of any type. c) Use MammalChannelTester to test what you have done for parts (a) and (b). 5

Question 2 More functionality is needed to help the television channel determine which shows performed well. Add each of the following class methods to your MammalChannel program, and use MammalChannelTester to test it. a)

Write a method that takes a ArrayList of Episode objects and returns a ArrayList of strings which contains the names of all the different shows shown on the network. Test this method. You should find that there are the following shows:

Ducks Dreaming Elk Tales Cats vs Dogs Sahara SitCom Pigs on the Run

b)

Write a method that takes a ArrayList of Episode objects and returns a reference to the episode that had the highest rating. For the given data file this should be:

Elk Tales 1-1: Treasure Pasture (2900 viewers)

(For simplicity, assume there is no tie. As an additional exercise: Write a solution without making this assumption.) c)

Write a method to return how many episodes were shown of a particular show. This method should take the list of episodes and the name of a show and return how many episodes in the list had that name.

Question 3 a)

Write a class method for class MammalChannel that outputs each episode in a list of episodes to a binary file. This method should take the name of the binary file (to write to) and the ArrayList of episodes. The method should iterate through the list and send each episode to the file sequentially. Do not forget to save also the number of episodes first.

b)

Write a class method that reads in the episodes from a binary file. This method should take the name of the binary file (to read from) and return a ArrayList of Episode objects. You will need to read each object back in one at a time and then add it to the list.

c)

Use MammalChannelTester to test what you have for parts (a) and (b); i.e. to save the ArrayList of Episode objects to a binary file and then read them back from file into a different ArrayList variable. Check that the 2 lists have the same content.

6

Class Episode import java.util.*; import java.io.Serializable; public class Episode implements Serializable { private String showName; private String episodeName; private int seasonNumber; private int episodeNumber; private int rating; public Episode(String show, String episode, int sNumber, int eNumber, int rating) { showName = show; episodeName = episode; seasonNumber = sNumber; episodeNumber = eNumber; this.rating = rating; } public String getShowName() { return showName; } public String getEpisodeName() { return episodeName; } public int getSeasonNumber() { return seasonNumber; } public int getEpisodeNumber() { return episodeNumber; } public int getRating() { return rating; } public String toString() { return showName + " " + seasonNumber + "-" + episodeNumber + ": " + episodeName + " (" + rating + " viewers)"; } public void setShowName(String name) { showName = name; } public void setEpisodeName(String episode) { episodeName = episode; }

7

public void setSeasonNumber(int season) { seasonNumber = season; } public void setEpisodeNumber(int episode) { episodeNumber = episode; } public void setRating(int rating) { this.rating = rating; } } screenings.txt Ducks Dreaming: Duck and Punishment: 2 : 1 : 1500 Elk Tales: A Tale of Two Elks: 1 : 1 : 2000 Ducks Dreaming: Far from the Quacking Crowd: 2 : 3 : 1500 Elk Tales: An Ideal Elk: 1 : 1 : 2200 Ducks Dreaming: Little Ducks: 2 : 4 : 1500 Elk Tales: Elk Karenina: 1 : 1 : 2400 Ducks Dreaming: MacDuck: 2 : 2 : 1500 Elk Tales: Idle Thoughts of an Idle Elk: 1 : 1 : 2500 Cats vs Dogs: 100 Dalmations: 1: 2: 800 Ducks Dreaming: Our Mutual Duck: 2 : 5 : 1500 Elk Tales: Moby Elk: 1 : 1 : 2600 Cats vs Dogs: Where's Puss's Boots?: 1: 1: 800 Ducks Dreaming: Tarzan of the Ducks: 2 : 6 : 1500 Cats vs Dogs: Lassie Needs a GPS: 1: 3: 900 Elk Tales: The Adventures of Huckleberry Elk: 1 : 1 : 2700 Sahara SitCom: The Importance of Being Tigger: 1 : 1: 2000 Ducks Dreaming: Duck of the D'Urbervilles: 2 : 7 : 1500 Cats vs Dogs: Ode to Spot: 1: 4: 1000 Elk Tales: The Invisble Elk: 1 : 1 : 2800 Sahara SitCom: The Giraffe King: 1 : 2: 1800 Ducks Dreaming: The Catcher in the Sky: 2 : 8 : 1500 Cats vs Dogs: The Cat's Lost his Hat: 1: 5: 1200 Elk Tales: Treasure Pasture: 1 : 1 : 2900 Pigs on the Run: Babe Saves the Day: 3: 1: 100 Cats vs Dogs: Toto's Troubles in Kansas: 1: 6: 1300 Ducks Dreaming: The Duck of Monte Cristo: 2 : 9 : 1500 Pigs on the Run: Miss Piggy goes to Town: 3: 2: 90 Ducks Dreaming: The Duck in the Iron Mask:2 : 10 : 1500 Sahara SitCom: Wuthering Lows: 1: 5: 1600 Cats vs Dogs: Garfield has Lunch... again: 1: 7: 1500 Pigs on the Run: Porky Pig brings down the House: 3: 3: 95 Ducks Dreaming: The Taming of the Duck: 2 : 11 : 1500 Pigs on the Run: A Piglet in the Hand is worth Two in the Mud: 3: 4: 100 Ducks Dreaming: Duck Dream Psychology: 2 : 12: 1500 Sahara SitCom: The Zebra's Guide to the Galaxy: 1: 7: 1200 

8...


Similar Free PDFs