Directed Lab Work Step 1. Create a new Eclipse project Lab17. Step 2. Copy the f
ID: 3817300 • Letter: D
Question
Directed Lab Work
Step 1. Create a new Eclipse project Lab17.
Step 2. Copy the files LinkInterface.java, Student.java, Name.java, and VectorList.java from Blackboard to the Eclipse project.
Step 3. In the project, create a client class StudentNames.
Step 4. In StudentNames create a list of objects of type Student and enter information about 5 students in that list.
Step 5. Write the statements that implement what is requested in Problem 4.
ListInterface.java
/**
An interface for the ADT list.
Entries in the list have positions that begin with 1.
@author Frank M. Carrano
@version 3.0
*/
public interface ListInterface<T>
{
/** Adds a new entry to the end of this list.
Entries currently in the list are unaffected.
The listÕs size is increased by 1.
@param newEntry the object to be added as a new entry */
public void add(T newEntry);
/** Adds a new entry at a specified position within this list.
Entries originally at and above the specified position
are at the next higher position within the list.
The listÕs size is increased by 1.
@param newPosition an integer that specifies the desired
position of the new entry
@param newEntry the object to be added as a new entry
@return true if the addition is successful, or
false if newPosition < 1, or newPosition > getLength() + 1 */
public boolean add(int newPosition, T newEntry);
/** Removes the entry at a given position from this list.
Entries originally at positions higher than the given
position are at the next lower position within the list,
and the listÕs size is decreased by 1.
@param givenPosition an integer that indicates the position of
the entry to be removed
@return a reference to the removed entry or null, if either
the list was empty, givenPosition < 1, or
givenPosition > getLength() */
public T remove(int givenPosition);
/** Removes all entries from this list. */
public void clear();
/** Replaces the entry at a given position in this list.
@param givenPosition an integer that indicates the position of
the entry to be replaced
@param newEntry the object that will replace the entry at the
position givenPosition
@return true if the replacement occurs, or false if either the
list is empty, givenPosition < 1, or
givenPosition > getLength() */
public boolean replace(int givenPosition, T newEntry);
/** Retrieves the entry at a given position in this list.
@param givenPosition an integer that indicates the position of
the desired entry
@return a reference to the indicated entry or null, if either
the list is empty, givenPosition < 1, or
givenPosition > getLength() */
public T getEntry(int givenPosition);
/** Sees whether this list contains a given entry.
@param anEntry the object that is the desired entry
@return true if the list contains anEntry, or false if not */
public boolean contains(T anEntry);
/** Gets the length of this list.
@return the integer number of entries currently in the list */
public int getLength();
/** Sees whether this list is empty.
@return true if the list is empty, or false if not */
public boolean isEmpty();
/** Retrieves all entries that are in this list in the order in which
they occur in the list. */
public T[] toArray();
} // end ListInterface
VectorList.java
import java.util.Vector;
/**
A class that implements the ADT list by using an instance of Vector.
@author Frank M. Carrano
@version 3.0
*/
public class VectorList<T> implements ListInterface<T>
{
private Vector<T> list; // entries in list
public VectorList()
{
list = new Vector<T>(); // when necessary, vector doubles in size
} // end default constructor
public VectorList(int initialSize)
{
list = new Vector<T>(initialSize);
} // end constructor
public void add(T newEntry)
{
// place new entry after last current entry
list.add(newEntry);
} // end add
public boolean add(int newPosition, T newEntry)
{
boolean isSuccessful = true;
if ((newPosition >= 1) && (newPosition <= list.size()+1))
list.add(newPosition - 1, newEntry);
else
isSuccessful = false;
return isSuccessful;
} // end add
public T remove(int givenPosition)
{
T result = null; // return value
if ((givenPosition >= 1) && (givenPosition <= list.size()))
{
assert !isEmpty();
result = list.remove(givenPosition - 1);
} // end if
return result; // return reference to removed entry,
// or null if list is empty
} // end remove
public boolean replace(int givenPosition, T newEntry)
{
boolean isSuccessful = true;
if ((givenPosition >= 1) && (givenPosition <= list.size()))
{
assert !isEmpty();
list.set(givenPosition - 1, newEntry);
}
else
isSuccessful = false;
return isSuccessful;
} // end replace
public T getEntry(int givenPosition)
{
T result = null; // result to return
if ((givenPosition >= 1) && (givenPosition <= list.size()))
{
assert !isEmpty();
result = list.get(givenPosition - 1);
} // end if
return result;
} // end getEntry
public boolean contains(T anEntry)
{
return list.contains(anEntry);
} // end contains
public void clear()
{
list.clear();
} // end clear
public int getLength()
{
return list.size();
} // end getLength
public boolean isEmpty()
{
return list.isEmpty();
} // end isEmpty
public T[] toArray()
{
@SuppressWarnings("unchecked")
T[] result = (T[])list.toArray();
return result;
} // end toArray
} // end VectorList
Student.java
public class Student
{
private Name fullName;
private String id; // identification number
public Student() {
fullName = new Name();
id = "";
} // end default constructor
public Student(Name studentName, String studentId) {
fullName = studentName;
id = studentId;
} // end constructor
public void setStudent(Name studentName, String studentId) {
setName(studentName); // or fullName = studentName;
setId(studentId); // or id = studentId;
} // end setStudent
public void setName(Name studentName) {
fullName = studentName;
} // end setName
public Name getName() {
return fullName;
} // end getName
public void setId(String studentId) {
id = studentId;
} // end setId
public String getId() {
return id;
} // end getId
public String toString() {
return id + " " + fullName.toString();
} // end toString
} // end Student
Name.java
public class Name {
private String first; // first name
private String last; // last name
public Name() {
} // end default constructor
public Name(String firstName, String lastName) {
first = firstName;
last = lastName;
} // end constructor
public void setName(String firstName, String lastName) {
setFirst(firstName);
setLast(lastName);
} // end setName
public String getName() {
return toString();
} // end getName
public void setFirst(String firstName) {
first = firstName;
} // end setFirst
public String getFirst() {
return first;
} // end getFirst
public void setLast(String lastName) {
last = lastName;
} // end setLast
public String getLast() {
return last;
} // end getLast
public String toString() {
return first + " " + last;
} // end toString
} // end Name
Explanation / Answer
StudentNames.java
public class StudentNames {
public static void main(String[] args)
{
VectorList<Student> studentList = new VectorList<>();
Name name1 = new Name("First name1", "Last name 1");
Student student1 = new Student(name1, "ID1");
studentList.add(student1);
Name name2 = new Name("First name2", "Last name 2");
Student student2 = new Student(name2, "ID2");
studentList.add(student2);
Name name3 = new Name("First name3", "Last name 3");
Student student3 = new Student(name3, "ID3");
studentList.add(student3);
Name name4 = new Name("First name4", "Last name 4");
Student student4 = new Student(name4, "ID4");
studentList.add(student4);
Name name5 = new Name("First name5", "Last name 5");
Student student5 = new Student(name5, "ID5");
studentList.add(student5);
for(int i = 1; i <= studentList.getLength(); i++)
{
System.out.println(studentList.getEntry(i));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.