CSC 216/s08/own career

From Expertiza_Wiki
Revision as of 02:59, 27 March 2008 by Willforamil (talk | contribs)
Jump to navigation Jump to search

Formatting Conventions

Java Code will look like this.

PseudoCode will look like this.

Commands will look like this.

Tip: Tips will look like this.

A string of menu commands will be marked as follows:
File => Save As

A string of keys will be marked as follows:
[Ctrl]+[Alt]+[Delete]

Instructive Graphics Will appear as follows:

http://d-site.net/stuff/java-wiki/java_logo.gif

Using Eclipse with JUnit for WebCAT

This activity will guide you through basic usage of the Eclipse IDE and JUnit for use with Webcat. Please have a computer available on which you may install Eclipse IDE. Also, please note that the following conventions will be used to draw attention to different parts of the article.

The problem

WebCAT is an software developed by Virginia Tech that allows you to submit your Java programs and have them graded in near-real-time. WebCAT tests your programs using something called JUnit. Eclipse is well integrated with JUnit, and provides an easy to use interface for evaluating your programs with JUnit. The challenge for this activity is to build a simple calculator that would pass the tests on WebCAT which would check the calculator using the following JUnit test cases:

public void testAdd() {
		Operations Calculation = new Operations(2, 2);
		assertEquals(Calculation.add(),4);
	}

public void testAddNegative() {
		Operations Calculation = new Operations(5, -2);
		assertEquals(Calculation.add(),3);
	}

Participation: Setting Up

For this activity, you can work with a partner. Either way, make sure you have a laptop with Eclipse available to you.

It is often useful to install Eclipse onto a Linux environment, because it more closely mirrors the actual testing environment where your code will be compiled. I won't go into details, but in particular, installing Ubuntu Linux, or one of its derivatives such as KUbuntu, is very easy. Note that here, you install JUnit as well.

On a Debian-based Linux such as Ubuntu, installing Eclipse should be as easy as

apt-get update
apt-get install eclipse junit

or

apt-get update
aptitude install eclipse junit

On an RPM based distribution such as Red Hat Linux or Fedora Core

yum install eclipse junit

should do the trick.

You can also install Eclipse graphically through the package manager, such as Synaptic, Adept, or simply what will be marked as "Manage Software","Manage Packages","Add and Remove Programs", or something similar in your menu.

Tip: On pure Debian, GCJ is installed instead of sun-java. Make sure to install sun-java and remove GCJ or Eclipse will not work quite right, and will likely frustrate you a great deal.

Windows and Mac OSX users will likely want to visit http://www.easyeclipse.org/site/home/ and download from there. EasyEclipse is just a pre-packaged version of Eclipse which will save you time and energy in the long run.

You can then install JUnit with the TPTP plugin: http://easyeclipse.org/site/plugins/eclipse-tptp.html


The Activity

We're assuming that your teacher has given you the following directions:

Create a simple calculator that will be graded by WebCAT. You calculator should have two classes, one that serves as a "Driver" and is called "Calculator" The other class will be called "Operations" and will have methods that perform various calculations on two numbers. You should construct a method in Operations.java called add() that takes no input but takes the sum of the numbers created with the instance of the Operations object. add() should return the resulting sum as an integer. The two numbers should be taken in from the command line, and if if no arguments are passed into the program at the time of execution, it should fail elegantly and produce an error. Points will be deducted for not having the appropriate classes, having the add() method inside Calculator.java, or if the program fails to display an error and exit when no command line input is given.the execution command should be formatted as follows:

java Calculator [int1] [int2]

Here is a simple pseudocode structure for the classes:

Calculator.java:


main($arguments){
	if($arguments < 2){
		Exit
	}else(CONTINUE){
		Create Operations Object($arguments) = $result
		Print "The sum is: " $result
	}
}

Operations.java:

constructor($num1, $num2){
	Store $num1, $num2
}
method add(){
	return $num1 + $num2
}

Remember, it should pass the test cases given in the problem above.


First, open Eclipse and make yourself at home.

Eclipse manages the different programs you write as "Projects."

Go to File => New => Project... to start a new Java project.

Look along the toolbar to find the small green button with the letter "c" in a circle. This is a small menu to create new files. You can use it to create a new class. Based on the directions, you'll want to create two.

Tip: Look in the directions for the names of the classes. If you name them wrong, it'll be difficult to fix later, so it's best to get them right the first time.

The two that you'll want to create are called Calculator.java and Operations.java. Note, however, that in the dialogue that Eclipse presents, you don't enter the .java suffix, and that you can have Eclipse automatically create a stub for the main() method.

Now, I'll give you the code for Calculator.java and Operations.java I recommend you type it, rather than copy and paste, so that you can see some of the features Eclipse has to help you program. If you want to copy a particular line or block, you can use the usual commands [ctrl]+[c] for copy, and [ctrl]+[v] for paste. If you're on a mac, you'll use [command] key instead of [ctrl]. As always, pay attention to your whitespace to keep your code neat and readable, and use comments to make it clear what each piece of code does.

Calculator.java:

public class Calculator {
	
	//Static numbers to work with:
	private static int num1;
	private static int num2;

	/**
	 * @author [Your Name]
	 * 
	 * Class that calls different operations
	 * on two numbers.
	 * 
	 */
	public static void main(String[] args) {
		
		// Check arguments passed in from command line
		int numberOfArguments = args.length;
		
		if(numberOfArguments != 2){
			System.out.println("To run the calculator, you must enter two numbers to be worked with.");
			System.exit(0);
		}else{
			num1 = Integer.parseInt(args[0]);
			num2 = Integer.parseInt(args[1]);
			System.out.println("Thank you. You have entered the numbers "+num1+" and "+num2+".");
		}
		
		Operations Calculation = new Operations(num1, num2);
		
		System.out.println("The sum is: "+Calculation.add());
	}