Question

In: Computer Science

Hi I am getting error in implement some test case using Java. I am adding my...

Hi I am getting error in implement some test case using Java. I am adding my code and relevant files here, and the failed test. Please let me know where my problem is occuring and fix my code. Thanks! I will upvote.

Implement a class to perform windowing of a Hounsfield value

Implement the class described by this API. A partial implementation is provided for you in the eclipse project; however, unlike the previous class, very little work has been done for you. You must carefully study the API of the class to determine the precise signatures of the constructors and methods that you need to implement. Furthermore, you need to thoroughly understand the concept of windowing to determine what fields are required in your class.

  1. Begin by deciding how many fields are required and what their types should be. Add these fields to your class (making sure that they each have a private access modifier) giving them a sensible name when you do so.
  2. Once you have added the fields to your class, implement the methods getLevel and getWidth. The JUnit tester requires these methods to test the constructors and other methods.
  3. Implement the constructor HounsfieldWindow(int level, int width) first. Make sure that it has the correct access modifier and signature.
  4. Implement the no-argument constructor HounsfieldWindow() next. Use constructor chaining to implement this constructor.
  5. Implement the remaining methods making sure that they each have the correct access modifier, signature, and return type.

Remember to run the JUnit tester each time you complete a constructor or method, and to carefully study the result of the tests to help you through the development process.

API doc: https://drive.google.com/open?id=1GKm_m74PwFBZIC__JmnWnGi6cFzmGuul

/**
* A class that represents a windowed view of Hounsfield units. A Hounsfield
* window is defined by two values: (1) the window level, and (2) the window
* width. The window level is the Hounsfield unit value that the window is
* centered on. The window width is the range of Hounsfield unit values that the
* window is focused on.
*
*


* A window has a lower and upper bound. The lower bound is defined as the
* window level minus half the window width:
*
*


* lo = level - (width / 2)
*
*


* The upper bound is defined as the window level plus half the window width:
*
*


* hi = level + (width / 2)
*
*


* Hounsfield units are mapped by the window to a real number in the range of
* {@code 0} to {@code 1}. A Hounsfield unit with a value less than lo is mapped
* to the value {@code 0}. A Hounsfield unit with a value greater than hi is
* mapped to the value {@code 1}. A Hounsfield unit with a value v between lo
* and hi is mapped to the value:
*
*


* (v - lo) / width
*
*
*/
public class HounsfieldWindow {
  
   private int level;
  
   private int width;
  
  
   HounsfieldWindow(int level, int width) {
       setLevel(level);
       this.width = width;
   }
  
   public HounsfieldWindow() {
       this(0, 400);
   }
   public int getLevel() {
       return level;
   }
  
   public int setLevel(int level) {
       if (level < Hounsfield.MIN_VALUE || level > Hounsfield.MAX_VALUE) {
           throw new IllegalArgumentException();
       }
       int data = this.level;
       this.level = level;
       return data;
   }
  
   public int getWidth() {
       return width;
   }
  
   public int setWidth(int width) {
       if(width < 1) {
           throw new IllegalArgumentException();
       }
       int data = this.width;
       this.width = width;
       return data;
  
      
   }
  
   public double getLowerBound() {
       return level - (width / 2);
   }
   public double getUpperBound() {
   return level + (width / 2);
   }

   public double map(Hounsfield h) {
       // TODO Auto-generated method stub
       int hounsfieldUnitVal = h.get();
   System.out.println("got " + hounsfieldUnitVal);
   if(hounsfieldUnitVal < getLowerBound()) {
   return 0;
   }
   if(hounsfieldUnitVal > getUpperBound()) {
   return 1;
   }
   return (hounsfieldUnitVal - getLowerBound()) / (double)width;
   }

  
  
  
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Tests that fails:

@Test
   public void test04_ctorThrowsOnBadWidth() {
       final int[] BAD_WIDTHS = {-10000, -10, 0};
       for (int width : BAD_WIDTHS) {
           try {
               new HounsfieldWindow(0, width);
               fail(String.format("new HounsfieldWindow(0, %s) should throw an exception", width));
           }
           catch (IllegalArgumentException x) {
               // ok
           }
       }
   }

@Test
   public void test10_map() {
       assumeTrue("test requires a correct implementation of HounsfieldWindow(int, int)", IS_CTOR_OK);
      
       // uses windows of width 1
       // easy to test because map should always return 0.5 for
       // Hounsfield values inside the window
       final int width = 1;

       for (int level = Hounsfield.MIN_VALUE; level <= Hounsfield.MAX_VALUE; level++) {
           // make a window to call map on
           HounsfieldWindow w = new HounsfieldWindow(level, width);

           // the actual value returned by map
           double got = w.map(new Hounsfield(level));

           // make an error message in case the test fails
           String error = String.format(
                   "map(%s) failed to return the correct value for a window with level %s, width %s", level, level,
                   width);

           // assert that 0.5 equals got
           assertEquals(error, 0.5, got, 1e-9);
       }
   }

@Test
   public void test11_map() {
       assumeTrue("test requires a correct implementation of HounsfieldWindow(int, int)", IS_CTOR_OK);
      
       // tests Hounsfield units in windows of various widths and levels
       final int[] WIDTH = {2, 3, 4, 5, 10, 25, 50, 75, 100, 255};
       final int[] LEVEL = {-800, -1, 1, 750};
      
       for (int level : LEVEL) {
           for (int width : WIDTH) {
               // make a window to call map on
               HounsfieldWindow w = new HounsfieldWindow(level, width);
              
               // expected values map should return
               double[] EXP = HounsfieldWindowTest.mapValues(width);
              
               for (int i = 0; i < EXP.length; i++) {
                   // Hounsfield unit to map
                   Hounsfield h = new Hounsfield(level - width / 2 + i);
                  
                   // the expected return value of map(h)
                   double exp = EXP[i];
                  
                   // the actual value returned by map(h)
                   double got = w.map(h);
                  
                   // make an error message in case the test fails
                   String error = String.format(
                           "map(%s) failed to return the correct value for a window with level %s, width %s", h.get(), level,
                           width);

                   // assert that exp equals got
                   assertEquals(error, exp, got, 1e-9);
               }
           }
       }
   }

Relevant files(Hounsfield.java and Hounsfieldtest.java): https://drive.google.com/open?id=1HRUH5bika5xeLO7G_kP2LeMxNeXejRgl

Solutions

Expert Solution

You have not provide the Hounsfield class. Because of that I could not run the code. However I have gone through the class and fixed the code which may be causing the junits to fail.
I have fixed the 2 arg constructor to call setWidth() and also using 2.0 in getLowerBound() and getUpperBound(). Rest of the code seems to be correct.

Can you please check with the updated code given below and let me know through comments if you still see some issues. In case of issues, please provide the Hounsfield class in your google drive and post a comment. I will reply back to you. If the answer helped, please do rate it. Thank you.


/**
* A class that represents a windowed view of Hounsfield units. A Hounsfield
* window is defined by two values: (1) the window level, and (2) the window
* width. The window level is the Hounsfield unit value that the window is
* centered on. The window width is the range of Hounsfield unit values that the
* window is focused on.
*
*


* A window has a lower and upper bound. The lower bound is defined as the
* window level minus half the window width:
*
*


* lo = level - (width / 2)
*
*


* The upper bound is defined as the window level plus half the window width:
*
*


* hi = level + (width / 2)
*
*


* Hounsfield units are mapped by the window to a real number in the range of
* {@code 0} to {@code 1}. A Hounsfield unit with a value less than lo is mapped
* to the value {@code 0}. A Hounsfield unit with a value greater than hi is
* mapped to the value {@code 1}. A Hounsfield unit with a value v between lo
* and hi is mapped to the value:
*
*


* (v - lo) / width
*
*
*/
public class HounsfieldWindow {

   private int level;

   private int width;


   HounsfieldWindow(int level, int width) {
       setLevel(level);
       setWidth(width);
   }

   public HounsfieldWindow() {
       this(0, 400);
   }
   public int getLevel() {
       return level;
   }

   public int setLevel(int level) {
       if (level < Hounsfield.MIN_VALUE || level > Hounsfield.MAX_VALUE) {
           throw new IllegalArgumentException();
       }
       int data = this.level;
       this.level = level;
       return data;
   }

   public int getWidth() {
       return width;
   }

   public int setWidth(int width) {
       if(width < 1) {
           throw new IllegalArgumentException();
       }
       int data = this.width;
       this.width = width;
       return data;


   }

   public double getLowerBound() {
       return level - (width / 2.0);
   }
   public double getUpperBound() {
       return level + (width / 2.0));
   }

   public double map(Hounsfield h) {
       // TODO Auto-generated method stub
       int hounsfieldUnitVal = h.get();
       System.out.println("got " + hounsfieldUnitVal);
       if(hounsfieldUnitVal < getLowerBound()) {
           return 0;
       }
       if(hounsfieldUnitVal > getUpperBound()) {
           return 1;
       }
       return (hounsfieldUnitVal - getLowerBound()) / width;
   }


}


Related Solutions

Why am I getting this error using 000webhost , but I do not get this error...
Why am I getting this error using 000webhost , but I do not get this error running XAMPP? Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd2/006/14881006/public_html/test/index.php:1) in /storage/ssd2/006/14881006/public_html/test/index.php on line 8 Code is below. ******************************************************************************************************************** <!DOCTYPE html> <?php    //Determine if the submit button has been clicked to set the cookie name and value    if (isset($_POST['name'])){            $cookie_name = $_POST['name'];            $cookie_value = $_POST['value'];            setcookie($cookie_name, $cookie_value,...
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
PLEASE EXPLAIN ANSWER. USING JAVA via jGRASP i am getting an error messge a. Create a...
PLEASE EXPLAIN ANSWER. USING JAVA via jGRASP i am getting an error messge a. Create a class named Sandwich. Data fields include a String for the main ingredient (such as tuna), a String for bread type (such as wheat), and a double for price (such as 4.99). Include methods to get and set values for each of these fields. Save the class as Sandwich.java. b. Create an application named TestSandwich that instantiates one Sandwich object and demonstrates the use of...
HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt for the number of minutes of each Rental until the value falls between 60 and 7,200 inclusive. For one of the Rental objects, create a loop that displays Coupon good for 10percent off next rental as many times as there are full hours in the Rental. ///////////////////////////////RentalDemo package java1; import java.util.Scanner; public class RentalDemo { public static void main(String[] args) {    Rental object1 =...
Hi, Working on a project in my group for class and I am having some issues...
Hi, Working on a project in my group for class and I am having some issues My part is current state of the business. It is a store and the annual sales are $460,000 Other info I have is: Ownership and Compensation; Percent Ownership Personal Investment Mitchell George, Founder & CEO 25% $125,000Katie Beauseigneur, COO 15% $75,000 Melissa Dunnells, CFO15% $75,000 Also, a medium coffee price from store is $3.75 Sarah Griffin, Marketing Officer 10% $50,000 Katharina Ferry, HR Director10%...
Hi, I am doing up my homework on mathematics and here are some questions. I want...
Hi, I am doing up my homework on mathematics and here are some questions. I want to cross-reference my answers thank you. Decide if the statements below are True or False. Please include some rough workings for me to understand: (1) Mr. Tan borrowed $500,000 from Bank XYZ at 5% annual interest to be repaid monthly over 20 years. The amount that he pays back to XYZ each month is between $3000 to $3500. (2) Continuing from (1): after 15...
Hi, I would like to test a java program. I am learning linked list and going...
Hi, I would like to test a java program. I am learning linked list and going to make a linked lists for integer nodes. For instance, I am going to add the numbers 12, 13, and 16 to the list and then display the list contents and add 15 to the list again and display the list contents and delete 13 from the list and display the list contents and lastly delete 12 from the list and display the list...
Hi I am quite confused with how to use scanner for my java project? Can someone...
Hi I am quite confused with how to use scanner for my java project? Can someone help me with this? Thanks
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT