Results 1 to 4 of 4
  1. #1
    blaqkout is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default StableMatch - Gale Shapley algorithm aka the Stable Marriage

    I'm having some trouble getting my StableMatch program to work. Any advice is welcome.

    Here's the code in question:

    I'm getting Type mismatch: cannot convert from Person<E> to E.

    Java Code:
    // sets the name of the match for the Person provided
    	public void setMatch(Person<E> name){
    		match = name;
    	}
    and here's the class code

    Java Code:
    public abstract class Person <E>{
    
    	public E match;
    	private String fname;
    	protected List <E> preferences;
    	private String name;
    	
    	// Person Constructor
    	public Person (String aName){
    		name = aName;
    	}
    	
    	// sets name value for Person
    	public void setName(String aName){
    		name = aName;
    	}
    	
    	// returns the name of the Person object
    	public String getName(){
    		return fname;
    	}
    	
    	// returns the name of the Person
    	public String toString(){
    		if (this.preferences == null)
    			return match.toString();
    		else return ("" + match.toString() + preferences);
    	}
    	
    	// sets the name of the match for the Person provided
    	public void setMatch(Person<E> name){
    		match = name;
    	}
    	
    	// gets the match of the Person
    	public E getMatch(){
    		return match;
    	}
    	
    	// tells whether the Person is available for a match
    	public boolean isAvailable(){
    		if (this.getMatch() == null) return true;
    		else return false;
    	}
    	
    	// displays the match of the Person
    	public String displayMatch(Person<E> p){
    		if (p.getMatch() == null){
    			return p.fname + " is not currently matched";
    		}
    		else return p.fname + " is matched to "+ this.getMatch();
    	}
    	
    	public void initializePreferences(List < E > hospitals, int[]hospitalPreferences){
    		for (int i=0; i < hospitalPreferences.length; i++)
    			preferences.add(hospitals.get(hospitalPreferences[i]));	
    	}
    	
    	public void displayPreferences(){
    		System.out.println(this.name + "'s preferences are:");
    		Iterator<E> it = preferences.iterator();
    		while (it.hasNext())
    			System.out.print("\t" + it.next());
    		System.out.println();
    	}
    	public static void main (String args[]){
    		
    	}
    }
    Obviously, I'm a fairly novice programmer. Just looking for some help pointing me in the right direction.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: StableMatch - Gale Shapley algorithm aka the Stable Marriage

    The error message says what the problem is: cannot convert from Person<E> to E.
    You have a type mismatch in an assignment statement.

  3. #3
    blaqkout is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: StableMatch - Gale Shapley algorithm aka the Stable Marriage

    Yeah, I know that the type in the signature is Person <E> (generic) and that the variable "match" is of type E (generic object). Can I cast it or is there something fundamentally wrong with my approach? I want to set the match of the person. Based on my other code do you have any suggestions?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: StableMatch - Gale Shapley algorithm aka the Stable Marriage

    I want to set the match of the person
    What is the type of variable can be assigned to the match variable?
    match is type E
    name is type Person<E>

    Can you change the type of name?

Similar Threads

  1. Replies: 0
    Last Post: 04-21-2011, 09:51 AM
  2. Replies: 0
    Last Post: 03-10-2011, 08:34 AM
  3. Replies: 0
    Last Post: 08-17-2009, 03:59 PM
  4. Is Java 6 stable?
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 12:12 PM
  5. Stable Build: Eclipse 3.3M6
    By goldhouse in forum Java Software
    Replies: 0
    Last Post: 04-02-2007, 02:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •