You have been hired by the UCMerced Dorm Life Council to prepare a program which
ID: 3695882 • Letter: Y
Question
You have been hired by the UCMerced Dorm Life Council to prepare a program which measures compatibility between potential roommates. You are given a file with the list of students. For each student, you are given their name, gender and birthday. You will also have their preference for quiet time, music, reading and chatting. Using this information, you can then determine the compatibility between two people for rooming together, according to a formula developed by the Dorm Life Council. You will need to create four classes in order to do this project: Match.java, Student.java, Date.java and Preference.java.
• Create an array of Students (max = 100)
• Read from a text file all the students information (tab delimited)
o Name (String)
o Gender (char)
o Date (String: Month-Day-Year, - delimited)
o Quiet Time (int 0-10)
o Music (int 0-10)
o Reading (int 0-10)
o Chatting (int 0-10)
• For each line (in the text file)
o Extract the information for each student
o Create a Student pointed to be an entry in the array
o Keep a count of actual number of students (less than 100)
• For each Student (after completely done reading)
o Check against every other student (assuming they are not matched already) their compatibility scores
o Find the Best Score and Best Match person
o Match the two roommates up and prints out the result
Note: Your algorithm for matching should work something like this:
Foreach student NOT currently matched
Foreach rest of students NOT currently matched
currentScore = studentA.compare(studentB)
if the currentScore is better than MaxScore
bestMatchStudent is student
bestMatchScore is currentScore
studentA is now Matched bestMatchStudent is now Matched
Also note that you have an array of students presumably. So studentA.compare(studentB) will actually look like this:
if(!students[j].getMatched) // student not matched already
currentScore = students[i].compare(students[j]);
Explanation / Answer
Match.java
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Match {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.print("Input location of file: ");
String filename = kbd.next();
kbd.close();
ArrayList<Student> student = new ArrayList<Student>();
int i = 0;
try {
Scanner input = new Scanner ( new FileReader(filename) );
while (input.hasNextLine()) {
input.useDelimiter("[ -]");
Character gender = 'M';
String valueAfter;
int quiet, music, reading, chatting, day, month, year;
String name = input.next();
valueAfter = input.next();
if (valueAfter.length() > 1){
name = name + " " + valueAfter;
}
else{
gender = valueAfter.charAt(0);
}
month = input.nextInt();
day = input.nextInt();
year = input.nextInt();
quiet = input.nextInt();
music = input.nextInt();
reading = input.nextInt();
chatting = input.nextInt();
student.add(i, new Student(name, gender, new Date(year, month, day), new Preference(quiet, music, reading, chatting)));
i++;
}
input.close();
} catch ( NoSuchElementException e){
System.out.println(e);
} catch (FileNotFoundException e) {
System.out.println(e);
}
int bestMatchStudent = 100;
int bestMatchScore = 0;
int j =0;
for (i = 0; i < student.size(); i++){
if(!(student.get(i).GetMatched())){
for (j = i+1; j < student.size(); j++){
if(!(student.get(j).GetMatched())){
int currentScore = student.get(i).Compare(student.get(j));
if (currentScore > bestMatchScore){
bestMatchScore = currentScore;
bestMatchStudent = j;
}
}
}
student.get(bestMatchStudent).SetMatched();
student.get(i).SetMatched();
if(bestMatchScore < 50){
System.out.println(student.get(i).Name() + " has no matches");
}
else {
System.out.println(student.get(i).Name() + " matches with " + student.get(bestMatchStudent).Name() + " with score " + bestMatchScore);
bestMatchScore = 0;
}
}
}
}
}
Date.java
public class Date {
private int year, month, day;
public Date (int y, int m, int d){
this.year = y;
this.month = m;
this.day = d;
}
public int compare (Date dt) {
int difference;
difference = ((dt.dayOfYear()+(365*dt.year))-(this.dayOfYear()+(365*this.year)))/30;
if(difference > 60)
return 60;
else
return difference;
}
public int year() {
return year;
}
public int month() {
return month;
}
public int day() {
return day;
}
public int dayOfYear() {
int totalDays = 0;
switch (month) {
case 12: totalDays += 30;
case 11: totalDays += 31;
case 10: totalDays += 30;
case 9 : totalDays += 31;
case 8 : totalDays += 31;
case 7 : totalDays += 30;
case 6 : totalDays += 31;
case 5 : totalDays += 30;
case 4 : totalDays += 31;
case 3 : totalDays += 28;
case 2 : totalDays += 31;
}
totalDays += day;
return totalDays;
}
}
Preference.java
public class Preference {
private int quietTime, music, reading, chatting;
public Preference(int q, int m, int r, int c){
this.quietTime = q;
this.music = m;
this.reading = r;
this.chatting = c;
}
public int compare (Preference pref) {
int diff;
diff = (Math.abs(this.chatting - pref.chatting)+ Math.abs(this.reading - pref.reading) + Math.abs(this.music - pref.music) + Math.abs(this.quietTime - pref.quietTime));
return diff;
}
public int quietTime() {
return quietTime;
}
public int music() {
return music;
}
public int reading() {
return reading;
}
public int chatting() {
return chatting;
}
}
Student.java
public class Student {
protected String student;
protected Character gender;
Date birthDay;// = new Date();
Preference pref;// = new Preference();
protected Boolean matched = false;
public Student (String s, char g, Date b, Preference p){
student = s;
gender = g;
birthDay = b;
pref = p;
}
public String Name() {
return student;
}
public char Gender() {
return gender;
}
public Date Date(){
return birthDay;
}
public Preference Pref() {
return pref;
}
public boolean GetMatched() {
return matched;
}
public void SetMatched() {
matched = true;
}
public int Compare(Student st) {
int compatibility;
int ageDifference = (this.birthDay.compare(st.birthDay));
int preferences = (this.pref.compare(st.pref));
compatibility = ((40-preferences) + (60-ageDifference));
if(this.gender == 'M' && st.gender == 'F')
return 0;
if (this.gender == 'F' && st.gender == 'M')
return 0;
return compatibility;
}
}
input.txt
Abhay F 1-5-1994 0 0 0 0
Adam M 2-6-1994 0 0 0 0
Alan M 3-7-1994 0 0 0 0
Alexander M 4-8-1994 0 0 0 0
Alexandra F 5-9-1994 0 0 0 0
Alfred M 6-10-1994 0 0 0 0
Analisa F 7-11-1994 0 0 0 0
Andrea F 8-12-1994 0 0 0 0
Andrew M 12-8-1995 0 0 0 0
Angelina F 12-8-1995 3 3 3 3
Antonietta Vicky F 12-8-1995 5 5 5 5
Arun Premnath M 12-8-1995 8 8 8 8
Chih F 12-8-1995 10 10 10 10
Colin M 1-1-1996 1 2 3 4
Cynthia F 2-2-1996 8 7 6 5
Dalila F 3-3-1996 9 10 0 1
Dana Colleen F 4-4-1996 4 3 2 1
Daniel M 5-5-1996 1 2 3 4
Kristal F 6-6-1996 1 2 1 2
Kristin Lunney F 7-7-1996 4 3 3 4
Kristina F 8-8-1996 7 7 6 5
Kurt M 9-9-1996 8 8 5 1
Lauren F 10-10-1996 4 5 2 3
Lien T. F 11-11-1996 5 4 2 2
Lifen F 12-12-1996 10 6 5 4
Students.txt
Abey F 5-5-1996 0 0 0 0
John M 5-5-1997 10 10 10 10
Melissa F 5-5-1996 10 10 10 10
Craig M 5-5-1998 5 5 5 5
Jeff M 5-5-1997 10 10 10 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.