Modify the following code so it has at least the same capabilities, but make 3 d
ID: 3862138 • Letter: M
Question
Modify the following code so it has at least the same capabilities, but make 3 different changes to improve the design (ex: naming all constants only counts as one change) You are free to enable the code to do more, and you are not obligated to provide the exact same API as the existing class. You should assume have the same length. public class Poor {String[] names; char [] genders; boolean[] highs, boughts; public Poor (String ()_names, char (_genders, boolean(_highs, boolean[]_ boughts){names = _names; genders = _genders; highs = _highs; boughts = _boughts;}//determine if gender makes a bigger impact//than salary on whether to buy a car public boolean genderOverSalary() {int fBuyers = 0;//female buyers int mBuyers = 0; int hiBuyers = 0;//high salary buyers int loBuyers = 0; int genderDiff, salDiff; for (int cust = 0; cust loBuyers) salDiff = hiBuyers - loBuyers; else salDiff = loBuyers - hiBuyers; return genderDiff > salDiff;}}Explanation / Answer
public class Poor
{
string[] names;
char[] genders;
Boolean[] highs, boughts;
public Poor(string[] _names, char[] _genders, Boolean[] _highs, Boolean[] _boughts)
{
names = _names;
genders = _genders;
highs = _highs;
boughts = _boughts;
}
public Boolean genrderOverSalary()
{
int highbuyers = 0, lowbuyers = 0;
// Change1 - We can declare all intger with the same values on th same line
int genderDiff, salDiff;
//for (int cust = 0; cust < genders.Length; cust++)
// if (boughts[cust])
// if (genders[cust] == 'f')
// fBuyers++;
// else mBuyers++;
// Change2 - Instead of writing whole code we can use Linq to find count of female and male buyers
var fBuyers = genders.Count(c => c == 'f');
var mBuyers = genders.Count(c => c == 'm');
for (int cust = 0; cust < highs.Length; cust++)
if (boughts[cust])
if (highs[cust])
highbuyers++;
else lowbuyers++;
// Change3 - Instead of using else we can write it down in the folliwng way
genderDiff = fBuyers > mBuyers ? fBuyers - mBuyers : mBuyers - fBuyers;
salDiff = highbuyers > lowbuyers ? highbuyers - lowbuyers : lowbuyers - highbuyers;
return genderDiff > salDiff;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.