Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice). This program will read in three integers with Scanner, put the values in an array of int, and then print the product of the three values. Example output of the program is shown below, with user input shown in bold:
Enter first integer: 3 Enter second integer: 4 Enter third integer: 5 Product: 60
More details about the method you need to write are in the comments of ProductUpTo3.java. Note that the method you need to write must handle input arrays holding fewer than three elements! The next step will check that additional behavior.
import java.util.Scanner; public class ProductUpTo3 { // TODO - write your code below this comment. // You will need to write a method that will take // an array of int, and will return the product of // the first three elements. NOTE THAT // THE ARRAY LENGTH MAY BE SMALLER THAN 3. // In the event that the array isn't long enough, // substitute 1s for the missing elements. // For example: // - if the array is empty you should return 1 (1 * 1 * 1 = 1), // - if the array contains only one element, you should // return that element (element * 1 * 1 = element) // - if the array contains only two elements, you // should return the product of those two elements // (first * second * 1 = first * second) // - if the array contains three or more elements, // you should return the product of the first three. // // You may be given an array which holds more than // three elements. These extra elements (beyond three) // should be ignored. // // As a hint, switch may be useful here (though you // are not required to use it). // // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); int[] array = new int[3]; System.out.print("Enter first integer: "); array[0] = input.nextInt(); System.out.print("Enter second integer: "); array[1] = input.nextInt(); System.out.print("Enter third integer: "); array[2] = input.nextInt(); System.out.println("Product: " + productUpToFirst3(array)); } }
After completing this step download the ProductUpTo3Test.java file, being sure to put it in the same folder/directory as your ProductUpTo3.java file. This file contains tests for the methods you wrote in the previous step. Open this file in jGrasp as a test file, using the same instructions you've used in previous labs. You need to write a number of tests in this file, and all of them must pass. The comments in the file provide more details.
import static org.junit.Assert.assertEquals; import org.junit.Test; public class ProductUpTo3Test { @Test public void testLength0() { assertEquals(ProductUpTo3.productUpToFirst3(new int[]{}), 1); } // TODO - write your code below this comment. // You will need to write at least FOUR tests for the // method you wrote in ProductUpTo3.java. Each test should // test a different behavior of this method. // // If you're not sure you're testing all the // behaviors, don't hesitate to ask! }
Code is Given Below:
=========================
ProductUpTo3.java
===========================
import java.util.Scanner;
public class ProductUpTo3 {
//method defination
public static int productUpToFirst3(int array[]) {
int product=1;
//check if length of array is 0
//if(array.length==0 || array==null)
// product=1;
//check if length of array is 1
if(array.length==1)
product=array[0];
//check if length of array is 2
if(array.length==2)
product=array[0]*array[1];
//check if length of array is greater than 3
if(array.length>=3)
product=array[0]*array[1]*array[2];
return product;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] array = new int[3];
System.out.print("Enter first integer: ");
array[0] = input.nextInt();
System.out.print("Enter second integer: ");
array[1] = input.nextInt();
System.out.print("Enter third integer: ");
array[2] = input.nextInt();
System.out.println("Product: " + productUpToFirst3(array));
}
}
ProductUpTo3Test.java
============================
import static org.junit.Assert.assertEquals;
import org.testng.annotations.Test;
public class ProductUpTo3Test {
@Test
public void testLength0() {
assertEquals(ProductUpTo3.productUpToFirst3(new int[]{}), 1);
}
//creating test cases
@Test
public void testLength1() {
assertEquals(ProductUpTo3.productUpToFirst3(new int[]{5}),
5);
}
@Test
public void testLength2() {
assertEquals(ProductUpTo3.productUpToFirst3(new int[]{2,3}),
6);
}
@Test
public void testLength3() {
assertEquals(ProductUpTo3.productUpToFirst3(new int[]{2,3,4}),
24);
}
@Test
public void testLength4() {
assertEquals(ProductUpTo3.productUpToFirst3(new int[]{8,4,2,2}),
64);
}
}
Output: of ProductUpTo3
==========
Output of ProductUpTo3Test
=======================
Code Snapshot:
=================
Get Answers For Free
Most questions answered within 1 hours.