Homework Help Question & Answers

JAVA Directions: Write a unit test for addInventory(), which has an error. Call redSweater.addInventory() with parameter...

JAVA

Directions:

Write a unit test for addInventory(), which has an error. Call redSweater.addInventory() with parameter sweaterShipment. Print the shown error if the subsequent quantity is incorrect. Sample output for failed unit test given initial quantity is 10 and sweaterShipment is 50:

Beginning tests.
   UNIT TEST FAILED: addInventory()
Tests complete.

Note: UNIT TEST FAILED is preceded by 3 spaces.

What I have so far:

// ===== Code from file InventoryTag.java =====
public class InventoryTag {
private int quantityRemaining;

public InventoryTag() {
quantityRemaining = 0;
}

public int getQuantityRemaining() {
return quantityRemaining;
}

public void addInventory(int numItems) {
if (numItems > 10) {
quantityRemaining = quantityRemaining + numItems;
}
}
}
// ===== end =====

// ===== Code from file CallInventoryTag.java =====
import java.util.Scanner;

public class CallInventoryTag {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
InventoryTag redSweater = new InventoryTag();
int sweaterShipment;
int sweaterInventoryBefore;

sweaterInventoryBefore = redSweater.getQuantityRemaining();
sweaterShipment = scnr.nextInt();

System.out.println("Beginning tests.");

// FIXME add unit test for addInventory

if (redSweater.getQuantityRemaining() != sweaterShipment+sweaterInventoryBefore )
{
System.out.println(" UNIT TEST FAILED: addInventory()");
}

System.out.println("Tests complete.");
}
}
// ===== end =====

Error:

Inventory is 0, shipment input is 25. Testing that quantityRemaining was updated to 25.

Value differs. See highlights below.

Your value

0

Expected value

25

Testing with sweaterShipment input of 25. addInventory updates quantityRemaining.

Output differs. See highlights below. Special character legend

Your output

Beginning tests. UNIT TEST FAILED: addInventory() Tests complete.

Expected output

Beginning tests. Tests complete.

Inventory is 25, shipment input is 5. Testing that quantityRemaining remains 25.

Value differs. See highlights below.

Your value

0

Expected value

25

0 0
Next > < Previous
ReportAnswer #1
// ===== Code from file InventoryTag.java =====
  public class InventoryTag {
    private int quantityRemaining;

    public InventoryTag() {
      quantityRemaining = 0;
    }

    public int getQuantityRemaining() {
       return quantityRemaining;
    }

    public void addInventory(int numItems) {
      if (numItems > 10) {
         quantityRemaining = quantityRemaining + numItems;
      }
    }
  }// ===== end =====



   // ===== Code from file CallInventoryTag.java =====
    public class CallInventoryTag {
       public static void main (String [] args) {
           InventoryTag redSweater = new InventoryTag();
           int sweaterShipment;
           int sweaterInventoryBefore;

           sweaterInventoryBefore = redSweater.getQuantityRemaining();
           sweaterShipment = 25;

           System.out.println("Beginning tests.");

           // FIXME add unit test for addInventory
           System.out.println("Tests complete.");
       }

     }// ===== end =====

Your code should be located in src/main for source code and src/test for tests. Then when you add a test for a class A in package a; and located at src/main then you write ATest in package a; located in src/test.

in your example the test class should look similar to:

public class CallInventoryTagTest {
   @Test(expected=YourException.class)
   public static void shouldThrowYourExceptionWhenX () {
       //given
       InventoryTag redSweater = new InventoryTag();
       int sweaterShipmen=25;
       int sweaterInventoryBefore;
       //when
       // that's what you need to write after your FIXME
       sweaterInventoryBefore = redSweater.getQuantityRemaining(); 
       redSweater.addInventory(sweaterShipmen)  //calling addinventor with parameter sweaterShipment
       //then
       fail("should throw an error because of X");
   }

 }

that InventoryTag is not inited successfully when a number less than or equal to 10 is provided.

Something like:

// ===== Code from file CallInventoryTag.java =====
public class CallInventoryTag {
   public static void main (String [] args) {
       InventoryTag redSweater = new InventoryTag();
       int sweaterShipment;
       int sweaterInventoryBefore;

       sweaterInventoryBefore = redSweater.getQuantityRemaining();
       sweaterInventoryBefore = 10;
       sweaterShipment = 50;

       System.out.println("Beginning tests.");

       // FIXME add unit test for addInventory
       redSweater.addInventory(sweaterInventoryBefore);
       redSweater.addInventory(sweaterShipment);

       if (sweaterInventoryBefore + sweaterShipment != redSweater.getQuantityRemaining()) {
           System.out.println(" UNIT TEST FAILED: addInventory()");
       }

       System.out.println("Tests complete.");
   }

 }// ===== end =====
Know the answer?
Add Answer of:
JAVA Directions: Write a unit test for addInventory(), which has an error. Call redSweater.addInventory() with parameter...
Your Answer: Your Name: What's your source?
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
  • I keep getting an Error after I ask the user for test scores. What is missing?...

    I keep getting an Error after I ask the user for test scores. What is missing? package testscores; import java.util.Scanner; public class TestScores { /** * @param args the command line arguments */ private double[] scores;    public TestScores(double[] score) throws IllegalArgumentException { scores = new double[scores.length]; for (int i = 0; i < scores.length; i++) { if (score[i] < 0 || score[i] > 100){ throw new IllegalArgumentException(); } scores[i]=score[i];    }    } public double getAverage() { int sum=0;...

  • Appliance class involving arrays

    A Power Usage Simulation System needs to be able to manage a collection of appliances(ApplianceSet). Along with standard constructors and toString method, the collection class should havemethods to:add an appliance to the collectionfind an appliance by its unique system generated IDdelete an appliance by its unique system generated IDUse the following test program to test your collection class (add more testing to it). Either match themethod signatures shown here in your ApplianceSet, or update the teest program to match yours.The...

  • Java Help ASAP!!! Instant Lifesaver

    import java.util.Scanner;public class Lab3_Ex1{public static void main (String [] args){Scanner keyboard = new Scanner(System.in);float x;int y;char ch1, ch2;String name;String input;System.out.print( "Enter a character: ");input = keyboard.next();ch1 = input.charAt(0);System.out.print( "Enter a number: ");y = keyboard.nextInt();System.out.print( "Enter another character:");input = keyboard.next();ch2 = input.charAt(0);System.out.print( "Enter a name:");name = keyboard.next();System.out.print( "Enter a floating point value:");x = keyboard.nextFloat();System.out.println("nch1 = " + ch1);System.out.println( "y = " + y);System.out.println("ch2 = " + ch2);System.out.println( "Name is " + name);System.out.println( "x = " + x);System.exit(0);}}Re-write the above program to...

  • Java

    Complete addition of print statements to each method and constructor, to the supplied code.public class CalcEngine{// The value in the display.private int displayValue;// The previous operator typed (+ or -).private char previousOperator;// The left operand to previousOperator.private int leftOperand;/*** Create a CalcEngine instance.*/public CalcEngine(){displayValue = 0;previousOperator = ' ';leftOperand = 0;}/*** Return the value currently displayed* on the calculator.*/public int getDisplayValue(){return displayValue;}/*** A number button was pressed.*/public void numberPressed(int number){System.out.println("numberPressed called with: " +number);displayValue = displayValue * 10 + number;reportState("end...

  • Average an array in Java

    (averaging an array) Write two overlooked methods that return the average of an array with the following headers:public static int average(int[] array)public static double average(double[] array)Write a test program that prompts the user to enter ten double values, invokes this method, and displays the average value.Below is what I have written and the error I keep getting. Any help is appreciatedimport java.util.*;public class Exercise06_08 {public static int average(int[] array){int sum=0;int average=0;for(int i=0;i<array.length;i++){sum=sum+array[i];}average=sum/array.length;return average;}public static double average(double[] array){double sum=0;double average=0;for(int i=0;i<array.length;i++){sum=sum+array[i];}average=sum/array.length;return...

  • JAVA~~~ Could you fix the error~~~

    x.øi5ce="Times New Roman" color="red" size="3">I want to program thatif I choose "1" then ask the user to load the value at index 0-9 of the "A" arayand if i choose "2" add all the elements of "A" array...**once the selected option is processed, I want go back to MENU back...import java.io.*;import java.util.Scanner;public class assign{public static void main(String[] args){{System.out.println("*********************************************************");System.out.println("*ArrayMenu*");System.out.println("**");System.out.print("*1.Load the values in ");System.out.print(""");System.out.print("A");System.out.print(""");System.out.print(" array");System.out.println("*");System.out.print("*2.Add all th elements of");System.out.print(" "");System.out.print("A");System.out.print(""");System.out.print(" array");System.out.println("*");System.out.print("*3.Find maximum value in ");System.out.print(" "");System.out.print("A");System.out.print(""");System.out.print(" array");System.out.println("*");System.out.print("*4.Find munimum value in ");System.out.print(" "");System.out.print("A");System.out.print(""");System.out.print("...

  • Write a do-while loop that continues to prompt a user to enter a number less than...

    Write a do-while loop that continues to prompt a user to enter a number less than 100, until the entered number is actually less than 100. End each prompt with newline Ex: For the user input 123, 395, 25, the expected output is: Enter a number (<100): Enter a number (<100): Enter a number (<100): Your number < 100 is: 25 import java.util.Scanner; public class NumberPrompt { public static void main (String [] args) { Scanner scnr = new Scanner(System.in);...

  • Java Program: counting words in a separate text file

    I am writing a program that reads words in a text file, but I am getting an error when input the complete file path to the text file, family.txt. I have made surethe file is saved in the path and that the text file is not open in memory. I have also tried other text files too. This text file is actually the file created bythe text book.I have the following code that is supposed to count the number of...

  • Java Fractions

    Create a class called Fraction.Provide a constructor that takes 2 integers.Provide methods for toString, setFraction, equals, add, subtract, multiple, and divide.See if you can figure out a way to reduce your fraction.The following will be your starting point:import java.util.*;public class Fraction{private Scanner scan = new Scanner(System.in);private int num=1;private int denom=1;public Fraction(){}public Fraction(int n, int d){// Fill in code (good to use setFraction)}public void setFraction(int n, int d){//Fill in code ... don't forget to reduce}public Fraction add(Fraction op){//Fill in code ......

  • Write a method that parses a binary number as a string into a hex number. The method header is...

    So far I have this but certain inputs do not produce the right output. For example, when I input 0101, the output is 65 instead of 5. When I input 1010 I get 3f2instead of A. What should my final code look like?import java.util.Scanner;import java.io.IOException;public class YourProgram {public static void main(String[] args) throws IOException{//Create Scanner objectScanner scan = new Scanner(System.in);//PromptSystem.out.print("Enter an integer value: ");int myInt = scan.nextInt();//ConvertString retval = binaryToHex(Integer.toBinaryString(myInt));System.out.println("Hex value: " + retval);}public static String binaryToHex(String binaryValue) throws IOException{long...

Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
Share Your Knowledge

Post an Article
Post an Answer
Post a Question with Answer

Self-promotion: Authors have the chance of a link back to their own personal blogs or social media profile pages.