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!
    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

Please Help Me with Program  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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}]
//------------------------------
//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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do not double post. Respond in Beginner please.
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!