הלינקייה: מגזין חודשי למפתחים

רוצה לשמוע על כל האירועים, המדריכים, הקורסים והמאמרים שנכתבו החודש ?
הלינקייה הינו מגזין חופשי בעברית שמשאיר אותך בעניינים.
בלי ספאם. בלי שטויות. פעם בחודש אצלך בתיבה.

Basic Syntax

We'll start by writing a new class called Person. We create a new class in eclipse by right-clicking src folder, selecting new -> Java Class

/*
 * A Person Class represents a Person entity in our system. 
 * Each person can handle two messages: getAge() and age(). 
 * We use Person objects to keep track of the ages of our community members, calling the age() method every year.
 */
 
public class Person {
	int age;
 
	/**
	 * A parameterized constructor is used to force each Person object created to have an age
	 * @param age
	 */
	public Person(int age) {
		this.age = age;
	}
 
	/**
	 * The getAge() method returns the age field of the object
	 * @return person's age
	 */
	public int getAge() {
		return this.age;
	}
 
	/**
	 * The age() method makes our person one year older.
	 */
 
	public void age() {
		this.age++;
	}
 
	/**
	 * The main method is our primary entry point to our Java program
	 * Code starts to execute here
	 * In this case, we only say hello
	 * @param args
	 */
	public static void main(String [] args) {
		System.out.println("Hello World");
	}	
}

When we have our class ready, we need to create a test for it. We'll use JUnit as our testing framework.
To create a new Test Case in JUnit, we right-click on the src folder, and select new -> JUnit Test Case

// importing some code from JUnit to let us automate testing process
import junit.framework.TestCase;
 
/**
 * A TestPerson is test class for Person. 
 * Every JUnit Test Class needs to extend TestCase, and write the testing method
 * it uses to test the specific class
 * 
 * In JUnit, each method named testSomething (i.e. the prefix is test) is treated as
 * a test code for our class, and will execute when we choose to test it in Eclipse.
 * @author ynonperek
 *
 */
 
public class TestPerson extends TestCase {
 
	/**
	 * We start with a small example of a test method. 
	 * testMulti will create two variables: x and y, and multiply them. 
	 * 
	 * The final assertEquals line tells JUnit that the test will succeed if the result
	 * equals 30.
	 */
 
	public void testMulti() {
		int x = 10;
		int y = 3;
 
		assertEquals(x * y, 30);
	}
 
 
	/**
	 * So now that we know how to test, let's examine a real test mehtod for our Person class
	 * This time we test the aging process. We create a new Person using the constructor
	 * at age 10, then we age it by one year and check the result
	 * This test succeeds if its age after aging is now 11.
	 */
 
	public void testAging() {
		Person p = new Person(10);
		p.age();
 
		assertEquals(p.getAge(), 11);
	}
 
	/**
	 * This method is not executed as part of the testing process, for
	 * its name does not start with a test
	 */
	public void justSaying() {
		System.out.println("Hello");
	}
}

The Rectangles Game

/**
 * The game board is the main entry point for the game
 * It initializes player's status and manages game flow
 *
 */
public class GameBoard {
	private Player p1, p2;
 
	/**
	 * A constructor that creates the two players
	 */
	public GameBoard() {
		this.p1 = new Player("Jimy");
		this.p2 = new Player("Joe");
	}
 
	/**
	 * Play gets the two rectangles and checks game rules to find the winner
	 */
	public void play() {
		Rectangle r1 = p1.play();
		Rectangle r2 = p2.play();
 
		if ( (! r1.isLegal() ) && ( ! r2.isLegal() ) ) {
			System.out.println("Both players lose -> Illegal rectangels");
			return;
		}
 
		if ( ! r1.isLegal() ) {
			System.out.println(p2.getName() + " Wins !. " + p1.getName() + " gave an illegal rectangle");
			return;
		}
 
		if ( ! r2.isLegal() ) {
			System.out.println(p1.getName() + " Wins !. " + p2.getName() + " gave an illegal rectangle");
			return;
		}
 
		if ( r1.containsOther(r2)) {
			System.out.println(p2.getName() + " Wins !. " + p1.getName() + "'s Rectangle contains it");
			return;
		}
 
		if ( r2.containsOther(r1)) {
			System.out.println(p1.getName() + " Wins !. " + p2.getName() + "'s Rectangle contains it");
			return;
		}
 
		if ( r1.isLargerThan(r2)) {
			System.out.println(p1.getName() + " Wins !. She had the larger rectangle");
			return;
		} 
 
		if ( r2.isLargerThan(r1)) {
			System.out.println(p2.getName() + " Wins !. She had the larger rectangle");
			return;
		} 
		System.out.println("A Draw. Nobody won...");
	}
 
	public static void main(String [] args) {
		GameBoard gb = new GameBoard();
		gb.play();
	}
}
/**
 * Each player object has a unique name.
 * A Player responds to a play message by
 * returning a valid rectangle. 
 * 
 * In our case, we use a predefined rectangle 
 * for simplicity
 *
 */
 
public class Player {
	private String name;
 
	public Player(String name) {
		this.name = name;
	}
 
	/**
	 * play method creates a new rectangle and returns
	 * it. The rectangle dimensions are based on the 
	 * player's name. If it has an even number of characters
	 * we'll use the larger rectangle, while an odd number
	 * of character will lead to the smaller one.
	 */
 
	public Rectangle play() {
		if (name.length() % 2 == 0) { 
			return new Rectangle(10, 10, 90, 90);
		} else {
			return new Rectangle(1, 2, 10, 200);
		}
	}
 
	/**
	 * A getter for the name field
	 * @return player's name
	 */
	public String getName() {
		return this.name;
	}
}
/**
 * A Rectangle does most of the work in this game. 
 * It can respond to messages asking for its legality, and its relations
 * wiht other rectangles
 * Each rectangle in our system is represented by four floats
 * Two for the top left corner, and two more for the bottom right
 * corner
 *
 */
 
public class Rectangle {
	private float tlx;
	private float tly;
 
	private float brx;
	private float bry;
 
	/**
	 * The parameterized constructor lets us create rectangles. 
	 * It notifies anyone using this class that any objects created
	 * from this class must have the following member fields
	 * @param tlx
	 * @param tly
	 * @param brx
	 * @param bry
	 */
	public Rectangle(float tlx, float tly, float brx, float bry) {
		this.tlx = tlx;
		this.tly = tly;
 
		this.brx = brx;
		this.bry = bry;
	}
 
	/**
	 * The isLegal method verifies three assumptions about the rectangle
	 * 1. All of its coordinates are be positive
	 * 2. Top left corner is more left than bottom right corner
	 * 3. Top left corner is upper to the bottom right corner
	 * @return
	 */
	public boolean isLegal() {
		if (! ( (tlx > 0) && ( tly > 0 ) && ( brx > 0 ) && ( bry > 0 ) ) ) {
			return false;
		}
 
		if ( tlx > brx ) {
			return false;
		}
 
		if ( tly > bry ) {
			return false;
		}
 
		return true;
	}
 
	public float getHeight() {
		return (bry - tly);
	}
 
	public float getWidth() {
		return (brx - tlx);
	}
 
	/**
	 * isLargerThan compares two rectangles to see if one is larger than the other
	 * A larger rectangle is one that has a larger perimeter AND a larger area
	 * @param other
	 * @return
	 */
	public boolean isLargerThan(Rectangle other) {
 
		float perimeter = getHeight() * 2 + getWidth() * 2;
		float area = getHeight() * getWidth();
 
		float perimeterOther = other.getHeight() * 2 + other.getWidth() * 2;
		float areaOther = other.getHeight() * other.getWidth();
 
		return ( ( perimeter > perimeterOther ) && ( area > areaOther ) ); 
	}
 
	/**
	 * containsOther checks if one rectangle contains another one.
	 * @param other
	 * @return
	 */
	public boolean containsOther(Rectangle other) {
		return ( 	( other.tly > this.tly ) &&
					( other.bry < this.bry ) && 
 
					( other.tlx > this.tlx ) && 
					( other.brx < this.brx ) );
	}
}
 
import junit.framework.TestCase;
 
/**
 * A test class for Rectangles
 */
public class RectangleTest extends TestCase {
 
	/**
	 * This test validates isLargerThan method of rectangle
	 * by comparing three predefined rectangles
	 */
	public void testLarger() {
		Rectangle r1 = new Rectangle(10, 10, 50, 50);
		Rectangle r2 = new Rectangle(5, 5, 7, 7);
		Rectangle r3 = new Rectangle(32, 33, 88, 90);
 
		assertTrue( r3.isLargerThan(r1) );
		assertTrue( r3.isLargerThan(r2) );
		assertFalse( r3.isLargerThan(r3) );
 
		assertFalse( r2.isLargerThan(r1) );
		assertFalse( r2.isLargerThan(r2) );
		assertFalse( r2.isLargerThan(r3) );
 
		assertFalse( r1.isLargerThan(r1) );
		assertTrue( r1.isLargerThan(r2) );
		assertFalse( r1.isLargerThan(r3) );
	}
 
	// TODO implement this test
	public void testContains() {
 
	}
 
	// TODY implement this test
	public void testLegal() {
 
	}
}
 
course: