Using java create the following program. The use of the Map, HashMap, and ArrayL
ID: 3589015 • Letter: U
Question
Using java create the following program.
The use of the Map, HashMap, and ArrayList data strutures are not allowed. You must use the data structures defined in the text.
The assignment is to write a console-based program to solve the Stable Matching Problem using the Gale-Shapley algorithm. You must use a linked list for the preference list of each man and woman. There are also numerous references to the Gale-Shapley algorithm on the Internet.
The input to the program will be a text file listing, in order,
the number n of men and women,
the names of the n men,
the names of the n women,
the list of preferences for each of the n men, and
the list of preferences for each of the n women.
For example, consider the following example of the contents of a file
----------------------------------------------------
5
Brian George John Robert Stephen
Anne Joyce Nancy Patricia Susan
// Preferences Men:
John: Susan Joyce Nancy Patricia Anne
Robert: Nancy Anne Joyce Susan Patricia
Brian: Patricia Susan Joyce Anne Nancy
Stephen: Joyce Anne Susan Nancy Patricia
George: Nancy Joyce Patricia Susan Anne
// Preferences Women:
Nancy: John Brian Stephen Robert George
Joyce: George John Stephen Robert Brian
Patricia: George Brian Robert Stephen John
Anne: George Stephen John Brian Robert
Susan: Brian George Stephen John Robert
----------------------------------------------------
The output will be the list of arranged marriages.
----------------------------------------------------
Marriages:
(Anne,Stephen)
(Joyce,George)
(Susan,John)
(Patricia,Brian)
(Nancy,Robert)
----------------------------------------------------
The program will operate as follows:
1.Ask the user for the name of the input file
2.Display the number of men and women, and the lists of men and women
3.Display the list of men’s preferences and the list of women’s preferences
4.Ask the user to select one of the following:
Men Propose
Women Propose
5.Ask the user for the go-ahead to apply the Gale-Shapley algorithm
6.Display the list of marriages.
The submission should include a printout of using the data given above.
Extra Credit: (30 points) Construct a GUI using JavaFX. The GUI must be defined in a separate class and follow the posted coding guidelines. You must declare all variables at the top of the class or method, and must not initialize variables in the declaration. (10 points for correctness of the GUI, 10 for the efficacy and attractiveness of the GUI, and 10 for following the coding guidelines nicely.)
Boys Girls A B CDEExplanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
public class StableMarriageTest {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
ArrayList<String> displayOnly = new ArrayList<String>(); // the contents of a text file stored here (just for display only)
LinkedList<String> JohnPreference = new LinkedList<String>();
LinkedList<String> RobertPreference = new LinkedList<String>();
LinkedList<String> BrianPreference = new LinkedList<String>();
LinkedList<String> StephanPreference = new LinkedList<String>();
LinkedList<String> GeorgePreference = new LinkedList<String>();
LinkedList<String> NancyPreference = new LinkedList<String>();
LinkedList<String> JoycePreference = new LinkedList<String>();
LinkedList<String> PatriciaPreference = new LinkedList<String>();
LinkedList<String> AnnePreference = new LinkedList<String>();
LinkedList<String> SusanPreference = new LinkedList<String>();
System.out.println();
System.out.println("Men:");
System.out.println("John");
System.out.println("Robert");
System.out.println("Brian");
System.out.println("Stephan");
System.out.println("George");
System.out.println();
System.out.println("Women:");
System.out.println("Nancy");
System.out.println("Joyce");
System.out.println("Patricia");
System.out.println("Anne");
System.out.println("Susan");
System.out.println();
System.out.println("Preferences for Men:");
System.out.println();
System.out.println("Person Pref 1 Pref 2 " + "Pref 3 Pref 4 Pref 5 John Susan "
+ "Joyce Nancy Patri Anne Robert Nancy Anne " + "Joyce Susan Patricia Brian Patri Susan"
+ " Joyce Anne Nancy Stephen Joyce Anne Susan "
+ "Nancy Patricia George Nancy Joyce Patri Susan Anne");
System.out.println();
System.out.print("Preferences for Women:");
System.out.println();
System.out.println();
System.out.println("Person Pref 1 Pref 2 Pref 3 Pref 4 "
+ "Pref 5 Nancy John Brian Stephen Robert George "
+ "Joyce George John Stephen Robert Brian " + "Patri George Brian Robert Stephen John "
+ "Anne George Stephen John Brian Robert " + "Susan Brian George Stephen John Robert");
//
System.out.print("Who should propose? Men or Woman: ");
String proposee = scanner.next();
System.out.println();
readLine("StableMatching", JohnPreference, 5);
readLine("StableMatching", RobertPreference, 6);
readLine("StableMatching", BrianPreference, 7);
readLine("StableMatching", StephanPreference, 8);
readLine("StableMatching", GeorgePreference, 9);
System.out.println( );
readLine("StableMatching", NancyPreference, 12);
readLine("StableMatching", JoycePreference, 13);
readLine("StableMatching", PatriciaPreference, 14);
readLine("StableMatching", AnnePreference, 15);
readLine("StableMatching", SusanPreference, 16);
System.out.println();
// finding couples
StableMarriage JohnCouple = new StableMarriage(RobertPreference, JohnPreference);
LinkedList<String> j = new LinkedList<String>();
j = JohnCouple.FindCouples(RobertPreference, JohnPreference);
System.out.println(j);
j.clear();
readLine("StableMatching", JohnPreference, 5);
System.out.println();
StableMarriage RobertCouple = new StableMarriage(JohnPreference, RobertPreference);
LinkedList<String> r = new LinkedList<String>();
r = RobertCouple.FindCouples(JohnPreference, RobertPreference);
System.out.println(r);
System.out.println();
StableMarriage BrianCouple = new StableMarriage(JohnPreference, BrianPreference);
LinkedList<String> b = new LinkedList<String>();
b = BrianCouple.FindCouples(JohnPreference, BrianPreference);
System.out.println(b);
System.out.println();
StableMarriage StephanCouple = new StableMarriage(JohnPreference, StephanPreference);
LinkedList<String> s = new LinkedList<String>();
s = StephanCouple.FindCouples(JohnPreference, StephanPreference);
System.out.println(s);
System.out.println();
StableMarriage GeorgeCouple = new StableMarriage(JohnPreference, GeorgePreference);
LinkedList<String> g = new LinkedList<String>();
g = GeorgeCouple.FindCouples(JohnPreference, GeorgePreference);
System.out.println(g);
System.out.println();
// finding marriages
StableMarriage JohnMarriage = new StableMarriage(j, r);
System.out.print("spouse is of " + j.getFirst() + ": " );
JohnMarriage.FindMarriages1Other(j,r);
StableMarriage BrianMarriage = new StableMarriage(b, r);
System.out.print("spouse of " + b.getFirst() + ": " );
JohnMarriage.FindMarriages1Other(b,r);
StableMarriage StephanMarriage = new StableMarriage(s, r, AnnePreference);
System.out.print("spouse of " + s.getFirst() + ": " );
JohnMarriage.FindMarriages2Other(s,r, AnnePreference);
StableMarriage RobertMarriage = new StableMarriage(r, g, NancyPreference);
System.out.print("spouse of " + r.getFirst() + ": " );
RobertMarriage.FindMarriages2Other(r,g, NancyPreference);
}
public static void AskFileName(String string) throws FileNotFoundException {
int i = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("You have 3 tries : ");
while (i <= 3) {
String FileName = scanner.next();
if (FileName != string) {
System.out.print("Incorrect file name. Try again.");
i++;
}
if (string == FileName) {
System.out.print("good");;
}
if (i == 3) {
System.out.print("Program terminated");
System.exit(0);
}
}
}
public static void readFileInput(String string, ArrayList<String> ArrayListName)throws FileNotFoundException{
Scanner stringFile = new Scanner(new File(string));
while(stringFile.hasNextLine()){
String line = stringFile.nextLine();
Scanner scanner = new Scanner(line);
while(scanner.hasNextLine()){
ArrayListName.add(scanner.nextLine());
}
scanner.close();
}
stringFile.close();
}
public static void readLine(String string, LinkedList<String> LinkedListName, int lineNumber) throws IOException{
String index = Files.readAllLines(Paths.get(string)).get(lineNumber);
for (String name : (index.split("[ :]+"))){
LinkedListName.add(name);
}
// System.out.println(LinkedListName);
}
public static void printFileInput(ArrayList<String> ArrayListName) throws FileNotFoundException{
Scanner scanner = new Scanner(new File("StableMatching"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
}
}
================================================================================
//StableMarriage.javA
import java.io.IOException;
import java.util.LinkedList;
public class StableMarriage {
LinkedList<String> Person1Couples = new LinkedList<String>();
LinkedList<String> Person2Couples = new LinkedList<String>();
LinkedList<String> ControlGroup = new LinkedList<String>();
StableMarriage(){
}
StableMarriage(LinkedList<String> Person1, LinkedList<String> Person2){
Person1Couples = Person1;
Person2Couples = Person2;
}
StableMarriage(LinkedList<String> Person1, LinkedList<String> Person2,LinkedList<String> NewControlGroup){
Person1Couples = Person1;
Person2Couples = Person2;
ControlGroup = NewControlGroup;
}
public static LinkedList<String> FindCouples (LinkedList<String> Person1Couples,LinkedList<String> Person2Couples){
for(int i = 1; i < Person1Couples.size(); i++){
if(Person1Couples.indexOf(Person1Couples.get(i)) > Person2Couples.indexOf(Person1Couples.get(i))){
continue;
}
if(Person2Couples.indexOf(Person1Couples.get(i)) > Person1Couples.indexOf(Person1Couples.get(i))){
Person2Couples.remove(Person2Couples.indexOf(Person1Couples.get(i)));
Person2Couples.add(i, "null");
}
// remove Person2's last interest if interest in Person1 has the same rank as in Person 2
if(Person1Couples.indexOf(Person1Couples.get(i)) == Person2Couples.indexOf(Person1Couples.get(i))){
Person2Couples.removeLast();
}
}
return Person2Couples;
}
public static void FindMarriages1Other(LinkedList<String> Person1Couples, LinkedList<String> Person2Couples){
LinkedList<String> MarriedCouples = new LinkedList<String>();
for (int i = 1; i < Person2Couples.size(); i++) {
if (Person1Couples.get(i) != "null") {
MarriedCouples.add(Person1Couples.get(i));
if(MarriedCouples.size() == 1){
System.out.println( MarriedCouples.getFirst());
}
}
else
continue;
}
}
// for people that have at least 2 others in their couples list
public static void FindMarriages2Other(LinkedList<String> Person1Couples, LinkedList<String> Person2Couples, LinkedList<String> Woman){
LinkedList<String> MarriedCouples = new LinkedList<String>();
for (int i = 1; i < Person2Couples.size(); i++) {
if( ((Person1Couples.get(i)!= "null") && Person2Couples.get(i) != "null" )){
if(Woman.indexOf(Person1Couples.get(0)) < Woman.indexOf(Person2Couples.get(0)))
MarriedCouples.add(Person1Couples.get(i));
if(MarriedCouples.size() == 1)
System.out.println( MarriedCouples.getFirst());
}
if(Person1Couples.get(i) != Person1Couples.get(i))
continue;
else
continue;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.