Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I am having an issue with my program. For this program, I am supposed to be utilzing the gale shapley algorithm. The program has

image text in transcribedimage text in transcribedimage text in transcribedHello, I am having an issue with my program. For this program, I am supposed to be utilzing the gale shapley algorithm. The program has to be man optimal and written in java. The issue that i am having is that I have a mistake somewhere in my program but i cannot find it. Right now the output of my program will say the correct amount of guys but keeps saying the same woman is matched with them all. I'll include my code down below. Some more information is that I created a class named person and two sub class (boys and girls), boys store their preferences in a linked list meanwhile girls store their preferences in an array. import java.util.*; import java.io.*; public class stable { public static void main(String[] args) { Scanner matchMaker = new Scanner(System.in); System.out.println(" How many men are being matched this session?"); int max = matchMaker.nextInt(); boys[] boysArray = new boys[max]; girls[] girlsArray = new girls[max]; //This array will store the girls that are matched with a specific guy. The guy will // be reflected using the index of the array. An example of this would be if girl #5 is stored in // index 1, then girl #5 is paired with man #1. int[] matchingPairs = new int[max]; //This for loop will get the preferences of the men from 1 to max. for(int i = 1; i = 1; i--) { unmatchedGuys.push(i); } //This loop will implement the Gale-Shapley Algorithm and produce a stable matching //amongst the soon to be couples. The algorithm will be male optimal. while(unmatchedGuys.empty() == false) { int x = unmatchedGuys.pop(); boys boytestSubject = new boys(x); boytestSubject = boysArray[x - 1]; boytestSubject.inRelationship = false; girls girlTestSubject; LinkedList y = new LinkedList(); y = boytestSubject.getDudesPref(); while(boytestSubject.matchedUp() == false) { int nextPref = y.getFirst(); girlTestSubject = new girls(nextPref, max); girlTestSubject.Galspreferences = girlsArray[nextPref - 1].getGalspreferences(); if(girlTestSubject.galMatchUp() == false) { boytestSubject.statusChange(); girlTestSubject.statusChange(); matchingPairs[boytestSubject.name] = girlTestSubject.name; //Changing the arrays to match what has changed boysArray[boytestSubject.name - 1] = boytestSubject; girlsArray[girlTestSubject.name - 1] = girlTestSubject; } else { int currentHusband = 0; for (int i = 0; i

}

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

10 /** 240 */ 4 package Assignment1; 5 60 /** 7 8 9 310 110 /* The person class contains two parameters: 12 * 1.) An integer that will store the associated number of individuals that are being used 13 * within the Gale-Shapely algorithm and when that specific person is introduced. * 15 16 public class person 17 { 318 int name; 019 Boolean inRelationship; 20 210 /* 22 * This constructor will set the name of a person equal to x. 23 */ person(int x) 225 { 226 setName(x); 27 inRelationship = false; 28 } 29 300 /** 31 * @return the name 32 */ 330 public int getName() { 134 return name; 135 } 36 370 /** 38 * @param name the name to set 39 */ $400 public void setName(int name) { 341 this.name = name; } 43 2440 /** 345 * @return the inRelationship 846 */ 3470 public Boolean getInRelationship() { 48 return inRelationship; 49 } 150 151 stable.java Dboys.java X D girls.java person.java CheckingAccount.java 10 /**0 4 package Assignment1; 5 6 import java.util.LinkedList; 7 80 /** 9 * @author WHEELERJA16 19 11 */ 12 public class boys extends person 13 { 14 15 LinkedList dudesPref = new LinkedList(); 16 170 public boys(int x) 18 { 19 super(x); 20 } 210 /** 22 * @return the dudesPref 23 */ 240 public LinkedList getDudesPref() { 25 return dudesPref; 26 } 270 /** 28 * @param dudesPref the dudesPref to set 29 300 public void setDudesPref(int x) { 31 this.dudesPref.add(x); 32 } 33 340 public void statusChange () 35 { 36 this.inRelationship = true; 37 } 38 39 // This class will return the relationship status of any male. 400 public boolean matchedup() 41 { 42 if (this.inRelationship = true) 43 { 144 return true; 45 } 446 return false; 47 } 148 48 4490 public void displayInfo() 50 { 51 System.out.printf("This is guy #%d ", this.name); 52 System.out.printf("His preferences are: "); 53 System.out.println(dudesPref); 54 } 55 } dudesPref = new LinkedList(); 16 170 public boys(int x) 18 { 19 super(x); 20 } 210 /** 22 * @return the dudesPref 23 */ 240 public LinkedList getDudesPref() { 25 return dudesPref; 26 } 270 /** 28 * @param dudesPref the dudesPref to set 29 300 public void setDudesPref(int x) { 31 this.dudesPref.add(x); 32 } 33 340 public void statusChange () 35 { 36 this.inRelationship = true; 37 } 38 39 // This class will return the relationship status of any male. 400 public boolean matchedup() 41 { 42 if (this.inRelationship = true) 43 { 144 return true; 45 } 446 return false; 47 } 148 48 4490 public void displayInfo() 50 { 51 System.out.printf("This is guy #%d ", this.name); 52 System.out.printf("His preferences are: "); 53 System.out.println(dudesPref); 54 } 55 }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions