Question: Convert Java to python Can anyone rewrite this code from java into python? Thanks. public class DateProfile { //constants public static final int MIN_ROMANCE =
Convert Java to python
Can anyone rewrite this code from java into python? Thanks.
public class DateProfile { //constants public static final int MIN_ROMANCE = 1; public static final int MAX_ROMANCE = 10; public static final int MIN_FINANCE = 1; public static final int MAX_FINANCE = 10; private static final char DEFAULT_GEN = 'M'; private static final char DEFAULT_SEARCH_GEND = 'F'; private static final String DEFAULT_NAME = "No Name"; private static final int DEFAULT_ROMANCE = 5; private static final int DEFAULT_FINANCE = 5; private char gender; private char searchGender; private int romance; private int finance; private String name;
public DateProfile()
{ setDefaults(); } public DateProfile(char gndr, char searchGndr, int rom, int fin, String name) { setAll(gndr, searchGndr, rom, fin, name); }
public char getGender() { return gender; }
public void setGender(char gender) //gender of applicant { if (gender == 'M' || gender == 'F') this.gender = gender;
else this.gender = DEFAULT_GEN; }
public char getSearchGender() { return searchGender; }
public void setSearchGender(char searchGender) // search partner gender { if (searchGender == 'M' || searchGender == 'F') this.searchGender = searchGender;
else this.searchGender = DEFAULT_SEARCH_GEND; }
public int getRomance() { return romance; }
public void setRomance(int romance) { if (romance >= MIN_ROMANCE && romance <= MAX_ROMANCE) //if romance is within range use the value this.romance = romance;
else this.romance = DEFAULT_ROMANCE; //if not in range use default romance value }
public int getFinance() { return finance; }
public void setFinance(int finance) { if (finance >= MIN_FINANCE & finance <= MAX_FINANCE) //if finance is within range use the value this.finance = finance;
else this.finance = DEFAULT_FINANCE; // if finance is not in range use default finance value }
public String getName() { return name; }
public void setName(String name) //gets name of applicant { if (name != null) this.name = name;
else this.name = DEFAULT_NAME; }
void setAll(char gndr, char searchGndr, int rom, int fin, String name)
{ setGender(gndr); setSearchGender(searchGndr); setRomance(rom); setFinance(fin); setName(name); }
void setDefaults()
{ setGender(DEFAULT_GEN); setSearchGender(DEFAULT_SEARCH_GEND); setRomance(DEFAULT_ROMANCE); setFinance(DEFAULT_FINANCE); setName(DEFAULT_NAME); }
public double fitValue(DateProfile partner) //calculates fit value
{ if (determineGenderFit(partner) == 0) return 0;
else { double total = determineFinanceFit(partner) + determineRomanceFit(partner); return total / 2; // return average of finance and romance fit values }
}
private double determineGenderFit(DateProfile partner) //finds gender fit
{ if (getSearchGender() == partner.getGender()) return 1;
else return 0; }
private double determineRomanceFit(DateProfile partner)
{
int diff = Math.abs(getRomance() - partner.getRomance()); //gets difference between romance //calculates romance fit if (diff == 0) return 1;
else { int max_romance_diff = MAX_ROMANCE - MIN_ROMANCE; double fit = ((double) (max_romance_diff - diff)) / max_romance_diff;
if (fit == 0) fit = 0.1;
return fit; }
}
private double determineFinanceFit(DateProfile partner)
{ int diff = Math.abs(getFinance() - partner.getFinance()); //gets difference between finance //calculates finance fit if (diff == 0) return 1;
else { int max_finance_diff = MAX_FINANCE - MIN_FINANCE; double fit = ((double) (max_finance_diff - diff)) / max_finance_diff;
if (fit == 0) fit = 0.1;
return fit; }
}
}
public class Foothill {
static void displayTwoProfiles(DateProfile profile1, DateProfile profile2)
{ System.out.println( "Fit between " + profile1.getName() + " and " + profile2.getName() + ": " + profile1.fitValue(profile2)); }
public static void main(String[] args) { DateProfile app1 = new DateProfile('M', 'F', 4, 6, "David"); DateProfile app2 = new DateProfile('F', 'M', 7, 5, "Kristie"); DateProfile app3 = new DateProfile('M', 'F', 9, 8, "Tim"); DateProfile app4 = new DateProfile('F', 'M', 7, 7, "Alice");
//displays result between 2 profiles displayTwoProfiles(app1, app1); displayTwoProfiles(app1, app2); displayTwoProfiles(app1, app3); displayTwoProfiles(app1, app4); displayTwoProfiles(app2, app1); displayTwoProfiles(app2, app2); displayTwoProfiles(app2, app3); displayTwoProfiles(app2, app4); displayTwoProfiles(app3, app1); displayTwoProfiles(app3, app2); displayTwoProfiles(app3, app3); displayTwoProfiles(app3, app4); displayTwoProfiles(app4, app1); displayTwoProfiles(app4, app2); displayTwoProfiles(app4, app3); displayTwoProfiles(app4, app4);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
