Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io.File; Can someone fix the errors that I'm getting in this java code . Thank you. import java.io.FileNotFoundException; import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays;

import java.io.File; Can someone fix the errors that I'm getting in this java code . Thank you.

import java.io.FileNotFoundException; import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; public class Project1 { private static class Marriage { private int n; private Person[] men; private Person[] women; private Marriage(int n) { this.n = n; this.men = new Person[n]; this.women = new Person[n]; for (int i = 0; i < n; ++i) { this.men[i] = new Person(true); this.women[i] = new Person(false); } } private void addRank(Person p, Person r, int n) { p.addRank(r, n); } private void getEngagements() { ArrayList freeMen = new ArrayList(); for (int i = 0; i < this.n; ++i) { freeMen.add(this.men[i]); } while (freeMen.size() > 0) { Person man = freeMen.remove(0); Person woman = man.getNextPossiblePartner(); Person currEngagement = woman.getEngagement(); if (currEngagement == null) { man.setEngagement(woman); woman.setEngagement(man); } else { if (currEngagement.is BetterThan(man, woman)) { freeMen.add(currEngagement); currEngagement.setEngagement(null); man.setEngagement(woman); woman.setEngagement(man); } else { freeMen.add(man); } } } } private void getEngagementsWomen() { ArrayList freeWomen = new ArrayList(); for (int i = 0; i < this.n; ++i) { freeWomen.add(this.women[i]); } while (freeWomen.size() > 0) { Person woman = freeWomen.remove(0); Person man = woman.getNextPossiblePartner(); Person currEngagement = man.getEngagement(); if (currEngagement == null) { man.setEngagement(woman); woman.setEngagement(man); } else { if (currEngagement.isBetterThan(man, woman)) { freeWomen.add(currEngagement); currEngagement.setEngagement(null); man.setEngagement(woman); woman.setEngagement(man); } else { freeWomen.add(woman); } } } } private String toString() { StringBuilder out = new StringBuilder(); for (int i = 0; i < n; ++i) { out.append(this.men[i].toString() + " "); } for (int i = 0; i < n; ++i) { out.append(this.women[i].toString() + " "); } return out.toString(); } public void printEngagements() { System.out.println("Output when men propose: "); System.out.println(this.toString() + " "); System.out.println("Output when women propose: "); this.getEngagementsWomen(); System.out.println(this.toString()); } public static class Person { private boolean isMan; private ArrayList ranks; private Person engagement; private int currRank; public Person(boolean isMan) { this.isMan = isMan; this.ranks = new ArrayList(); this.engagement = null; this.currRank = 0; } public void addRank(Person p, int n) { if (this.isMan) { this.ranks.add(p); } else { this.ranks.add(n - 1, p); } } public Person getEngagement() { return this.engagement; } public void setEngagement(Person p) { this.engagement = p; } public Person getNextPossiblePartner() { return this.ranks.get(this.currRank); } public void moveUp() { ++this.currRank; } public boolean isBetterThan(Person p1, Person p2) { if (this.isMan) { return this.ranks.indexOf(p1) < this.ranks.indexOf(p2); } else { return this.ranks.indexOf(p2) < this.ranks.indexOf(p1); } } public String toString() { StringBuilder out = new StringBuilder(); out.append("{"); for (int i = 0; i < this.ranks.size(); ++i) { out.append("(" + (this.isMan ? "m" : "w") + (i + 1) + ", " + (this.isMan ? "w" : "m") + (this.ranks.indexOf(this.engagement) + 1) + ")"); if (i != this.ranks.size() - 1) { out.append(", "); } } out.append("}"); return out.toString(); } } } public static void main(String[] args) throws FileNotFoundException { File file = new File(args[0]); Scanner sc = new Scanner(file); int n = Integer.parseInt(sc.nextLine()); Marriage marriage = new Marriage(n); for (int i = 0; i < n; ++i) { String[] ranks = sc.nextLine().split(" "); for (int j = 0; j < n; ++j) { marriage.addRank(marriage.men[i], marriage.women[Integer.parseInt(ranks[j]) - 1], n); } } for (int i = 0; i < n; ++i) { String[] ranks = sc.nextLine().split(" "); for (int j = 0; j < n; ++j) { marriage.addRank(marriage.women[i], marriage.men[Integer.parseInt(ranks[j]) - 1], n); } } marriage.getEngagements(); marriage.printEngagements(); } }

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_2

Step: 3

blur-text-image_3

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

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions