You have been hired by the UCMerced Dorm Life Council to prepare a program which
ID: 3695879 • 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.
Write a Preference class that records preferences for different types of activities:
• quiet Time
• music
• reading
• chatting
Each of these is of type int and must contain values in the range 0 to 10 (inclusive). 0 means the person hates the activity. 10 means the person loves the activity. The class Preference should have at least the following methods:
• a constructor, which sets the instance variables to their appropriate input parameters (4 inputs)
• Accessor methods for each of the 4 instance variables
• compare(Preference pref) method that returns the difference between oneself and the Preference input parameter, pref. o Sum of absolute value of the differences in the 4 activities.
Explanation / Answer
Match.java
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Match{
public static void main(String[] args){
//Create an array of Students (max = 100)
Student [] arr = new Student [100];
System.out.println("Please give the file name: ");
Scanner input = new Scanner(System.in);
String filename = input.next();
try {
Scanner fileInput = new Scanner (new FileReader(filename));
int i = 0;
while (fileInput.hasNextLine()){
Scanner line = new Scanner(fileInput.nextLine());
line.useDelimiter("[ ]"); //delimited
String name = line.next(); //name(String)
String gender = line.next(); //gender(char)
String date = line.next(); //date (String)
Scanner birthDateReader = new Scanner (date);
birthDateReader.useDelimiter("-");
int month = birthDateReader.nextInt();
int day = birthDateReader.nextInt();
int year = birthDateReader.nextInt();
int quietTime = line.nextInt();
int music = line.nextInt(); //int 0-10
int reading = line.nextInt(); //int 0-10
int chatting = line.nextInt(); //int 0-10
Date birthdate = new Date (month, day, year);
Preference pref = new Preference(quietTime, music, reading, chatting);
Student studentAdd = new Student(name, gender.charAt(0), birthdate, pref);
arr[i++] = studentAdd;
}
int max = i;
for (i = 0; i<max; i++){
if (!arr[i].getMatched()){
int bestScore = 0; int bestMatch = 0;
for (int j=i+1; j<max;j++){
if(!arr[j].getMatched()){
int tmp = arr[i].compare(arr[j]);
if (tmp > bestScore){
bestScore = tmp;
bestMatch = j;
}
}
}
if (bestScore != 0){
arr[i].setMatched(true);
arr[bestMatch].setMatched(true);
System.out.println(arr[i].getName() + " matches with " + arr[bestMatch].getName() + " with the score " + bestScore);
} else
if (!arr[i].getMatched())
System.out.println(arr[i].getName() + " has no matches.");
}
}
input.close();
} catch (NoSuchElementException e){
System.out.println(e);
} catch (FileNotFoundException e){
System.out.println(e);
}
}
}
Date.java
public class Date {
int day; //year: of type int
int month; //month: of type int
int year; //year: of type int
public Date() {
month = 1-12;
day= 1-31;
year = 1900-3000;
}
//a constructor, which sets the instance variables to their appropriate input parameters (3 inputs)
public Date (int year, int month, int day){
if (year >= 1900 && year <= 3000) // range 1900-3000
this.year = year;
if (month >= 1 && month <= 12) //range 1 to 12 (January is 1)
this.month = month;
if (day >=1 && day <=31) //in the range 1 to 31 AND appropriate to the month.
this.day = day;
}
/*
public int getYear () {
int Year = year;
return Year;
}
public int getMonth () {
int Month = month;
return Month;
}
public int getDay () {
int Day = day;
return Day;
}
*/
//Accessor methods
public int getDay(){
return day;
}
//Accessor methods
public int getMonth(){
return month;
}
//Accessor methods
public int getYear(){
return year;
}
/*compare(Date dt) method that returns the difference between
oneself and the Date input parameter, dt.
Use dayOfYear() to calculate the difference between
month/day then add 365*years to it. Finally divide the total number of days differential by 30 to approximate the # of months as a difference.
If the difference is higher than 60 then should only return 60 as a max value.
*/
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;
}
public int compare(Date dt) {
int diff = Math.abs((dayOfYear() + 365 * year)-(dt.dayOfYear() + 365 * dt.year));
int numMonths = diff/30;
if (numMonths > 60){
numMonths = 60;
}
return numMonths;
}
}
Preference.java
public class Preference{
int quietTime;
int music;
int reading;
int chatting;
public Preference (){
quietTime = 0;
music = 0;
reading = 0;
chatting = 0;
}
// a constructor: 4 input parameters
public Preference (int quietTime, int music, int reading, int chatting){
if (quietTime >= 0 && quietTime <= 10){
this.quietTime = quietTime;
}
if (music >= 0 && music <=10){
this.music = music;
}
if (reading >= 0 && reading <= 10){
this.reading = reading;
}
if (chatting >= 0 && chatting <= 10){
this.chatting = chatting;
}
}
//Accessor methods
public int getQuietTime (){
return quietTime;
}
//Accessor methods
public int getMusic (){
return music;
}
//Accessor methods
public int getReading (){
return reading;
}
//Accessor methods
public int getChatting (){
return chatting;
}
//Sum of absolute value of the differences in the 4
public int compare(Preference pref) {
return (Math.abs(quietTime - pref.quietTime) + Math.abs(music - pref.music) + Math. abs(reading - pref.reading) + Math.abs(chatting - pref.chatting));
}
}
Student.java
public class Student {
private String name; // name: of type String
private char gender; // gender: of type char
private Date birthDay; //birthDay of type Date
private Preference pref; // pref: of type Preference
private boolean matched; // matched of type Boolean
private int month;
private int day;
private int year;
public int compatabilityScore;
public Student (){ //default constructor
name = "Annaliza";
gender = 'F';
matched = false;
birthDay = new Date (month, day, year);
}
public Student(String name, char gender, Date birthDay, Preference pref) {
this.name = name; // input parameter 1
this.gender = gender; //input parameter 2
this.birthDay = birthDay; //input parameter 3
this.pref = pref; //input parameter4
//a constructor, which sets the instance variables to their appropriate input parameters (4 inputs)
}
// Mutator for the name
public void setName(String name){
this.name = name;
}
// Mutator for the gender
public void setGender(char gender){
this.gender = gender;
}
// Mutator for the gender
public void setbirthDay(Date birthDay){
this.birthDay = birthDay;
}
// Mutator for the preference
public void setPreference(Preference pref){
this.pref = pref;
}
public void setMatched(boolean matched){
this.matched = matched;
}
//Accessor for the name
public String getName() {
return name;
}
//Accessor for the Gender
public char getGender(){
return gender;
}
//Accessor for the Day
public Date getbirthDay(){
return birthDay;
}
//Accessor for the Preference
public Preference getPref(){
return pref;
}
public boolean getMatched(){
return matched;
}
//compare(Student st) method that returns the compatibility score between oneself and the Student input parameter, st.
public int compare(Student st){
if(gender != st.gender){
return 0; //Different genders make score 0 (only matching same gender students as roommates)
}
compatabilityScore = (40 - pref.compare(st.pref)) + (60-birthDay.compare(st.birthDay)); //Formula : (40 Preferences) + (60 AgeDifference)
if (compatabilityScore < 0){
return 0; //Highest score is 100 and lowest is 0
}
else if (compatabilityScore > 100){
return 100; //Highest score is 100 and lowest is 0
}
return compatabilityScore; //Compatibility Score Calculation (returned by compare)
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.