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...

  • 1) Using the Quadratic class you have already developed, make it Comparable. A Quadratic is bigger...

    1) Using the Quadratic class you have already developed, make it Comparable. A Quadratic is bigger than another Quadratic if it opens faster. 2) Write a driver for Quadratic.java. In the driver program create a few objects and compare them . then create a list of those objects and sort them. import java.util.*; import java.lang.*; class Quadratic{    private double a,b,c; // Determines/declares the class variables to store the coefficients        Quadratic(){ // Determine/declare the default constructor       ...

  • 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...

  • Implement the remove() and the rehash() methods for the following externally chained hash table. Note you...

    Implement the remove() and the rehash() methods for the following externally chained hash table. Note you should rehash when "size > arraySize" at the start of the put() method. The new size of the arraySize should be "(2 * old array size) + 1" import java.util.ArrayList; import java.util.Hashtable; import java.util.LinkedList; import java.util.Map; public class ExternalChainingHashTable<K,V> { private class Mapping { private Mapping next; private K key; private V value; private Mapping(K key, V value) { this.key = key; this.value =...

  • 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...

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