In JAVA: You should be comfortable with the content in the modules up to and inc
ID: 3770878 • Letter: I
Question
In JAVA:
You should be comfortable with the content in the modules up to and including the module "Methods" for this project.
For this project, you will create a program that asks the user to enter a positive integer value less than 20. Note that the program will only collect one number from the user. All other numbers will be generated by the program.
If the user enters a number greater than 20, the user should get an error.
If the user enters a number equal to or less than 20, display the double of each value beginning with 1 up to the selected number (multiply each number by 2), then provide the total of all doubles.
For example, if the user entered the number 5, the following should display:
Double up 1 = 2
Double up 2 = 4
Double up 3 = 6
Double up 4 = 8
Double up 5 = 10
Total = 30
Minimum Requirements:
Create a separate class that contains the following methods. Note: This class should be separate and apart from the public class that holds the main method. (5 points)A method that will accept two parameters: an integer array and a integer variable.
The integer variable should hold the value provided by the user. The array will be used to hold the double up results.
This method should perform the calculations, and store the double up results in the array. Additionally, the total of all double up values should be calculated using an accumulator variable that is also a private instance variable.
You must make sure the calling method gets the changes to the array. Remember, arrays are passed by reference by default. This method must use a loop. (10 points)
A separate method that will return (not display) the value stored in the private instance variable called total.(5 points)
Create a main method that creates an array and integer variable, as well as an instance of your class. The main method should then use the instance to call each of the methods of the class, passing the data needed and handling any return data. Display the entire contents of the array and total variable (5 points).
Explanation / Answer
Complete Program:
// File: Methods.java
public class Methods
{
private int total;
public Methods()
{
total = 0;
}
public void doublesUp(int[] arr, int n)
{
for(int i = 0; i < n; i++)
{
arr[i] = (i + 1) * 2;
total += arr[i];
}
}
public int getTotal()
{
return total;
}
}
// File: MethodsTest.java
import java.util.*;
public class MethodsTest
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a positive integer value less than 20: ");
int n = input.nextInt();
if(n > 0 && n < 20)
{
int[] arr = new int[n];
Methods methods = new Methods();
methods.doublesUp(arr, n);
for(int i = 0; i < n; i++)
{
System.out.println("Double up " + (i + 1) + " = " + arr[i]);
}
System.out.println("Total = " + methods.getTotal());
}
else
{
System.out.println("Error: " + n + " is not a positive integer value less than 20");
}
}
}
Sample Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.