Martian Cubics and Unit Testing
You must implement all of the data members and methods described below. RAM is more
limited on NASA missions than on your home computer, so you may NOT add any instance
variables or static variables to this class other than those described below. You may add methods
of your own, as long as they are private. The data type class you are writing is a very general
class that could be of use in a wide variety of projects at NASA in their Mars research, so take
care in your work!
Implement
eval -- this method takes one parameter (DoubleWithAppx), evaluates the Martian cubic at the
point represented by the parameter and returns a DoubleWithAppx representing the result of that
evaluation. (i.e., if your Martian cubic is 5x^3-3x^2+2x+4, and you call eval(5), it should return
564.)
My Code:
public class MartianCubic {
private final DoubleWithAppx a ;
private final DoubleWithAppx b;
private final DoubleWithAppx c;
private final DoubleWithAppx d;
public MartianCubic() {
a = new DoubleWithAppx(0);
b = new DoubleWithAppx(0);
c = new DoubleWithAppx(0);
d = new DoubleWithAppx(0);
}
public MartianCubic(DoubleWithAppx dIn) {
d = dIn;
a = new DoubleWithAppx(0);
b = new DoubleWithAppx(0);
c = new DoubleWithAppx(0);
}
public MartianCubic(DoubleWithAppx cIn, DoubleWithAppx dIn) {
c = cIn;
d = dIn;
a = new DoubleWithAppx(0);
b = new DoubleWithAppx(0);
}
public MartianCubic(DoubleWithAppx bIn, DoubleWithAppx cIn, DoubleWithAppx dIn) {
this.b = bIn;
this.c = cIn;
this.d = dIn;
a = new DoubleWithAppx(0);
}
public MartianCubic(DoubleWithAppx aIn, DoubleWithAppx bIn, DoubleWithAppx cIn,
DoubleWithAppx dIn) {
this.a = aIn;
this.b = bIn;
this.c = cIn;
this.d = dIn;
}
public MartianCubic(MartianCubic other) {
this(
other.getA(),
other.getB(),
other.getC(),
other.getD()
);
}
public DoubleWithAppx getA() {
return a;
}
public DoubleWithAppx getB() {
return b;
}
public DoubleWithAppx getC() {
return c;
}
public DoubleWithAppx getD() {
return d;
}
public DoubleWithAppx eval(DoubleWithAppx x) {
//HINT: Think about how to chain method calls to make this compact.
}
public boolean equals (Object other) {
if (other == null) {
return false;
}
else if (this.getClass()!=other.getClass()) {
return false;
}
else {
MartianCubic casted = (MartianCubic)other;
return (
a.equals(casted.a) &&
b.equals(casted.b) &&
c.equals(casted.c) &&
d.equals(casted.d)
);
}
}
Solution
//solution
package ex;
public class MartianCubic {
private final DoubleWithAppx a ;
private final DoubleWithAppx b;
private final DoubleWithAppx c;
private final DoubleWithAppx d;
public MartianCubic() {
a = new DoubleWithAppx(0);
b = new DoubleWithAppx(0);
c = new DoubleWithAppx(0);
d = new DoubleWithAppx(0);
}
public MartianCubic(DoubleWithAppx dIn) {
d = dIn;
a = new DoubleWithAppx(0);
b = new DoubleWithAppx(0);
c = new DoubleWithAppx(0);
}
public MartianCubic(DoubleWithAppx cIn, DoubleWithAppx dIn) {
c = cIn;
d = dIn;
a = new DoubleWithAppx(0);
b = new DoubleWithAppx(0);
}
public MartianCubic.
Martian Cubics and Unit TestingYou must implement all of the data .pdf
1. Martian Cubics and Unit Testing
You must implement all of the data members and methods described below. RAM is more
limited on NASA missions than on your home computer, so you may NOT add any instance
variables or static variables to this class other than those described below. You may add methods
of your own, as long as they are private. The data type class you are writing is a very general
class that could be of use in a wide variety of projects at NASA in their Mars research, so take
care in your work!
Implement
eval -- this method takes one parameter (DoubleWithAppx), evaluates the Martian cubic at the
point represented by the parameter and returns a DoubleWithAppx representing the result of that
evaluation. (i.e., if your Martian cubic is 5x^3-3x^2+2x+4, and you call eval(5), it should return
564.)
My Code:
public class MartianCubic {
private final DoubleWithAppx a ;
private final DoubleWithAppx b;
private final DoubleWithAppx c;
private final DoubleWithAppx d;
public MartianCubic() {
a = new DoubleWithAppx(0);
b = new DoubleWithAppx(0);
c = new DoubleWithAppx(0);
d = new DoubleWithAppx(0);
}
public MartianCubic(DoubleWithAppx dIn) {
d = dIn;
a = new DoubleWithAppx(0);
b = new DoubleWithAppx(0);
c = new DoubleWithAppx(0);
}
public MartianCubic(DoubleWithAppx cIn, DoubleWithAppx dIn) {
c = cIn;
d = dIn;
2. a = new DoubleWithAppx(0);
b = new DoubleWithAppx(0);
}
public MartianCubic(DoubleWithAppx bIn, DoubleWithAppx cIn, DoubleWithAppx dIn) {
this.b = bIn;
this.c = cIn;
this.d = dIn;
a = new DoubleWithAppx(0);
}
public MartianCubic(DoubleWithAppx aIn, DoubleWithAppx bIn, DoubleWithAppx cIn,
DoubleWithAppx dIn) {
this.a = aIn;
this.b = bIn;
this.c = cIn;
this.d = dIn;
}
public MartianCubic(MartianCubic other) {
this(
other.getA(),
other.getB(),
other.getC(),
other.getD()
);
}
public DoubleWithAppx getA() {
return a;
}
public DoubleWithAppx getB() {
return b;
}
3. public DoubleWithAppx getC() {
return c;
}
public DoubleWithAppx getD() {
return d;
}
public DoubleWithAppx eval(DoubleWithAppx x) {
//HINT: Think about how to chain method calls to make this compact.
}
public boolean equals (Object other) {
if (other == null) {
return false;
}
else if (this.getClass()!=other.getClass()) {
return false;
}
else {
MartianCubic casted = (MartianCubic)other;
return (
a.equals(casted.a) &&
b.equals(casted.b) &&
c.equals(casted.c) &&
d.equals(casted.d)
);
}
}
Solution
4. //solution
package ex;
public class MartianCubic {
private final DoubleWithAppx a ;
private final DoubleWithAppx b;
private final DoubleWithAppx c;
private final DoubleWithAppx d;
public MartianCubic() {
a = new DoubleWithAppx(0);
b = new DoubleWithAppx(0);
c = new DoubleWithAppx(0);
d = new DoubleWithAppx(0);
}
public MartianCubic(DoubleWithAppx dIn) {
d = dIn;
a = new DoubleWithAppx(0);
b = new DoubleWithAppx(0);
c = new DoubleWithAppx(0);
}
public MartianCubic(DoubleWithAppx cIn, DoubleWithAppx dIn) {
c = cIn;
d = dIn;
a = new DoubleWithAppx(0);
b = new DoubleWithAppx(0);
}
public MartianCubic(DoubleWithAppx bIn, DoubleWithAppx cIn, DoubleWithAppx dIn) {
this.b = bIn;
this.c = cIn;
this.d = dIn;
a = new DoubleWithAppx(0);
}