Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Based on chapter 6 (Methods). Write a program that has 3 methods. CIT130-Assi

ID: 3915990 • Letter: 2

Question

2. Based on chapter 6 (Methods). Write a program that has 3 methods. CIT130-Assignment 3 – Chapters 5, 6&7 Page 2 Method 1: This method accepts the number of lines as its parameter for creating the following pattern and returning the total of all of the numbers. The method simply displays the pattern and returns the total. For the given example, the number of lines is 4. I see that I “loops” come in handy to solve this problem! 4 3 2 1 3 2 1 2 1 1 If the number of lines is more than 8, do not display a pattern and display a simple error message. For this example, the total will be calculated and returned as 20. Method 2: This method that accepts 3 integer parameters. Calculate and return the product of all integers between (inclusive) the first and second number with the step size identified by the third number. If the parameter values are 1, 10, 3, the method returns 1*4*7*10 = 280. If the parameter values are -5, 7, 2, the product should be: -5*-3*-1*1*3*5*7 = -1575. This method does not display the product; it just returns the value of the calculated product. Method 3: This method accepts one integer parameter representing the number of test scores to be averaged. Inside the method, ask the user for the indicated number of test scores and calculate and return the average of the numbers. These test scores can be any number (decimal or floating point). Inside the method, return -1 if the number of items is 1 or less. For example, if the value of the parameter is -2, the method returns -1. If the value of the parameter is 5, the method should ask for 5 test scores and return the average. This method does not display the average; it just returns the value of the calculated average or it returns the error code -1. In a main method, call these methods and show how each method will be called. does any one know how to write a java code for this question?

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// Methods.java

import java.util.Scanner;

public class Methods {

                /**

                * method to accept number of lines as input, and display a specific pattern

                * described in the question

                *

                * @param numLines

                *            - number of lines

                * @return - the sum total of all numbers in the pattern combined, will not

                *         display the pattern if number of lines is greater than 8, but the

                *         total value is still returned

                */

                static int method1(int numLines) {

                                int total = 0;

                                int line = numLines;

                                boolean showPattern = true;// to determine if the pattern is to be

                                                                                                                                                // printed or not

                                if (numLines > 8) {

                                                System.out

                                                                                .println("Number of lines is more than 8, pattern not displayed!");

                                                showPattern = false;

                                }

                                /**

                                * loop until all lines are printed/calculated

                                */

                                while (line > 0) {

                                                // setting up an inner loop to print numbers from 'line' to 1

                                                for (int i = line; i > 0; i--) {

                                                                if (showPattern)

                                                                                System.out.print(i + " ");

                                                                total += i;

                                                }

                                                if (showPattern)

                                                                System.out.println();

                                                line--;

                                }

                                return total;

                }

                /**

                * method to find the product of numbers between the given start and end

                * values incremented by the given step size every time

                *

                * @param start

                *            - start value (inclusive)

                * @param end

                *            - end value (inclusive)

                * @param step

                *            - step size

                * @return the product, -1 if start>end

                */

                static int method2(int start, int end, int step) {

                                if (start > end) {

                                                // start is greater than end

                                                return -1;

                                }

                                if (start == end) {

                                                // base case, returning the number

                                                return start;

                                }

                                /**

                                * returning the current number * next number until last number

                                * [recursively calling]

                                */

                                return start * method2(start + step, end, step);

                }

                /**

                * method to accept given number of test scores from user and return the

                * average

                *

                * @param numTests

                *            - number of tests

                * @return average if number of tests>1 else -1

                */

                static double method3(int numTests) {

                                if (numTests <= 1) {

                                                //invalid number of tests

                                                return -1;

                                }

                                double total = 0, average = 0;

                                //scanner to read user input

                                Scanner scanner = new Scanner(System.in);

                                /**

                                * looping, and adding up all numbers

                                */

                                for (int i = 0; i < numTests; i++) {

                                                System.out.print("Enter test " + (i + 1) + " score: ");

                                                total += Integer.parseInt(scanner.nextLine());

                                }

                                //finding and returning the average

                                average = (double) total / numTests;

                                return average;

                }

                public static void main(String[] args) {

                                /**

                                * testing all three methods

                                */

                                System.out.println("Testing method 1 passing 4 as argument");

                                int total = method1(4);

                                System.out.println("Total is : " + total);

                                System.out.println("Testing method 1 passing 9 as argument");

                                total = method1(9);

                                System.out.println("Total is : " + total);

                                System.out.println("Testing method 2 passing 1, 10, 3 as arguments");

                                int product = method2(1, 10, 3);

                                System.out.println("Product: " + product);

                                System.out.println("Testing method 3 passing 4 as argument");

                                double average = method3(4);

                                System.out.println("Average: " + average);

                }

}

/*OUTPUT*/

Testing method 1 passing 4 as argument

4 3 2 1

3 2 1

2 1

1

Total is : 20

Testing method 1 passing 9 as argument

Number of lines is more than 8, pattern not displayed!

Total is : 165

Testing method 2 passing 1, 10, 3 as arguments

Product: 280

Testing method 3 passing 4 as argument

Enter test 1 score: 100

Enter test 2 score: 99

Enter test 3 score: 95

Enter test 4 score: 89

Average: 95.75