Below are 4 classes. I have no clue where i'm going wrong to get the correct output, it runs perfectly fine just gives the wrong output. Anything will help. Thank you!
This is my output:
Abhay matches with Alexandra with the score 96
Adam matches with Alan with the score 99
Alan has no matches
Alexander matches with Alfred with the score 98
Alexandra has no matches
Alfred has no matches
Analisa matches with Andrea with the score 99
Andrea has no matches
Andrew matches with Colin with the score 89
Angelina matches with Antonietta Vicky with the score 92
Antonietta Vicky has no matches
Arun Premnath matches with Kurt with the score 80
Chih matches with Cynthia with the score 84
Colin has no matches
Cynthia has no matches
Dalila matches with Dana Colleen with the score 85
Dana Colleen has no matches
Daniel matches with Kurt with the score 77
Kristal matches with Kristin Lunney with the score 91
Kristin Lunney has no matches
Kristina matches with Lifen with the score 90
Kurt has no matches
Lauren matches with Lien T. with the score 96
Lien T. has no matches
Lifen has no matches
This is the output i'm supposed to get:
Abhay matches with Alexandra with the score 96
Adam matches with Alan with the score 100
Alexander matches with Alfred with the score 98
Analisa matches with Andrea with the score 99
Andrew matches with Colin with the score 90
Angelina matches with Dana Colleen with the score 93
Antonietta Vicky matches with Cynthia with the score 93
Arun Premnath matches with Kurt with the score 81
Chih matches with Dalila with the score 78
Daniel has no matches.
Kristal matches with Kristin Lunney with the score 91
Kristina matches with Lifen with the score 90
Lauren matches with Lien T. with the score 96
This is my output:
Abhay matches with Alexandra with the score 96
Adam matches with Alan with the score 99
Alan has no matches
Alexander matches with Alfred with the score 98
Alexandra has no matches
Alfred has no matches
Analisa matches with Andrea with the score 99
Andrea has no matches
Andrew matches with Colin with the score 89
Angelina matches with Antonietta Vicky with the score 92
Antonietta Vicky has no matches
Arun Premnath matches with Kurt with the score 80
Chih matches with Cynthia with the score 84
Colin has no matches
Cynthia has no matches
Dalila matches with Dana Colleen with the score 85
Dana Colleen has no matches
Daniel matches with Kurt with the score 77
Kristal matches with Kristin Lunney with the score 91
Kristin Lunney has no matches
Kristina matches with Lifen with the score 90
Kurt has no matches
Lauren matches with Lien T. with the score 96
Lien T. has no matches
Lifen has no matches
This is the output i'm supposed to get:
Abhay matches with Alexandra with the score 96
Adam matches with Alan with the score 100
Alexander matches with Alfred with the score 98
Analisa matches with Andrea with the score 99
Andrew matches with Colin with the score 90
Angelina matches with Dana Colleen with the score 93
Antonietta Vicky matches with Cynthia with the score 93
Arun Premnath matches with Kurt with the score 81
Chih matches with Dalila with the score 78
Daniel has no matches.
Kristal matches with Kristin Lunney with the score 91
Kristina matches with Lifen with the score 90
Lauren matches with Lien T. with the score 96
Code:
public class Student {
public String Name;
public char Gender;
public int Month;
public int Day;
public int Year;
Date birthDay = new Date ();
Preference pref;
public boolean matched;
public Student () {
Name = "";
Gender = 'M';
matched = false;
birthDay = new Date (Month, Day, Year);
}
public Student (String Name, char Gender, boolean matched, Preference pref) {
this.Name = Name;
this.Gender = Gender;
this.matched = matched;
birthDay = new Date ( Month, Day, Year);
this.pref = pref;
}
public void setGender (char Gender) {
this.Gender = Gender;
}
public void setBirthDay ( int Month, int Day, int Year){
birthDay = new Date (Month, Day, Year);
}
public void setMatched (boolean matched){
this.matched = matched;
}
public void setPref(Preference pref){
this.pref = pref;
}
public char getGender () {
return this.Gender;
}
public int Compatibility(Student st ){
int score = 0;
int preference = Math.abs(pref.Compability (st.pref));
int AgeDifference = Math.abs(birthDay.compare(st.birthDay));
if (this.Gender != st.getGender()){
score = 0;
}
else {
score = (40 - preference) + (60 - AgeDifference);
}
return score;
}
}
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) {
// TODO Auto-generated method stub
Student[] person;
person = new Student[100];
int personNum, k=0;
String change;
for (int i=0; i<100; i++) {
person[i] = new Student();
}
Scanner input = new Scanner(System.in);
System.out.print("File to load: ");
String filename;
filename = input.next();
try {
Scanner in = new Scanner(new FileReader(filename +".txt"));
int i = 0;
while (in.hasNextLine() && i<100) {
Scanner data = new Scanner(in.nextLine()).useDelimiter("[\t-]");
if (data.hasNext()){
person[i].Name = data.next();
}
if (data.hasNext()){
change = data.next();
person[i].Gender = change.charAt(0);
}
if (data.hasNext()){
person[i].birthDay.month = data.nextInt();
person[i].birthDay.day = data.nextInt();
person[i].birthDay.year = data.nextInt();
}
int Music =0;
int Reading= 0;
int Chatting = 0;
int Quiet =0 ;
if (data.hasNext())
Quiet= data.nextInt();
if (data.hasNext())
Music= data.nextInt();
if (data.hasNext())
Reading= data.nextInt();
if (data.hasNext())
Chatting= data.nextInt();
person[i].pref = new Preference (Quiet, Music, Reading, Chatting);
i++;
}
input.close();
}catch (NoSuchElementException e) {
System.out.println(e);
}catch (FileNotFoundException e) {
System.out.println(e);
}
for (personNum=0; person[personNum].Name !=""; personNum++) {
}
String[] match = new String[personNum];
int[] results = new int[1000];
k=0;
int marker = 0;
for (int i=0; i<personNum; i++) {
for (k=i+1; k<personNum; k++) {
if (person[i].matched==false) {
if (person[i].Gender == person[k].Gender) {
if (person[i].Compatibility(person[k])>results[i]) {
results[i] = person[i].Compatibility(person[k]);
match[i]=person[k].Name;
marker = k;
}
}
else{
}
}
if (k == personNum-1) {
if (results[i]>0) {
person[i].matched=true;
person[marker].matched=true;
}
}
}
}
for (int i=0; i<match.length; i++) {
for (int j=0; j<match.length; j++) {
if (j != i) {
if (match[i] == match[j]) {
//match[j] = null;
}
}
}
}
for (int i=0; i<personNum; i++) {
if (match[i] != null) {
System.out.println(person[i].Name + " matches with " + match[i] + " with the score " +results[i]);
}
else {
System.out.println(person[i].Name + " has no matches");
}
}
}
}
public class Date {
protected int year;
protected int month;
protected int day;
public int Compare;
public Date(){
month = 0;
day = 0;
year = 0;
}
public Date(int month, int day, int year){
this.month = month;
this.day = day;
this.year = year;
}
public int getyear(){
return year;
}
public int getmonth(){
return month;
}
public int getday(){
return day;
}
public int compare(Date dt){
int totaldays = 0;
int p1 = ((dayofYear(dt) + dt.year * 365)/ 30);
int p2 = ((dayofYear(this) + this.year * 365)/30);
totaldays = Math.abs(p1 - p2);
if (totaldays > 60)
totaldays = 60;
Compare = totaldays;
return Compare;
}
public int dayofYear (Date datee) {
int totalDays = 0;
switch (datee.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 += datee.day;
return totalDays;
} }
public class Preference {
public int Quiet;
public int Music;
public int Reading;
public int Chatting;
public Preference (int Quiet, int Music, int Reading, int Chatting)
{
this.Quiet = Quiet;
this.Music = Music;
this.Reading = Reading;
this.Chatting= Chatting;
}
public int Compability (Preference pref) {
return Math.abs (Quiet-pref.Quiet) + Math.abs (Reading-pref.Reading) +Math.abs (Chatting-pref.Chatting)+Math.abs (Music-pref.Music);
}
}
Comment