Goals for this Lab

  • Familiarize you with the Java development cycle
  • Familiarize you with Java constructs

Overview

If you have never programmed in Java, then this overview is for you. Programming in Java has several differences from Racket, python, C, and other languages. My assumption is that you have a working knowledge of C. It is similar to C, except that it is object oriented and has different ways of doing things like printing to the screen, reading in files, and operating on strings and booleans.

Like C, you can choose your favorite code editor for writing code, then you can separately compile and run your code.

When type your program, you type it into a file with a file extension .java and edit it there. The file must be a plain text file with just the letters and carriage returns. Every time you make changes, make sure you save the changes before recompiling. All files need to end with the extension .java. Java has a much more rigid way of dividing code into files and naming files than C does.

The next step is to compile the code into an bytecode that the Java Virtual Machinecan run. The compiler we'll be using is javac. If you compile the file Happy.java, it will produce a file named Happy.class. If it has a main function (called a method in object-oriented languages) in it, then you can run it by typing "java Happy"

If your program has multiple files (which it will), then to run it, you need to use the name of the class that has the main method in it.

Getting Started: Example Code

During lab, in order to practice editing and compiling, we are providing you a complete program, split into three important files. Go through each file, with the associated explanation, to get ready for lab.

Begin by reading through TestLab1.java and Lab1SampleFunctions.java. Puppy.java and a Makefile are also provided.

This code has been chosen especially to introduce you to not only the syntax and semantics of Java, but also norms we will expect in this class. Take note of:

  • The types in the input parameters and return values
  • What to do when erroneous input is received
  • Explanatory comment for each function
  • Inline comments for the useful function
  • Proper indentation
  • Examples of printing out with different types and different configurations
  • Separation of main / testing code from the lab code
  • Using command-line parameters to allow flexible tests
  • Packaging code into classes and using classes



To compile the code, run: javac TestLab1.java Lab1SampleFunctions.java Puppy.java
This will produce three bytecode outputs: TestLab1.class, Lab1SampleFunctions.class, and Puppy.class.

Testing Methodology

There are several general principles we will follow for testing.
1. Place your main in a separate file from your implementation so that you can swap out the main easily. 2. Create one test function for each function you're testing that takes in a single test case and tests it.
3. Create a set of test cases for each function that exercise the code well.

We'll illustrate those three principles below.
Principle 1: Place the main in a separate file.

Now that you have code written, you need to test it. This requires a main method. All Java programs begin in main. We are going to place this main function in a different file (and thus class) so that we can easily swap out test code for real code (or future lab code). The main function is in TestLab1.java.


Principle 2. Create one test function for each function you're testing that takes in a single test case and tests it.

Inside TestLab1.java, you will notice that the main method is last, and above it is a single method for each method we are testing (well, right now, it is just a single test method, which I have provided. You will need to write the rest of the test methods). Each test method takes in the input arguments. Inside the test method, it checks the number of arguments, converts them to the proper format, calls the method, and, if there is a return value, checks the return value.

If you look at main, you'll see that it looks at the first command-line argument and uses that to determine which method to test (and call the appropriate test method). In Java, unlike C, the executable name is not passed in to the program itself. The first number (at index 0) tells what test to run (printStuff vs returnAValue, etc.). That is followed by the inputs to give the method being tested. Finally, if the method has a return value, the last argument is the expected value so it can be compared to what the method returns.



Use the following command lines to test each method:
  • function: public void printStuff(int ival, float fval, char cval, string sval)
    How to run it: java TestLab1 0 5 6.3 c howdy
  • function: public int returnAValue(int ival)
    How to run it: java TestLab1 1 5 25 (5 is the input, 25 is the expected output)
  • function: public int factorial(uint n)
    How to run it: java TestLab1 2 3 6 (3 is the input, 6 is the expected output)
  • function: public int sumAges(Puppy p1, Puppy p2)
    How to run it: java TestLab1 3 3 6 9 (3,6 are the inputs, 9 is the expected output)


Principle 3. Create a set of test cases for each function that exercise the code well.

The last piece is to design and implement test cases. These functions are very simple, so no sophisticated test design is required. We test very small, degenerate cases (like adding or multiplying by 0), small cases that actually show the operation, and larger cases to verify that it works on two-digit or large numbers. In this testing methodology, the test cases can be placed in a separate file. I've placed mine in testlab1.txt

Week 1 Warmup Assignment

Now that you are familiar with how to code, compile, and run Java programs with rigorous testing methods, you are ready to start diving in and programming!

For this warm-up, you'll implement a single method in Lab1SampleFunctions.java and its testing code in TestLab1.java:
public uint dogYears(Puppy p1)

This calculates a dog's age in "dog years." So if a dog is 5 years old, it's actually 35 years old in "dog years" because dogs age 7x faster than humans.

The skeleton is already in the file - you need only fill in the code. Then you will need to go into TestLab1.java and implement a test function for it, following the convention created above. The code to test that function will be 4, and it will accept a single input parameter of the age of the puppy.