This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Casting Problem *Help Please  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
What are your superhero powers? Go ahead and try them on this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!