You are on page 1of 4

CS

 160:  Fall  2010  


Lab  Assignment  10  
 
J.  Denbigh  Starkey  
 
In-­lab  assignment  due  at  end  of  lab  on  Tuesday,  11/9/10):      
 
The  goal  of  this  lab  is  to  become  familiar  with  Array  Lists.    
 
You  will  have  a  class  called  Course  whose  only  instance  field  is  an  array  List  of  
Student  objects,  where  Student  is  a  class  that  you’ll  also  define.    There  will  be  a  
third  class,  TestIt,  that  calls  on  methods  in  Course  that  let  you  add  and  drop  
students  from  the  course,  print  out  a  course  roster,  check  the  number  of  students  in  
the  course  from  a  specific  major,  etc.  Some  of  these  methods  will  be  in  the  in-­‐lab  and  
some  in  the  out-­‐lab.  
 
Student  will  have  three  instance  fields.  String name,  int id,  and  String major.    
 
Student’s  constructor  will  have  three  parameters,    in_name,  in_id,  and  in_major,  
which  assign  values  to  the  three  instance  fields.  in_major  will  be  saved  to  major  as  
upper  case,  as  you  did  in  an  earlier  lab.  So  if,  for  example,  in_major  is  either  “CS”  or  
“cs”  the  value  assigned  to  major  will  be  “CS”.  (Remember  the  toUpperCase()  
method  in  the  String  class.)  
 
Student  will  have  one  method,  public static void printInfo(),  which  prints  out  
info  on  a  Student.  If,  for  example,  the  current  Student  was  created  by  a  call  from  
another  class  that  was  Student niece = new Student(“Lucy Jupe”, 123,
“French”),  then  niece.printInfo()  should  output    
 
Name: Lucy Jupe, ID: 123, Major: FRENCH
 
Course  will  have  a  single  instance  field,  
 
private ArrayList<Student> courseList = new ArrayList<Student>();
 
which  will  create  a  list  (initially  empty)  of  students  in  the  course.    
 
The  constructor  for  course  will  be:  
 
public Course()
{
// do nothing
}
 
Java  lets  you  leave  out  a  constructor  like  this  if  you  want  to,  but  it  often  makes  
things  more  readable  to  put  it  in  like  this.  
 
Course  will  have  two  methods:  
 
public void addStudent(String first, String last, int id, String major)  
defines  a  new  Student  (to  get  the  Student’s  name  concatenate  the  first  and  last  
names  with  a  space  between  them)  and  will  use  the  ArrayList  method  add()  to  add  
this  student  to  the  end  of  the  course  Array  List.  
 
public void courseRoster()  will  print  out  a  list  of  students  in  the  course.  To  do  
this  use  a  for  loop  that  calls  the  Student  printInfo()  method  on  each  student  in  
the  list.  So  you’ll  be  making  calls  inside  the  loop  like    
courseList.get(i).printInfo();    
since  courseList  is  a  list  of  Student  objects  so  courseList.get(i)  is  a  Student  
object.  
 
Remember  that  you’ll  need  to  import java.util.*;  or  import
java.util.ArrayList;  before  the  Course  header  so  that  it  can  get  the  ArrayList  
code.  
 
The  TestIt  class  will  contain:  
 
public class Testit
{
public static void main()
{
Course csci111 = new Course();

csci111.addStudent("Rosemary", "Hossack", 321, "Law");


csci111.addStudent("John", "Paxton", 123, "Cs");
csci111.addStudent("Ian", "Hossack", 222, "Medical");
csci111.addStudent("Lucy", "Jupe", 444, "French");
csci111.addStudent("Chris", "Jupe", 666, "Engineering");
csci111.addStudent("Denbigh", "Starkey", 107, "cs");
csci111.addStudent("Rocky", "Ross", 987, "CS");
csci111.addStudent("Chris", "Pinet", 456, "French");
csci111.courseRoster();
}
}
 
which  should  give  the  output:  
 
Course listing:
Name: Rosemary Hossack, ID: 321, Major: LAW
Name: John Paxton, ID: 123, Major: CS
Name: Ian Hossack, ID: 222, Major: MEDICAL
Name: Lucy Jupe, ID: 444, Major: FRENCH
Name: Chris Jupe, ID: 666, Major: ENGINEERING
Name: Denbigh Starkey, ID: 107, Major: CS
Name: Rocky Ross, ID: 987, Major: CS
Name: Chris Pinet, ID: 456, Major: FRENCH
 
 
 
 
 
Out-­lab  assignment  (due  by  5:00  pm  on  Monday  11/15/10):    
 
The  out-­‐lab  will  extend  the  three  classes  from  the  in-­‐lab  as  follows:  
 
1. Modify  printInfo()  so  that  the  student’s  name  will  be  output  17  characters  
wide,  left  justified,  so  that  the  output  will  line  up  better.  
2. Add  a  method  public void dropByName(String name)  to  Course  that  looks  
for  a  Student  in  the  array  list  with  that  name,  and  if  found  removes  them  
from  the  array  list.  If  there  isn’t  a  student  with  this  name  in  the  list,  output  an  
error  message.  To  do  this  you’ll  probably  need  a  public String getName()  
method  in  Student.  
3. Add  a  method  dropByID(int id),  which  is  similar  to  dropByName(),  but  lets  
drops  be  done  by  id.  
4. Add  a  method  public  void  listMajor(String searchMajor)  to  Course  which  
lists  all  of  the  students  in  the  course  whose  major  is  searchMajor.  This  will  
probably  need  a  getMajor()  method  in  Student.  
 
To  test  this  change  TestIt  so  that  it  contains:  
 
public class Testit
{
public static void main()
{
Course csci111 = new Course();
csci111.addStudent("Rosemary", "Hossack", 321, "Law");
csci111.addStudent("John", "Paxton", 123, "CS");
csci111.addStudent("Ian", "Hossack", 222, "Medical");
csci111.addStudent("Lucy", "Jupe", 444, "French");
csci111.addStudent("Chris", "Jupe", 666, "Engineering");
csci111.addStudent("Denbigh", "Starkey", 107, "CS");
csci111.addStudent("Rocky", "Ross", 987, "CS");
csci111.addStudent("Chris", "Pinet", 456, "French");
csci111.courseRoster();

csci111.dropByName("Ian Hossack");
csci111.dropByID(107);
csci111.courseRoster();

csci111.dropByName("Queen Elizabeth");
csci111.dropByID(999);

csci111.listMajor("CS");
}
}
 
which  should  give  the  output  shown  below:  
 
 
 
 
 
 
 
Course Roster
Name: Rosemary Hossack, ID: 321, Major: LAW
Name: John Paxton, ID: 123, Major: CS
Name: Ian Hossack, ID: 222, Major: MEDICAL
Name: Lucy Jupe, ID: 444, Major: FRENCH
Name: Chris Jupe, ID: 666, Major: ENGINEERING
Name: Denbigh Starkey, ID: 107, Major: CS
Name: Rocky Ross, ID: 987, Major: CS
Name: Chris Pinet, ID: 456, Major: FRENCH

Course Roster
Name: Rosemary Hossack, ID: 321, Major: LAW
Name: John Paxton, ID: 123, Major: CS
Name: Lucy Jupe, ID: 444, Major: FRENCH
Name: Chris Jupe, ID: 666, Major: ENGINEERING
Name: Rocky Ross, ID: 987, Major: CS
Name: Chris Pinet, ID: 456, Major: FRENCH

Queen Elizabeth isn't in the course

999 isn't in the course

Course Roster for major CS


Name: John Paxton, ID: 123, Major: CS
Name: Rocky Ross, ID: 987, Major: CS
 
 
Optional  additional  out-­lab,  required  for  the  Honors  lab:    
 
1. Add  a  method  public static void removeDuplicates()  to  Course  that  
removes  any  duplicate  entries.  This  might  involve  nested  for  loops  that  
compare  each  element  of  the  list  against  all  following  entries.  To  be  a  
duplicate  it  must  be  equal  in  all  three  fields.  
2. Add  a  private ArrayList<Student> listOfMajors()  method  to  Course  
that  returns  a  list  of  all  majors  in  the  course  without  duplicates.  
3. Ad  a  method  public void listByMajor()  to  Course  that  lists  everyone  in  
the  course,  sorted  by  major.  This  could  use  listOfMajors()  from  part  2.  
 
 

You might also like