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