You are given two BagInterface objects (using the Name class below): originalNam
ID: 3880250 • Letter: Y
Question
You are given two BagInterface objects (using the Name class below): originalNameBag and shortNameBag. The originalNameBag is a collection of Name objects. The shortNameBag is empty.
Name Class:
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 void giveLastNameTo(Name aName)
{
aName.setLast(last);
} // end giveLastNameTo
public String toString()
{
return first + " " + last;
} // end toString
} // end Nam
Write code statements from the client perspective to accomplish the following tasks:
Place all names whose last name String is < 4 characters into the shortNameBag.
The originalNameBag object should be unchanged when you complete.
Output the size of the shortNameBag.
For example, if the originalNameBag included these names:
Mike Smith, Jane Doe, Jessica Masters, Qi Li
then after your code executes, the originalNameBag would have the same values and shortNameBag would have these names and would output 2.
Jane Doe, Qi Li
Notes:
You can use additional initially empty BagInterface objects if needed.
For full credit, do not use the toArray() method.
Part B:
Repeat the previous question, only using lists instead of bags.
Thus, you are given ListInterface<Name> originalNameList and ListInterface<Name> shortNameList.
The relative order of the names in the shortNameList should match the order of the names in the originalNameList.
Explanation / Answer
Hii.. Please check the below code.
Part A;
Name.java
public class Name {
private String first;
private String last;
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 void giveLastNameTo(Name aName)
{
aName.setLast(last);
} // end giveLastNameTo
public String toString()
{
return first + " " + last;
} // end toString
}
BagMain.java
public class BagMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
Name[] originalNameBag=new Name[4];
for (int i=0;i<4;i++)
{
originalNameBag[i]= new Name(); // create each actual Name
}
originalNameBag[0].setFirst("Mike");
originalNameBag[0].setLast("Smith");
originalNameBag[1].setFirst("Jane");
originalNameBag[1].setLast("Doe");
originalNameBag[2].setFirst("Jessica");
originalNameBag[2].setLast("Masters");
originalNameBag[3].setFirst("Qi");
originalNameBag[3].setLast("Li");
int count=0;
Name[] temp=new Name[4];
for (int i=0;i<4;i++)
{
if(originalNameBag[i].getLast().length()<4){
temp[count] = new Name();
temp[count].setFirst(originalNameBag[i].getFirst());
temp[count].setLast(originalNameBag[i].getLast());
count++;
}
}
Name[] shortNameBag=new Name[count];
for(int i=0;i<count;i++){
shortNameBag[i] = new Name();
shortNameBag[i].setFirst(temp[i].getFirst());
shortNameBag[i].setLast(temp[i].getLast());
}
System.out.println("Orginal Bag Values");
for(int i=0;i<originalNameBag.length;i++){
System.out.println(originalNameBag[i].toString());
}
System.out.println(" Short Bag Values");
for(int i=0;i<shortNameBag.length;i++){
System.out.println(shortNameBag[i].toString());
}
}
}
Output:
Orginal Bag Values
Mike Smith
Jane Doe
Jessica Masters
Qi Li
Short Bag Values
Jane Doe
Qi Li
Part B;
Name class should be same as above and i have written new class with name ListMain.java
ListMain.java
import java.util.ArrayList;
import java.util.List;
public class ListMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Name> originalNameList = new ArrayList<Name>();
List<Name> shortNameList = new ArrayList<Name>();
Name n = new Name();
n.setFirst("Mike");
n.setLast("Smith");
originalNameList.add(n);
n = new Name();
n.setFirst("Jane");
n.setLast("Doe");
originalNameList.add(n);
n = new Name();
n.setFirst("Jessica");
n.setLast("Masters");
originalNameList.add(n);
n = new Name();
n.setFirst("Qi");
n.setLast("Li");
originalNameList.add(n);
for(Name name:originalNameList){
if(name.getLast().length()<4){
shortNameList.add(name);
}
}
System.out.println("Orginal Bag Values");
for(int i=0;i<originalNameList.size();i++){
System.out.println(originalNameList.get(i).toString());
}
System.out.println(" Short Bag Values");
for(int i=0;i<shortNameList.size();i++){
System.out.println(shortNameList.get(i).toString());
}
}
}
Output:
Orginal Bag Values
Mike Smith
Jane Doe
Jessica Masters
Qi Li
Short Bag Values
Jane Doe
Qi Li
Please check the code and let me know any issues. Thank you. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.