Class DateProfile Create a class called DateProfile that has the following priva
ID: 3549044 • Letter: C
Question
Class DateProfile
Create a class called DateProfile that has the following private instance members:
Class DateProfile Create a class called DateProfile that has the following private instance members: gender - a char, the gender of the applicant ('M' or 'F'). searchGender - a char, the gender of desired partner ('M' or 'F'). This is not the gender of the applicant, but of the applicant's requested partner. romance - an int from 1 to 10, indicating the importance of romance to the applicant. finance - an int from 1 to 10, indicating the importance of finance to the applicant. name - a String indicating the full name of the applicant. Each object in the DateProfile class represents an applicant's profile. If the object is ('M', 'F', 7, 4, "Hugh Hefner") then the applicant's name is "Hugh Hefner", he's looking for a date who is Female, with romance being somewhat important (7) and finance being less important (4). You should supply all of the following member functions of class DateProfile (at a minimum): Accessors and Mutators for each field (instance member). For example: char getGender() and boolean setGender(char gdr). Constructors that take no parameters (default) and all 5 parameters. double fitValue(DateProfile partner), which returns a number from 0.0 (very bad fit) to 1.0 (perfect fit). The public instance method compares the calling object (this) to the object passed as a parameter. This method should call three private methods determineGenderFit( ... ), determineRomanceFit( ... ) anddetermineFinanceFit( ... ), that will be used to return intermediate results for each of the three factors. It should multiply the three intermediate numbers together to get and return the final FitValue. double determineGenderFit(DateProfile partner) returns either a 0 or 1 depending on the gender compatibility of the calling object and the passed parameter object. You have to compare gender compatibility completely: i.e., there must be mutual consent on this one! double determineRomanceFit(DateProfile partner) returns a number from 0.0 to 1.0 depending on the romance compatibility of the calling object and the passed parameter object. The romance numbers should be highest (1.0) if the two values are equal (both 3, both 5, both 7) and lowest (perhaps a small non-zero value like .1) if their difference is 9. double determineFinanceFit(DateProfile partner) returns a number from 0.0 to 1.0 depending on the finance compatibility of the calling object and the passed parameter object. The finance numbers should be highest (1.0) if the two values are equal (both 3, both 5, both 7) and lowest (perhaps a small non-zero value like .1) if their difference is 9. DateProfile should be a class distinct from (and not contained within) your main class (which we call Foothill). However, you can and should defined it as a non-public class so it can reside in the same file, Foothill.java. The Client Driver From your main() inside Foothill, you will instantiate a total of four DateProfile objects, applicant1, ... applicant4 manually from literal values in your program, i.e., do not involve the user with run-time input. Then for each of the four applicants, display the fits with the others - including themselves. Do this by showing the name of the applicant, then the names and fit values of all applicants relative to this one applicant. Repeat this list for all four applicants producing 16 comparison figures grouped into four groups of four each. (You will be comparing each applicant to his/herself in each of these four groups. This will serve to check whether the result is correct - it must be either a 1 or a 0 depending on the searchGender they requested, but never a number between (can you see why?)). Here is part of a sample output: Hugh Hefner Carmen Miranda fit: 0.82314 Brad Pitt fit 0.0 Mary Lynn Rajskub fit 0.6302 Hugh Hefner fit 0.0 Carmen Miranda Carmen Miranda fit: 0.0 Brad Pitt fit .72029 Mary Lynn Rajskub fit 0.0 Hugh Hefner fit 0.82314, ... etc. Make sure all mutators, constructors and other methods that affect private data adequately test for illegal values and, if possible, return a boolean that reports the results of this test.Explanation / Answer
public class DateProfile {
//declaring private variables
private char gender;
private char searchGender ;
private int romance;
private int finance ;
private String name;
//getter method for gender
public char getGender() {
return gender;
}
//setter method for gender
public boolean setGender(char gender) {
//if the gender value in char is M or F, then only set it and return true else return false and dont set it.
if (gender== 'M' || gender == 'F'){
this.gender = gender;
return true;
}
else
return false;
}
//getter method for searchGender
public char getSearchGender() {
return searchGender;
}
//setter method for searchGender
public boolean setSearchGender(char searchGender) {
//if the searchGender value in char is M or F, then only set it and return true else return false and dont set it.
if (searchGender== 'M' || searchGender == 'F'){
this.searchGender = searchGender;
return true;
}
else
return false;
}
//getter method for romance
public int getRomance() {
return romance;
}
//setter method for romance
public boolean setRomance(int romance) {
//if the value of romance lies in the range 1 to 10, then only set it and return true
// else return false and dont set it.
if (romance >= 1 && romance <= 10){
this.romance = romance;
return true;
}
else
return false;
}
//getter method for finance
public int getFinance() {
return finance;
}
//setter method for finance
public boolean setFinance(int finance) {
//if the value of finance is in the range 1 to 10, then only set it and return true else dont set it and return false.
if (finance >=1 && finance<= 10){
this.finance = finance;
return true;
}
else
return false;
}
//getter method for name
public String getName() {
return name;
}
//setter method for name
public boolean setName(String name) {
//if the name is neither null nor blank and number of characters is at least 1 and maximum 80, set the value
if (name != null || name != " " || (name.length() >=1 && name.length() <= 80)){
this.name = name;
return true;
}
else
return false;
}
//Default constructor
public DateProfile() {
super();
this.gender = 'M';
this.searchGender = 'F';
this.romance = 5;
this.finance = 5;
this.name = "Undefined";
}
//parameterized constructor
public DateProfile(char gender, char searchGender, int romance,
int finance, String name) {
super();
this.gender = gender;
this.searchGender = searchGender;
this.romance = romance;
this.finance = finance;
this.name = name;
}
//get values from three private methods which are determineGenderFit, determineRomanceFit, determineFinanceFit and multiply and return the value
double fitValue(DateProfile partner){
double genderFitValue = this.determineGenderFit(partner);
double romanceFitValue = this.determineRomanceFit(partner);
double financeFitValue = this.determineFinanceFit(partner);
return genderFitValue * romanceFitValue * financeFitValue;
}
//this method ll return either 0 or 1 based on the match. The search gender should match with gender of the partner, if it matches
// then return 1 else return 0
private double determineGenderFit(DateProfile partner) {
if (this.getSearchGender() == partner.getGender())
return 1;
else
return 0;
}
// This method will determine romance.
private double determineRomanceFit(DateProfile partner){
// if both the applicant and the partner has same romance numbers then return 1
if (this.getRomance() == partner.getRomance())
return 1;
// else on the basis of difference print the double value
else if (Math.abs(this.getRomance() - partner.getRomance()) == 9)
return 0.1;
else if (Math.abs(this.getRomance() - partner.getRomance()) == 8)
return 0.2;
else if (Math.abs(this.getRomance() - partner.getRomance()) == 7)
return 0.3;
else if (Math.abs(this.getRomance() - partner.getRomance()) == 6)
return 0.4;
else if (Math.abs(this.getRomance() - partner.getRomance()) == 5)
return 0.5;
else if (Math.abs(this.getRomance() - partner.getRomance()) == 4)
return 0.6;
else if (Math.abs(this.getRomance() - partner.getRomance()) == 3)
return 0.7;
else if (Math.abs(this.getRomance() - partner.getRomance()) == 2)
return 0.8;
else
return 0.9;
}
//this method will determine finance fit
private double determineFinanceFit(DateProfile partner){
//if both the applicant and partner has same finance numbers then return 1
if(this.getFinance() == partner.getFinance())
return 1;
//else on the basis of difference return double value. For 9 it will be 0.1, for 8, it will be 0.2 and so on.
else if (Math.abs(this.getFinance() - partner.getFinance()) == 9)
return 0.1;
else if (Math.abs(this.getFinance() - partner.getFinance()) == 8)
return 0.2;
else if (Math.abs(this.getFinance() - partner.getFinance()) == 7)
return 0.3;
else if (Math.abs(this.getFinance() - partner.getFinance()) == 6)
return 0.4;
else if (Math.abs(this.getFinance() - partner.getFinance()) == 5)
return 0.5;
else if (Math.abs(this.getFinance() - partner.getFinance()) == 4)
return 0.6;
else if (Math.abs(this.getFinance() - partner.getFinance()) == 3)
return 0.7;
else if (Math.abs(this.getFinance() - partner.getFinance()) == 2)
return 0.8;
else
return 0.9;
}
}
//Runner class
public class Foothill {
//Main method
public static void main(String[] args) {
//Making 4 objects of DateProfile with populated values
DateProfile applicant1 = new DateProfile('M', 'F', 5, 4, "Hugh Hefner");
DateProfile applicant2 = new DateProfile('F', 'M', 4, 7,"Carmen Miranda");
DateProfile applicant3 = new DateProfile('M', 'F', 10, 2, "Brad Pit");
DateProfile applicant4 = new DateProfile('F', 'M', 5, 4, "Mary Lynn Rajskub");
//Printing applicant name
System.out.println(applicant1.getName());
//Calling the fitValue method and printing the double value which denotes the compatibility for applicant and partner.
//if it is 0, then it is worst fit. If it is 1, then it is best fit. And any thing in range 0 to 1 will denote the fit.
System.out.println(" "+applicant2.getName()+" fit: "+applicant1.fitValue(applicant2));
System.out.println(" "+applicant3.getName()+" fit: "+applicant1.fitValue(applicant3));
System.out.println(" "+applicant4.getName()+" fit: "+applicant1.fitValue(applicant4));
System.out.println(" "+applicant1.getName()+" fit: "+applicant1.fitValue(applicant1));
System.out.println(" "+applicant2.getName());
System.out.println(" "+applicant3.getName()+" fit: "+applicant2.fitValue(applicant3));
System.out.println(" "+applicant4.getName()+" fit: "+applicant2.fitValue(applicant4));
System.out.println(" "+applicant1.getName()+" fit: "+applicant2.fitValue(applicant1));
System.out.println(" "+applicant2.getName()+" fit: "+applicant2.fitValue(applicant2));
System.out.println(" "+applicant3.getName());
System.out.println(" "+applicant4.getName()+" fit: "+applicant3.fitValue(applicant4));
System.out.println(" "+applicant1.getName()+" fit: "+applicant3.fitValue(applicant1));
System.out.println(" "+applicant2.getName()+" fit: "+applicant3.fitValue(applicant2));
System.out.println(" "+applicant3.getName()+" fit: "+applicant3.fitValue(applicant3));
System.out.println(" "+applicant4.getName());
System.out.println(" "+applicant1.getName()+" fit: "+applicant4.fitValue(applicant1));
System.out.println(" "+applicant2.getName()+" fit: "+applicant4.fitValue(applicant2));
System.out.println(" "+applicant3.getName()+" fit: "+applicant4.fitValue(applicant3));
System.out.println(" "+applicant4.getName()+" fit: "+applicant4.fitValue(applicant4));
//Checking for validations
System.out.println(" Checking for illegal values");
//Trying to set finance value to 12. There is a check that the value should be in range 1 to 10
boolean flag = applicant1.setFinance(12);
if (flag)
System.out.println("Value of finance changed to "+applicant1.getFinance()+" for "+applicant1.getName());
else
System.out.println("Invalid value for finance. It should be in range 1 to 10.");
System.out.println(" Checking for illegal values");
//Setting the romance value of applicant 2 to 10. It is a valid value so change will be made.
boolean flags = applicant2.setRomance(10);
if (flags)
System.out.println("Value of romance changed to "+applicant2.getRomance()+" for "+applicant2.getName());
else
System.out.println("Invalid value. It should be in range 1 to 10.");
}
}