- Use Objects
- Demonstrate inheritance
- Introduce build systems
- Introduce unit testing and Test Driven Development (TDD)
- Introduce the project structure for the labs
This assignment is intended to serve as a warmup assignment. It may have been a long time since you have written any code in Java, so we will get warmed up by writing Hello World enterprise edition™! We are no longer satisfied with a simple hello world, we must make it as complex as possible, that way we will ensure job security for years to come!
This assignment will introduce you to how all of our assignments will be structured this semester. We will also introduce Test Driven Development (TDD) where we will write all of our tests first before we write our code.
Here is a traditional hello world in Java.
class Hello{
public static void main(String [] args){
System.out.println("needs more cowbell");
}
}We can compile and run this simple example as follows:
$ javac Hello.java
$ java Hello
needs more cowbellBoo!! Boring! This is not enterprise grade software.
Lets spice this up. We need to have a more robust Hello World that is enterprise grade. Enterprise grade software at a minimum needs a well defined build system, testing, and some unnecessary abstraction to ensure long term job security. The next few sections will walk you through setting this all up which you will then replicate on your own system.
Let's formally introduce a build system so we can build, run, and debug our code just like you would do in industry.
There are quite a few build systems that were specifically designed for java. By far the top three are:
- ant - Released in 2000. Designed as a replacement for make. Still in use but has largely been replaced by newer tools such as maven and gradle.
- maven - Released in 2004. Notable for managing both the build process and dependencies.
- gradle - Released in 2008. One of the newer build systems for Java. Adopted by google as their official build system for Android.
We will be using gradle because it is relatively new and well supported. The gradle docs explain how to organize a project. We will follow the recommended layout because Gradle's language plugins establish conventions for discovering and compiling source code that we can take advantage of.
We will be using a popular testing framework called TestNG to test our
code. We will write some tests for our Hello World Enterprise Edition™ to ensure that everything
works as expected. Every class (except App.java) will need to have a test class. Unlike CS121 where
you may have spent most of your time in the class that has the main method, the App.java class
will not do much except create your other objects. 99% of your program logic will be in classes
outside of main.
.
├── README.md
├── app
│ ├── build.gradle
│ └── src
│ ├── main
│ │ └── java
│ │ └── lesson
│ │ ├── App.java
│ │ ├── SayHello.java
│ │ ├── SayWorld.java
│ │ └── Talker.java
│ └── test
│ └── java
│ └── lesson
│ ├── SayHelloTest.java
│ └── SayWorldTest.java
├── gradle
│ └── wrapper
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradleThe starter code does not build out of the box. So the first step is to write as little code as possible to get everything building. We will just stub out methods and classes at this point and not worry about implementation.
We are going to be practicing TDD all semester! So the first step in any project is to write your tests! There are two test files stubbed out and ready for you to implement. Look in the test directory to find the classes SayHelloTest.java and SayWorldTests.java. Complete the test methods before you go on to the next task.
After you have completed Task #1 you should have two tests that are failing! That is because we
now need to actually implement the classes that provide our application logic. There are four files
in the app/src/main/java/lesson/ directory. Your job is to complete the SayHello.java and
SayWorld.java classes so they return the appropriate strings.
Once you have completed all the tasks open the file Retrospective.md and complete each section with a TODO comment.
Once you are finished you need to make sure that you have pushed all your code to GitHub for grading!
Generally speaking, unless you are explicitly told,you are not permitted to change the build system (gradle), move the files around, rename classes, or change interfaces. I have marked all the code that can not be changed with comments. Your instructor needs the directory structure and file names to be consistent from student to student in order to get grades back in a timely manner. If you do make changes that impact grading you will be awarded 0 points and given a chance to fix your code.
While it is true that there is some creativity when programming you still have to follow basic APIs that were defined years if not decades ago. The assignments have been crafted in a particular way to ensure you meet the stated learning objectives.
If you get command 'XXX' not found error, mostly it's caused by the Java Language server failing
to activate. More information can be found on the redhat-developer tool
wiki. You can clean the
workspace directory in the following way:
- Open the command palette (F1)
- select Java: Clean the Java Language Server Workspace
- select Restart and delete from the confirmation prompt