Categories
Foundations of Programming

FIT9017: Week 6

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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
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&lt;(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();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *