Convert Java to python Can anyone rewrite this code from java into python? Thank
ID: 3891158 • Letter: C
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 = 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);
}
}
Explanation / Answer
Solution:
class DateProfile:
MIN_ROMANCE = 1
MAX_ROMANCE = 10
MIN_FINANCE = 1
MAX_FINANCE = 10
DEFAULT_GEN = 'M'
DEFAULT_SEARCH_GEND = 'F'
DEFAULT_NAME = "No Name"
DEFAULT_ROMANCE = 5
DEFAULT_FINANCE = 5
def _init_(self):
setDefaults()
def _init_(self,gndr,searchGndr,rom,fin,name):
setAll(gndr,searchGndr,rom,fin,name)
def getGender():
return gender
def setGender(self,gender):
if gender=='M' or gender=='F':
self.gender=gender
else:
self.gender=DEFAULT_GEN
def getSearchGender():
return searchGender
def setSearchGender(self,searchGender):
if searchGender == 'M' or searchGender == 'F':
self.searchGender = searchGender
else:
self.searchGender = DEFAULT_SEARCH_GEND
def getRomance():
return romance
def setRomance(self,romance):
#if romance is within range use the value
if romance >= MIN_ROMANCE and romance <= MAX_ROMANCE:
self.romance = romance
else:
#if not in range use default romance value
this.romance = DEFAULT_ROMANCE
def getFinance():
return finance
def setFinance(self,finance):
#if finance is within range use the value
if finance >= MIN_FINANCE and finance <= MAX_FINANCE:
self.finance = finance
#if finance is not in range use default finance value
else:
self.finance = DEFAULT_FINANCE
def getName():
return name
def setName(self,name):
if name != null:
self.name = name
else:
self.name = DEFAULT_NAME
def setAll(gndr,searchGndr,rom,fin,name):
setGender(gndr)
setSearchGender(searchGndr)
setRomance(rom)
setFinance(fin)
setName(name)
def setDefaults():
setGender(DEFAULT_GEN)
setSearchGender(DEFAULT_SEARCH_GEND)
setRomance(DEFAULT_ROMANCE)
setFinance(DEFAULT_FINANCE)
setName(DEFAULT_NAME)
#calculates fit value
def fitValue(self, partner):
if determineGenderFit(partner) == 0:
return 0
else:
total = determineFinanceFit(partner) + determineRomanceFit(partner)
#return average of finance and romance fit values
return total / 2
#finds gender fit
def determineGenderFit(self, partner):
if getSearchGender() == partner.getGender():
return 1
else:
return 0
def determineRomanceFit(self, partner):
#gets difference between romance
diff = Math.abs(getRomance() - partner.getRomance())
#calculates romance fit
if diff == 0:
return 1
else:
max_romance_diff = MAX_ROMANCE - MIN_ROMANCE
fit = ((double) (max_romance_diff - diff)) / max_romance_diff
if fit == 0:
fit = 0.1
return fit
def determineFinanceFit(self, partner):
#gets difference between finance
diff = Math.abs(getFinance() - partner.getFinance())
#Calculates finance fit
if diff == 0:
return 1
else:
max_finance_diff = MAX_FINANCE - MIN_FINANCE
fit = ((double) (max_finance_diff - diff)) / max_finance_diff
if fit == 0:
fit = 0.1
return fit
class Foothill:
def displayTwoProfiles(self, prfile1,profile2):
print("Fit between", profile1.getName(),"and",profile2.getName(),":",profile1.filValue(profile2))
app1=DateProfile('M', 'F', 4, 6, "David")
app2 =DateProfile('F', 'M', 7, 5, "Kristie")
app3 =DateProfile('M', 'F', 9, 8, "Tim")
app3 =DateProfile('M', 'F', 9, 8, "Tim")
#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)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.