Question

Martian Cubics and Unit Testing You must implement all of the data members and methods described...

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)

           );

       }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//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);
}
  
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.
double ans;
ans= getA().getValue()* Math.pow(x.getValue(),3)-getB().getValue()*Math.pow(x.getValue(),2)+getC().getValue()*x.getValue()+getD().getValue();
return new DoubleWithAppx((int) ans);
}

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)
);
}
}

}

Add a comment
Know the answer?
Add Answer to:
Martian Cubics and Unit Testing You must implement all of the data members and methods described...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the Triangle class (from previous code, will post under this) to throw an exception in...

    Modify the Triangle class (from previous code, will post under this) to throw an exception in the constructor and set routines if the triangle is not valid (i.e., does not satisfy the triangle inequality). Modify the main routine to prompt the user for the sides of the triangle and to catch the exception and re-prompt the user for valid triangle sides. In addition, for any valid triangle supply a method to compute and display the area of the triangle using...

  • Given the following class: class Q2 { private int a; private int b; private int c;...

    Given the following class: class Q2 { private int a; private int b; private int c; public void setA(int a){this.a = a; } public void setB(int b){this.b = b;} public void setc(int c){this.c = c;} public int geta(){return a; } public int gets(){return b;} public int getc(){return c;} public int m1(int a, int b){ return a + b; public boolean m2 (int x, int y){ return m1(x, y) + x + y < 10; What is the output of the...

  • java The following code is an implementation of a HeapPriorityQueue. You are to implement the methods...

    java The following code is an implementation of a HeapPriorityQueue. You are to implement the methods commented with: // TODO: TIP: Do not just go from memory of your assignment implementation, be sure to consider carefully the constructor and method implementation provided to you. NOTE: You do not have to provide an implementation for any methods intentionally omitted public class Heap PriorityQueue implements PriorityQueue private final static int DEFAULT SIZE 10000 private Comparable [ ] storage private int currentSize: public...

  • Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support...

    Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support the Iterable interface such that the following code works: JstyArray<Integer> data; data = new JstyArray<Integer>(10); for (int i = 0; i < 10; ++i) { data.set(i, new Integer(i) ); } int sum = 0; for ( int v : data ) { if (v == null) continue; // empty cell sum += v; } The iterator provided by this class follows the behaviour of...

  • Consider the class as partially defined here: //class Pizzaorder class Pizzaorder // static public members public...

    Consider the class as partially defined here: //class Pizzaorder class Pizzaorder // static public members public static final int MAX TOPPINGS 20: // instance members private String toppings[]; private int numToppings; // constructor public PizzaOrder () { numToppings toppings 0; String[MAX TOPPINGS]; new // accessor tells # toppings on pizza, i.e., #toppings in array public int getNum Toppings ()return numToppings; // etc. We want to write an instance method, addTopping) that will take a String parameter, topping, and add it...

  • Your task is to go through and implement the methods getBucketIndex, add, get, and remove. Follow...

    Your task is to go through and implement the methods getBucketIndex, add, get, and remove. Follow the comments inside respective methods. import java.util.ArrayList; import java.util.Scanner; public class HashtableChaining<K, V> {    // Hashtable bucket    private ArrayList<HashNode<K, V>> bucket;    // Current capacity of the array list    private int numBuckets;    // current size of the array list    private int size;       public HashtableChaining(int buckets){        bucket = new ArrayList<>();        numBuckets = buckets;   ...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

  • Comparing the insertion and removing performance between ALPQueue with HeapPQueue. Requirements: 1/ Task1: implement a concrete...

    Comparing the insertion and removing performance between ALPQueue with HeapPQueue. Requirements: 1/ Task1: implement a concrete ALPQueue class (an ArrayList based priority queue) that extends the AbstractPQueue discussed in the class, along with PQueue.java, ArrayList.java, List.java, PQEntry.java , EntryComparitor.java, and OutOfRangeException.java given in the class (any other implementation of above classes that are not compatible with List interface and AbstractPQueue class will not receive any credit). 2/ Task2: implement a concrete HeapPQueue class (a Heap based priority queue) that extends...

  • When we test your Fraction.java we will use the given FractionTester.java file. Your Fraction.java file -MUST-...

    When we test your Fraction.java we will use the given FractionTester.java file. Your Fraction.java file -MUST- work perfectly with the supplied FractionTester.java file. You may NOT modify the FractionTester to make your Fraction class work right. Starter Fraction File: Fraction.java ADD METHODS TO THIS FILE. HAND IN THIS FILE ONLY. Given/Completed Tester File: FractionTester.java DO NOT MODIFY. DO NOT HAND IN. You are to add the following methods to the given Fraction.java file public Fraction add( Fraction other) returns a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT