Question

Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice)....

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!
}

Homework Answers

Answer #1

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:

=================

Know the answer?
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
Similar Questions
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text...
Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text editor of your choice). This program will “explode” a String into an array of characters (char[]), and then print out the array. The String comes from the first command-line argument. Example output with the command-line argument foo is below: f o o --------------------------------------------------------------------- public class StringExplode { // TODO - write your code below this comment. // You will need to write a method...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Step 1: Get OperationsBetween.java Compiling Download the OperationsBetween.java file, and open it in jGrasp (or a...
Step 1: Get OperationsBetween.java Compiling Download the OperationsBetween.java file, and open it in jGrasp (or a text editor of your choice). This program takes two command-line arguments representing the minimum and maximum number in a numeric range. Currently, the code does not compile. As a first step, you need to get this code to compile. To this end, you'll need to implement the following: Instance variables Constructor A “stub” for the sum method, which will allow the call to sum...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1 2 3 4 import java.util.Scanner; public class ForLoops { public static void main (String [] args) { int userNum; int i; Scanner input = new Scanner(System.in); userNum = input.nextInt(); for (/* Your code goes here */) { System.out.print(i + " "); } } } Need to fill out the "for" solved in JAVA
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering...
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering “Scanner in = new Scanner (System.in);”, input a value into element 5 of one-dimensional double array nums. c) Initialize each of the four elements of the one-dimensional integer array test to 7. d) Declare and create an array called table as a float array that has four rows and three columns. e) Considering the following ArrayList declaration, insert “test” at the fourth position (index...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int top ; String [] stack ; public Stack2540Array () { stack = new String [ CAPACITY ]; top = -1; } 1 3.1 Implement the stack ADT using array 3 TASKS public int size () { return top + 1; } public boolean isEmpty () { return (top == -1); } public String top () { if ( top == -1)...
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT