A bit of a late entry for last weeks classes. The lecture was once again presented by Michael Smith and this week we ran through a number of Java (and programming) basics.

  • Library Classes – Java API, API documentation is generally the first google results when you search a method/data type + ‘JAVA’
  • Collections – Arrays and ArrayLists is a types of collection
  • Dynamic Arrays – called ‘ArrayList ‘ in Java (there may be other collections in Java? but ArrayList the only one I know of). Much better than arrays in that the size is dynamic and objects can be stored and all iteration and sorting is pretty much already optimized. Java’s for-each loop – for(Elementtype element : collection) – for (String note : noteBook)
  • Loops – Some types of loops in Java
    • for(i = 0; i < isbn.Length(); i++) {}
    • while (scanner.hasNext()) {}
    • do {} while (inChar != ‘X’);
    • for (String note : noteBook) {}

These are all great tools that can and should be used in programming. With such a huge library, languages such as Java are very powerful in the hands of programmers who learn many of the API classes and apply them correctly.

This was the final week for working on the assignment which is due tomorrow night. Its was a good learning curve although the testing requirements were quite irritating (screen dumps required I assume as an ineffective attempt to curb plagiarism?).

In the topic of loops and Java API, below is a snippet of code from one of my projects. I hope to look back on it and laugh at how low the quality is (although it does work seamlessly).
[cc lang=”Java”]
/**
Updates MySQL DB with new book fields
*/
public void updateBookDB()
throws SQLException, IOException, ClassNotFoundException
{
if (qArray.length == 0)
{
SimpleDataSource.init();
Connection conn = SimpleDataSource.getConnection();
try
{
java.sql.PreparedStatement
stat2 = conn.prepareStatement
(“DELETE FROM quotes WHERE book_ID = ?”);
stat2.setInt(1, bookID);
stat2.execute();
}
finally
{
conn.close();
}
}
else
{
String SQLCmd = “INSERT INTO quotes values “;
for (int a=0;a<(qArray.length – 1);a++)
{
SQLCmd = SQLCmd + “(” + bookID + “, 1, ” +
qArray[a][0] + “, ‘” +
qArray[a][1] + “‘, ‘” +
qArray[a][2] + “‘),”;
}
SQLCmd = SQLCmd + “(” + bookID + “, 1, ” +
qArray[qArray.length – 1][0] + “, ‘” +
qArray[qArray.length – 1][1] + “‘, ‘” +
qArray[qArray.length – 1][2] + “‘);”;
SimpleDataSource.init();
Connection conn = SimpleDataSource.getConnection();
try
{
java.sql.PreparedStatement
stat2 = conn.prepareStatement
(“DELETE FROM quotes WHERE book_ID = ?”);
stat2.setInt(1, bookID);
java.sql.PreparedStatement
stat3 = conn.prepareStatement(SQLCmd);
stat2.execute();
stat3.execute();
}
finally
{
conn.close();
}
}
[/cc]