Question: //import java.util.Arrays; public class FinalSpring2018 { public static void main(String[] args) { //call methods with their arguments here //call evenNumbers() with numbers array argument int[]
//import java.util.Arrays;
public class FinalSpring2018 {
public static void main(String[] args) { //call methods with their arguments here
//call evenNumbers() with numbers array argument int[] numbers = {5, 7, 8, 4,6}; //
drawParallelogram(); //call drawParallelogram() with argument 10
System.out.println(); //call decimal2hex() with argument 255
System.out.println(); //call getFactors() with argument 128
System.out.println(); //call reportCommonFactors() with arguments (15, 30)
System.out.println(); //call factorial() with argument 20
} // end main
//1)Write method evenMumbers(), which takes a single int[] //parameter array, 'numbers'.Call evenNumbers() in main with //a argument array of size at least 10, that has some even //numbers. Count the # of even numbers in the array and //return the count. Hint: use the remainder operator, %, to //determine if the number at index i is even or odd. //Remember 10 % 2 == 0, because 2 divides 10 evenly 5 times.
//2) Method drawParallelogram() draws a parallelogram figure with //'width' specified as parameter. Draw lines,each indented 2 //spaces farther to right than the previous line. Continue drawing //new lines until the last line starts at a position one greater //that the ending position of the first line. Hard code '*' for //line characters. Hint: Use two different loops. Let the first //for loop construct a trapezoid line( type String) of length //width. Then use this line in a second indefinite while() loop //to draw parallelogram lines. Draw lines in the while() loop //while the length of the line(line.length) is greater than the //number of indentation spaces for the next line. Increment # of //indentation spaces by 2 each iteration. Use the String method //length() to keep track of the number of indentation spaces. //Initially set String spaces = ?, to empty. // // ********** // ********** // ********** // ********** // ********** // **********
//3) Method decimal2hex() take a single int parameter representing a //decimal number. The method convert a decimal number into a //facsimile of hexadecimal(base 16) number, represented by a char[] //array of characters. Simplify the algorithm by creating a char[] //array of size 4. The method returns the arrays of characters. //Use a cascading if-else to explicitly assign a hexadecimal char to //the char[] array at index i, based upon the value of the statement, //remainder = decimal % 16, for each loop iteration, e.g. // // if( remainder == 0 ) // hexadecimal[i] = '0'; // ........ // // else if( remainder == 10 ) // hexadecimal[i] = 'A'; // //hexadecimal digits are {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}
//4) Write method factors() that takes a single int typed decimal //number, with the restriction that the input number is NOT prime. //Return all factors of the number as a sequence of numbers held in //an array of int[]. Declare the local int[] array with size 10. //Since all elements of the int[] factors array will be initialized //to 0, and 0 cannot be a factor, consider any element with value of //0 as not being used. Use a counted for-loop, to calculate the //factors. Also include the number as a factor of itself. Copy and //paste from the Prime number example discussed in class; re-factor //it to write method factors().
//5) Use(call) method factors() developed in question 4 in a second //method, reportCommonFactors(), that take two int parameters, that //are compared to one another for common factors. Print common factors //in the method, ignoring any 0s. in int[] arrays returned from //calling factors(). reportCommonFactors() does not return an explicit //value, i.e its return type is void. Use nested-for-loops to compare //each factor from the first parameter to each factor from the second //parameter.
//6) Write a factorial method that takes a single parameter, int N, //the maximum numberused to calculate a factorial sequence. Print //the factorial table up to number N. The factorial of N is defined // as: // // factorial(N) = factorial(N-1) * N // factorial(0) = 0, factorial(1) = 1. // //Use an internal int[] array, 'fact' of long[N + 1] to keep track of //computed factorial values. Print the values in rows of 5, formatted //so: '(number)factorial' + '\t'. Each field should be separated //by the tab '\t' character to keep values aligned in columns. Use an //if-else statement and a counter to keep track of when newlines are //needed to start another row of values. Reset the counter = 0 after //each new line. Return the factorial array to the calling method. //Since the array is of size N+1, and indexing starts a 0, //factorial(N) will be stored at index [N]. Explicitly set //fact(0) = 1, fact(1) = 1 before entering the loop. Initialize the //for loop index at i = 1. Think about when the loop(test) terminates //carefully.
} // end class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
