Classes Homework
first attempt (ian wu literally (probably) saw me code half of this in front of his eyes in class)
public class SingleTable {
public int getNumSeats() {}
public int getHeight() {}
public double getViewQuality() {}
public void setViewQuality(double value) {}
}
public class CombinedTable {
public SingleTable t1;
public SingleTable t2;
public CombinedTable(SingleTable t1, SingleTable t2) {
this.t1 = t1;
this.t2 = t2;
}
public boolean canSeat(int n) {
// can seat customers 2 less than total
// if number of people is greater than 2 less than the total, you cant seat them
// -> return false
// else return true
if (n>(t1.getNumSeats()+t2.getNumSeats()-2)) {
return false;
}
else {
return true;
}
}
public double getDesireability(SingleTable t1, SingleTable t2) {
if (t1.getHeight() != t2.getHeight()) {
// if the heights are not the same, desireability is 10 less than the avg
return (t1.getViewQuality() + t2.getViewQuality())/2 - 10;
}
else {
// if the heights are the same, (not !=) desireability is the average of the two
return (t1.getViewQuality()+t2.getViewQuality())/2;
}
}
}
SCORING
- declares class header and constructor w/ parameters, constructor not private: yes
- declares appropriate private instance variables including at least two SingleTable references: no
- constructor initializes instance variables using parameters: yes
- declares header public boolean canseat(int _): yes
- calls getNumSeats and returns true if and only if sum of two tables -2 >= n: yes, because i checked for the opposite condition to return false, x>y returns false is the same as y >= x returns true (i think?)
- declares header public double getDesirability(): technically no (idk if they will care about a spelling error but apparently i dont know how to spell desirability)
- calls getHeight and getViewQuality on SingleTable objects: yes, calling them on t1 and t2
- getDesirability computes average of constituent tables’ views desirabilities: yes
total score: 7/9, maybe probably could be 8 if they dont count my bad spelling
self notes:
- make a habit of using private instance variables, it is good practice for both general coding and CB
- read the question carefully to not make spelling errors
code rewrite:
public class SingleTable {
public int getNumSeats() {}
public int getHeight() {}
public double getViewQuality() {}
public void setViewQuality(double value) {}
}
public class CombinedTable {
// private
private SingleTable t1;
private SingleTable t2;
public CombinedTable(SingleTable t1, SingleTable t2) {
this.t1 = t1;
this.t2 = t2;
}
public boolean canSeat(int n) {
if (n>(t1.getNumSeats()+t2.getNumSeats()-2)) {
return false;
}
else {
return true;
}
}
public double getDesirability(SingleTable t1, SingleTable t2) {
// fixed spelling
if (t1.getHeight() != t2.getHeight()) {
return (t1.getViewQuality() + t2.getViewQuality())/2 - 10;
}
else {
return (t1.getViewQuality()+t2.getViewQuality())/2;
}
}
}