View Single Post
Old 09-12-2012, 10:02 PM   PM User | #1
trantommyd
New Coder

 
Join Date: Mar 2012
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
trantommyd is an unknown quantity at this point
Problem with .equals()

I can't figure out why my program thinks that the integer 1 is equal to integer 2 (and integer 3 and so on). What am I overlooking here?

Code:

import java.lang.*;

public class MyFour<T>
{
	private T item1, item2, item3, item4;
	private static boolean same;
	private static String word = "word";
	
	// "it's contructor recieves values for setting all four items." 
	public MyFour(T i1, T i2, T i3, T i4)
	{
		item1 = i1;
		item2 = i2;
		item3 = i3;
		item4 = i4;	
	}

	//a method that returns true if all 4 of the parameters are the same
	public static boolean allEqual(MyFour ex)
	{
		same = false;
		
		if((ex.item1).equals(ex.item3)); 
		{
			same = true;
		}
		
		System.out.println(ex.item1);
		System.out.println(ex.item3);
		
		return same;
	}
	
	//a method that shifts all the items up one position 
	public void shiftLeft(T i1, T i2, T i3, T i4)
	{
		T one, two, three, four;
		
		one = i2;
		two = i3;
		three = i4;
		four = i1;
	}
	
	public static void toString(MyFour ex)
	{
		String a = ex.item1.toString();
		String b = ex.item2.toString();
		String c = ex.item3.toString();
		String d = ex.item4.toString();
					
		System.out.println("(" + a + ", " + b + ", " + c + ", " + d + ")");
	}
	
	public static void main(String[] args)
	{
		MyFour<String> ex = new MyFour<String>(word, word, word, word);
		toString(ex);
		System.out.println(allEqual(ex));
		
		MyFour<Integer> ex2 = new MyFour<Integer>(1, 2, 3, 4);
		toString(ex2);
		System.out.println(allEqual(ex2));
	}
}
trantommyd is offline   Reply With Quote