| Author |
Casting Problem *Help Please
|
Henry Bueno
Greenhorn
Joined: Apr 03, 2003
Posts: 8
|
|
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}] code: -------------------------------------------------------------------------------- //----------------------------------------- //EXERCISE1.JAVA //----------------------------------------- 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 = new TableRow(); 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() ) { /***casting problem*** row = (TableRow)*/ i.next(); System.out.println( "First Name is: " + row.get("firstName")); } //display the entire table System.out.println(table.toString() ); } } //----------------------------------------- //TABLE.JAVA //----------------------------------------- import java.util.*; public class Table{ //fields ArrayList tableRows;//The List //constructors public Table(){ tableRows = new ArrayList(); } //methods public void addRow(TableRow rowIn){ tableRows.add(rowIn.getRow()); } public Iterator iterator() { return tableRows.iterator(); } public String toString() { for ( int k = 0; k < tableRows.size(); k++ ) System.out.println( tableRows.get( k ) ); return null; } } //----------------------------------------- //TABLEROW.JAVA //----------------------------------------- public class TableRow{ //fields HashMap data; //constructors public TableRow(){ data = new HashMap(); } //methods public void set(String p1, String p2){ data.put(p1,p2); } public String get(String search) { return (String)data.get(search); } public String getRow() { String dataRow = new String(); { dataRow = data.toString(); } return dataRow; } } //---------------------------------------- //end //---------------------------------------- How can I do to configure this code to make it better? Thank You
|
 |
Henry Bueno
Greenhorn
Joined: Apr 03, 2003
Posts: 8
|
|
I fixed it. I removed the getRow(). Then I changed the toString() and gave it a String Buffer... I also gave the TableRow.class a toString(). It works well.
|
 |
 |
|
|
subject: Casting Problem *Help Please
|
|
|