Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment you will be creating a Shepherd class that extends a Person c

ID: 3833434 • Letter: I

Question

In this assignment you will be creating a Shepherd class that extends a Person class.

A Shepherd, like any Person, has a name, so when a Shepherd is born (constructed) it is always given a name.

A Shepherd will tend a collection of Sheep objects, and each Sheep object will have a unique name. The Shepherd is not very creative with names, however, so each Sheep is named "Sheep 1", "Sheep 2", "Sheep 3", and so on.

A Shepherd can have a maximum of 20 sheep at any time (any more, and the Shepherd will run out of fingers and toes to count on!).

The Shepherd should use a Collections object to hold onto all of its Sheep.The Shepherd holds onto a maximum of 20 Sheep.

The Shepherd provides a method that you can use to give the Shepherd a bunch of Sheep in a Collections object. However, the Shepherd only has 20 fingers and toes, so any extra Sheep will be eaten by the Java Garbage Collector.

Your Shepherd must follow the rules given above. Use the code specifications below to implement a Shepherd class. Don't forget comments in your code!

Flag this Question

Write a Shepherd class that meets the following specifications:

Shepherd class should utilize the Sheep and Person classes given below.

Shepherd class should extend the Person class and have a name.

Shepherd should name the Sheep in the order they are received, and should match a given Sheep instance to the same name each time ("Sheep 1", "Sheep 2", "Sheep 3", etc).

Shepherd class should implement the following methods:getSheep( List<Sheep> sheep_list )

This method will accept a bunch of Sheep, add it to the flock, and give it a unique name (If "Sheep 1" and "Sheep 2" are taken, the next Sheep gets the name "Sheep 3")

The Shepherd can only have 20 Sheep total, so any extra Sheep are discarded!

5 Points Extra Credit:

countSheep()

This method is run when the Shepherd can't sleep. It uses an iterator to loop over the Collection containing the Sheep and prints out each Sheep's name.

The code below gives the Sheep and Person classes. You should utilize both of these classes in your code. However, you should not upload any files related to the Sheep and Person classes.

Sheep.java:

Person.java:

Explanation / Answer

import java.io.*;
import java.util.*;


class Person {
protected String name;
public Person(String name) {
this.name = name;
}
}


class Sheep {
private int id;
public Sheep(int id) {
this.id = id;
}
public String getName() {
return "Sheep "+this.id;
}
}

class Shepherd extends Person{

ArrayList<Sheep> sheep;
int size;
public Shepherd(String name) {
super(name);
sheep = new ArrayList();
}
  
public void addSheep(Sheep s){
if(sheep.size()<20){
sheep.add(s);
}
}
public void getSheep(List<Sheep> sheep_list){
for(Sheep s : sheep_list){
if(sheep.size()>=20){
break;
}
sheep.add(s);
}
}
public void countSheep(){
for(Sheep s : sheep){
System.out.println(s.getName());
}
}
}

public class Main{

public static void main(String[] args) {
Shepherd s = new Shepherd("John");
for(int i=1;i<=25;i++){
s.addSheep(new Sheep(i));
}
ArrayList<Sheep> extra = new ArrayList();
extra.add(new Sheep(30));
s.getSheep(extra);
s.countSheep();
}
}