Your Computer Science Resource

Archive for January, 2009

Jan
12th

HOW-TO: Evaluate a Mathematical String Using ScriptEngine

Suppose you have a String containing some mathematical operation(s). For example, we need to perform the following calculation, but for various reasons, the data type is a String: (10*(34+92)-23)/4. Rather than figuring out ways to parse it (e.g. split or some fancy regex), we can use Java’s ScriptEngine to evaluate the function as is.

(more…)

Jan
10th

HOW-TO: Read a Text File Using BufferedReader

As simple as this may seem to experienced Java programmers, I still see many posts in the Java Forums from beginners asking this very question: How do you read a text file line-by-line using a BufferedReader? Let’s find out.

(more…)

Jan
9th

PROJECT: Screen Capture Utility

Using my previous posts on how to take a screenshot and how to save an image to disk, I’ve created a Screen Capture Utility extending these basic ideas to create a program that would automate the process of taking multiple screenshots over a period of time. Full source code is available for download here.

(more…)

Jan
9th

HOW-TO: Save a BufferedImage to Disk

Recently, I showed you how to take a screenshot and display it in a JFrame using Java’s Robot class. What if you wanted to save the image to the disk as a GIF, JPG, or PNG? It’s actually really easy. Let’s find out how!

(more…)

Jan
5th

HOW-TO: Save JTable to Excel Spreadsheet using Apache POI

Have you ever needed to save the contents of a JTable? One convenient way to save this data is to make it available as a Microsoft Excel spreadsheet. At first, this may not seem like an easy task, but with Apache’s POI API, we can do it without too much frustration.

(more…)

Jan
4th

PROJECT: Long Integer ADT

Data types in Java and C++ (amongst other languages) are limited to a specified range. Even in most basic calculators, performing an operation with 9 or more digits will result in an error. Sometimes, though, you have to perform very large calculations. Wouldn’t it be nice if there were an ADT that can perform arbitrary calculations, and “grow” depending on the size of the number? Of course, Java has a class library that will do this for you (BigInt), and there is almost certainly one available in C++, as well. But what if you had to develop your own? Of course, this would mean you’d have to re-implement basic mathematical operations — which may turn out to be not so “basic” at all. In this project, I’ve developed a “Long Integer ADT” in C++. Because the code for this project is somewhat extensive, I will only post the header files and smaller source files. The full source code can be downloaded here.

(more…)