Question: Part 1: Write a program that creates Pet objects from data read from the keyboard. Store these objects into an instance of ArrayList. Then sort
Part 1:
Write a program that creates Pet objects from data read from the keyboard. Store these objects into an instance of ArrayList. Then sort the Pet objects into alphabetic order by pet name, and finally display the data in the sorted Pet objects on the screen. Use PetRecord.java provided.
Part 2:
Create an application that will keep track of several groups of strings. Each string will be a member of exactly one group. We would like to be able to see whether two strings are in the same group as well as perform a union of two groups. Use a linked structure to represent a group of strings. Each node in the structure contains a string and a reference to another node in the group. For example, the group {"a", "b", "d", "e"} is represented by the following structure:
One string in each group"d" in our exampleis in a node that has a null reference. That is, it does not reference any other node in the structure. This string is the representative string of the group. Create the class GroupHolder to represent all of the groups and to perform operations on them. It should have the private instance variable items to hold the nodes that belong to all of the groups. The nodes within each group are linked together as described previously. Make items an instance of ArrayList whose base type is GroupNode, where GroupNode is a private inner class of GroupHolder. GroupNode has the following private instance variables:
dataa string
linka reference to another node in the group, or null
Define the following methods in the class GroupHolder:
addItem(s)adds a string s to an empty group. First search items for s; if you find s, do nothing; if you do not find s, create a new GroupNode object that has s as its string and null as its link and add it to items. The new group will contain only the item s.
getRepresentative(s)returns the representative string for the group containing s. To find the representative string, search items for s. If you do not find s, return null. If you find s, follow links until you find a null reference. The string in that node is the representative string for the group.
getAllRepresentativesreturns an instance of ArrayList that contains the representative strings of all the groups in this instance of GroupHolder. (A representative string is in an instance of GroupNode that contains a null reference.)
inSameGroup(s1, s2)returns true if the representative string for s1 and the representative string for s2 are the same and not null, in which case the strings s1 and s2 are in the same group.
union(s1, s2)forms the union of the groups to which s1 and s2 belong. (Hint: Find the representative strings for s1 and s2. If they are different and neither is null, make the link of the node containing s1s representative string reference the node for s2s representative string.)
For example, suppose that we call addItem with each of the following strings as an argument: "a","b","c","d","e","f","g", and "h". Next, lets form groups by using these union operations:
union("a", "d"), union("b", "d"), union("e", "b"), union("h", "f")
We will have four groups{"a", "b", "d", "e"}, {"c"}, {"f", "h"}, and {"g"}represented by the following structure:
The representative strings for these four groups are "d","c","f", and "g", respectively. Now the operation inSameSet("a", "e") would return true because both getRepresentative("a") and getRepresentative("e") return d. Also, inSameSet("a", "f") would return false because getRepresentative("a") returns d and getRepresentative("f") returns f. The operation union("a", "f") would make the node containing the representative string of the group to which "a" belongswhich is "d"reference the node containing the representative string of the group to which "f" belongs, which is "f". This reference would be represented by an arrow from "d" to "f" in the previous diagram.
Your application should create an instance of GroupHolder and allow the user to add an arbitrary number of strings, each to its own group. It should then perform an arbitrary number of union operations to form several groups. Finally, it should demonstrate the other operations.
Part 3:
There are n people in a room, where n is an integer greater than or equal to 2. Each person shakes hands once with every other person. What is the total number of handshakes in the room? Write a recursive method to solve this problem with the following header:
public static int handshake(int n)
where handshake(n) returns the total number of handshakes for n people in the room. To get you started, if there are only one or two people in the room, then:
handshake(1) = 0
handshake(2) = 1
import java.io.*; import java.util.*;
/** Class for basic pet records: name, age, and weight.
Same as PetRecord, Listing 6.1 except: 1. It implements Serializable. 2. It has a readInput() method, similar to Species, Listing 5.19 3. It has a toString() method similar to Species, Listing 10.9 */
public class PetRecord implements Serializable { private String name; private int age; // in years private double weight; //in pounds
public void readInput() { Scanner keyboard = new Scanner(System.in);
System.out.println("Enter pet's name:"); name = keyboard.nextLine(); System.out.println("Enter pet's age:"); age = keyboard.nextInt(); if(age < 0) { System.out.println("Error: Age cannot be negative."); System.exit(0); } System.out.println("Enter pet's weight:"); weight = keyboard.nextDouble(); if(weight < 0) { System.out.println("Error: Weight cannot be negative."); System.exit(0); } }
public String toString() { return ("Name = " + name + " " + "Age = " + age + " " + "Weight = " + weight + " "); }
public void writeOutput() { System.out.println("Name: " + name); System.out.println("Age: " + age + " years"); System.out.println("Weight: " + weight + " pounds"); }
public PetRecord( String initialName, int initialAge, double initialWeight) { name = initialName; if ((initialAge < 0) || (initialWeight < 0)) { System.out.println("Error: Negative age or weight."); System.exit(0); } else { age = initialAge; weight = initialWeight; } }
public void set(String newName, int newAge, double newWeight) { name = newName; if ((newAge < 0) || (newWeight < 0)) { System.out.println("Error: Negative age or weight."); System.exit(0); } else { age = newAge; weight = newWeight; } }
public PetRecord(String initialName) { name = initialName; age = 0; weight = 0; }
public void set(String newName) { name = newName; // age and weight are unchanged. }
public PetRecord(int initialAge) { name = "No name yet."; weight = 0; if (initialAge < 0) { System.out.println("Error: Negative age."); System.exit(0); } else age = initialAge; }
public void set(int newAge) { if (newAge < 0) { System.out.println("Error: Negative age."); System.exit(0); } else age = newAge; // name and weight are unchanged. }
public PetRecord(double initialWeight) { name = "No name yet"; age = 0; if (initialWeight < 0) { System.out.println("Error: Negative weight."); System.exit(0); } else weight = initialWeight; }
public void set(double newWeight) { if (newWeight < 0) { System.out.println("Error: Negative weight."); System.exit(0); } else weight = newWeight; // name and age are unchanged. }
public PetRecord() { name = "No name yet."; age = 0; weight = 0; }
public String getName() { return name; }
public int getAge() { return age; }
public double getWeight() { return weight; } }
UMLs
| PetRecordSortedByName |
| none |
| + main(): void + addSomePets(List |
| GroupHolder |
| - items: ArrayList |
| + GroupHolder() + findGroupNodeFor(String s): GroupNode + addItem(String s): void + getRepresentative(String s): String + getAllRepresentatives(): ArrayList + inSameGroup(String s1, String s2): boolean + union(String s1, String s2): void - class GroupNode + main() |
| GroupNode internal class to GroupHolder |
| - data: String - next: GroupNode |
| + GroupNode() + GroupNode(String newData, GroupNode nextValue)
|
findGroupNodeFor:
Create an instance of an iterator
Create a node variable of type GroupNode set to null
Create a Boolean variable found set to false
Use a while loop to test: Does iterator variable have a next and if not found
Retrieve the next value from the iterator and store it in the node
If the node data equals s, then change found to true
Outside the loop, test to see if the item was found: If found, return the node. Else, return null.
Union:
Call getRepresentative sending it s1 and again sending it s2 storing the variables in rep1 and rep2
Only if rep1 and rep2 are not equal to null and rep1 is not equal to rep2, then call findGroupNodeFor() method using rep1 and store the result into a GroupNode variable named node1. Then call findGroupNodeFor() method using rep2 and store the result into a GroupNode variable named node2.
node2 is stored in node1.next
| Handshake |
| none |
| + handshake()- NOTE: This is not a constructor but a method- note the smallcase + main() |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
