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...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
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 =...
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
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...
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)...
Java Program: You will be inserting values into a generic tree, then printing the values inorder,...
Java Program: You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT