Task Description: Use notepad or textpad to implement two classes(Table & TableRow)to support the code listed below titled Exercise1.java. Compile your classes and then compile Exercise1.java. Test and run. Hints: * Consider using Java collection classes to implement Table and TableRow. Expected Output: First Name is : Mickey First Name is : Donald Table with 2 rows [{phone=407-555-1212, age=50, lastName=Mouse, firstName=Mickey}{phone=800-555-1212, age=40, lastName=Duck, firstName=Donald}] //------------------------------ //Table.java Class //------------------------------ import java.util.*; public class Table{ //fields protected ArrayList tableRows; //The List
//constructor public Table(){ tableRows = new ArrayList(); } //methods public void addRow(TableRow rowIn){ tableRows.add(rowIn); }
public Iterator iterator() { return tableRows.Iterator(); } public String toString() { Map mapRef = tableRows; return mapRef.toString(); } } //------------------------------ //TableRow.java Class //------------------------------ import java.util.*; public class TableRow{ //fields HashMap data; //constructors public TableRow(){ data = new HashMap(); } //methods public void set(String p1, String p2){ data.put(p2,p1); } public String get(String search) { return data.get(search.toString()); } } //------------------------------ //Exercise1.java Class //------------------------------ import java.util.Iterator; public class Exercise1 { public static void main(String[] args) { Table table = new Table(); //add first row to table TableRow row = new TableRow(); row.set("firstName", "Mickey"); row.set("lastName", "Mouse"); row.set("phone", "407-555-1212"); row.set("age", "50"); table.addRow( row ); //add second row to table row.set("firstName", "Donald"); row.set("lastName", "Duck"); row.set("phone", "800-555-1212"); row.set("age", "40"); table.addRow( row ); //display the first name for all rows Iterator i = table.iterator(); while ( i.hasNext() ) { row = (TableRow) i.next(); System.out.println( "First Name is: " + row.get("firstName")); } //display the entire table System.out.println(table.toString() ); } } ------------------------------------------ ------------------------------------------ Please Help. Thanks.
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
Please do not double post. Respond in Beginner please.