This lab assignment continues to give you practice using loops, particularly loops with variable termination conditions, and it also provides you an opportunity to use one-dimensional arrays.
Recall that an array is used to store a collection of data. The data can be values of Java primitive data types or else objects (for instance, String objects), but the data stored must all be of the same type. An array is created in Java using a statement of the form
BaseType[] arrayName = new BaseType[length];
where BaseType is the underlying type of data stored in the array, arrayName is just a variable name used to refer to the array, and length is a positive integer indicating the number of elements in the array. An array element can be referred to using arrayName[i], where i is an integer index (starting with 0). The length of the array is stored in the variable arrayName.length.
The arrays used in this assignment will all be one-dimensional (that is, they won’t be arrays of arrays), and they will all use Java primitive data types as the base type.
For the assignment, you are to create a program using two arrays of type double to store multiple values for x and y, where y is defined as the below function.
𝑦𝑦 = 20.0 ∗ |sin 𝑥𝑥|
It is assumed that the angles for the sine function are measured in radians. In the program, both the values for x and y are computed based on input from the user. Once the values are found, the program should print them out as indicated by the examples. The program should also print out a graphical representation of the function, using a sequence of ‘*’s to indicate the magnitude of y.
Lab Objectives
By the end of the lab, you should be able to:
• create nested loops and loops with variable termination conditions;
• create and manipulate one-dimensional arrays.
Prerequisites
The lab deals with material from Chapter 4 (loops) and Section 7.1 (arrays).
Instructions
1. The class should be called StarGraph.
2. You must include a comment stating your name, the date, program purpose, and containing a statement of academic honesty as shown in Lab 02. When writing, you must also abide by the Java formatting and naming conventions.
3. The program should prompt the user for the following information:
o the size N of the array (that is, the number values of x for which y will be computed); o xmin, a minimum value for x; o an increment for x (a value by which x is increased).
The messages to display to the user are shown in the examples below.
4. Note that the size of the arrays must be a positive integer. If the user enters 0 or a negative integer, then the program should print out an error message and terminate the program, as indicated in the examples.
5. The increment value should be a positive floating point value (a double). If the user enters 0 or a negative number, then the program should print an error message and terminate.
6. Once N, xmin, and the increment are established, the program should create two double arrays, one to hold the x values and one to hold the y values. The size of both arrays will be N.
The first element of the x-array will hold xmin, and each successive element will be incremented by the value provided by the user. The values for the y-array will be defined using the corresponding value from the x-array and the function defined earlier. To compute the value for y, the program should use the Math.abs and Math.sin methods.
Observe that in order to populate the arrays with values, loops must be used.
7. Once computed, the values of x and y should be printed to the console. Each pair of values for x and y stored in the arrays should be printed. Format the values of x and y to have 3 digits after the decimal point.
8. After the values have been printed, a simple graph of the function should be plotted using asterisks (*). Each successive line corresponds to an x value, and the number of ‘*’s on the line corresponds to the whole-number part of y (the fractional part is ignored/truncated). To compute the number of ‘*’s, you should not round the value for y (instead, truncate it).
eLC Submission and Grading
After you have completed and thoroughly tested StarGraph.java using both the examples included here and your own, submit the program to eLC in order to receive credit for the lab. Always double check that your submission was successful on eLC!
The lab will be graded according to the following guidelines.
• A score between 0 and 100 will be assigned.
• If the source file(s) are not submitted before the specified deadline’s late period ends (48 hours after the deadline), or if they do not compile a grade of 0 will be assigned.
• The program will be evaluated using examples in this document as well as others. For each test case, the output must be consistent with the examples shown below in order to receive credit.
• All instructions and requirements must be followed; otherwise, points maybe deducted.
Sample Input and Output
These are sample program runs. Input is underlined. Your output should be consistent with what is shown here. Each example is an execution of a correctly working program.
Example 1
Please enter the number of x values to process: 10
Enter a minimum value for x: 0
Enter the amount to increment x: 0.25
Values x: 0.000, y: 0.000 x: 0.250, y: 4.948 x: 0.500, y: 9.589 x: 0.750, y: 13.633 x: 1.000, y: 16.829 x: 1.250, y: 18.980 x: 1.500, y: 19.950 x: 1.750, y: 19.680 x: 2.000, y: 18.186 x: 2.250, y: 15.561
Graph
:
:****
:*********
:*************
:****************
:******************
:*******************
:*******************
:******************
:***************
Example 2
Please enter the number of x values to process: 10
Enter a minimum value for x: 0
Enter the amount to increment x: 0.31415926535
Values x: 0.000, y: 0.000 x: 0.314, y: 6.180 x: 0.628, y: 11.756 x: 0.942, y: 16.180 x: 1.257, y: 19.021 x: 1.571, y: 20.000 x: 1.885, y: 19.021 x: 2.199, y: 16.180 x: 2.513, y: 11.756 x: 2.827, y: 6.180
Graph
:
:******
:***********
:****************
:*******************
:********************
:*******************
:**************** :***********
:******
Example 3
Please enter the number of x values to process: 5
Enter a minimum value for x: 0
Enter the amount to increment x: 3.14159265359
Values x: 0.000, y: 0.000 x: 3.142, y: 0.000 x: 6.283, y: 0.000 x: 9.425, y: 0.000 x: 12.566, y: 0.000
Graph
:
:
:
:
:
Example 4
Please enter the number of x values to process: 9
Enter a minimum value for x: -1.5707963267
Enter the amount to increment x: 0.78539816339
Values x: -1.571, y: 20.000 x: -0.785, y: 14.142 x: 0.000, y: 0.000 x: 0.785, y: 14.142 x: 1.571, y: 20.000 x: 2.356, y: 14.142 x: 3.142, y: 0.000 x: 3.927, y: 14.142 x: 4.712, y: 20.000
Graph
:******************** :**************
:
:**************
:******************** :**************
:
:**************
:********************
Example 5
Please enter the number of x values to process: 20
Enter a minimum value for x: 0
Enter the amount to increment x: 0.25
Values x: 0.000, y: 0.000 x: 0.250, y: 4.948 x: 0.500, y: 9.589 x: 0.750, y: 13.633 x: 1.000, y: 16.829 x: 1.250, y: 18.980 x: 1.500, y: 19.950 x: 1.750, y: 19.680 x: 2.000, y: 18.186 x: 2.250, y: 15.561 x: 2.500, y: 11.969 x: 2.750, y: 7.633 x: 3.000, y: 2.822 x: 3.250, y: 2.164 x: 3.500, y: 7.016 x: 3.750, y: 11.431 x: 4.000, y: 15.136 x: 4.250, y: 17.900 x: 4.500, y: 19.551 x: 4.750, y: 19.986
Graph
:
:****
:*********
:*************
:****************
:******************
:*******************
:*******************
:****************** :***************
:***********
:*******
:**
:**
:*******
:***********
:***************
:*****************
:*******************
:*******************
Example 6
Please enter the number of x values to process: 20
Enter a minimum value for x: 0
Enter the amount to increment x: 0.78539816339
Values x: 0.000, y: 0.000 x: 0.785, y: 14.142 x: 1.571, y: 20.000 x: 2.356, y: 14.142 x: 3.142, y: 0.000 x: 3.927, y: 14.142 x: 4.712, y: 20.000 x: 5.498, y: 14.142 x: 6.283, y: 0.000 x: 7.069, y: 14.142 x: 7.854, y: 20.000 x: 8.639, y: 14.142 x: 9.425, y: 0.000 x: 10.210, y: 14.142 x: 10.996, y: 20.000 x: 11.781, y: 14.142 x: 12.566, y: 0.000 x: 13.352, y: 14.142 x: 14.137, y: 20.000 x: 14.923, y: 14.142
Graph
:
:**************
:******************** :**************
:
:**************
:******************** :**************
:
:**************
:******************** :**************
:
:**************
:******************** :**************
:
:**************
:********************
:**************
Example 7
Please enter the number of x values to process: 10
Enter a minimum value for x: -5
Enter the amount to increment x: 0.20
Values x: -5.000, y: 19.178 x: -4.800, y: 19.923 x: -4.600, y: 19.874 x: -4.400, y: 19.032 x: -4.200, y: 17.432 x: -4.000, y: 15.136 x: -3.800, y: 12.237 x: -3.600, y: 8.850 x: -3.400, y: 5.111 x: -3.200, y: 1.167
Graph
:*******************
:*******************
:*******************
:*******************
:*****************
:***************
:************
:********
:*****
:*
Example 8
Please enter the number of x values to process: 20
Enter a minimum value for x: -1.5707963267
Enter the amount to increment x: 0.15707963267
Values x: -1.571, y: 20.000 x: -1.414, y: 19.754 x: -1.257, y: 19.021 x: -1.100, y: 17.820 x: -0.942, y: 16.180 x: -0.785, y: 14.142 x: -0.628, y: 11.756 x: -0.471, y: 9.080 x: -0.314, y: 6.180 x: -0.157, y: 3.129 x: 0.000, y: 0.000 x: 0.157, y: 3.129 x: 0.314, y: 6.180 x: 0.471, y: 9.080 x: 0.628, y: 11.756 x: 0.785, y: 14.142 x: 0.942, y: 16.180 x: 1.100, y: 17.820 x: 1.257, y: 19.021 x: 1.414, y: 19.754
Graph
:********************
:*******************
:*******************
:*****************
:**************** :**************
:***********
:*********
:****** :*** :
:***
:******
:*********
:***********
:**************
:****************
:*****************
:*******************
:*******************
Example 9
Please enter the number of x values to process: 10
Enter a minimum value for x: 1.57079632679
Enter the amount to increment x: 1.57079632679
Values x: 1.571, y: 20.000 x: 3.142, y: 0.000 x: 4.712, y: 20.000 x: 6.283, y: 0.000 x: 7.854, y: 20.000 x: 9.425, y: 0.000 x: 10.996, y: 20.000 x: 12.566, y: 0.000 x: 14.137, y: 20.000 x: 15.708, y: 0.000
Graph
:********************
:
:********************
:
:********************
:
:********************
:
:******************** :
Example 10
Please enter the number of x values to process: 0 The number of x values must be an integer greater than 0.
Example 11
Please enter the number of x values to process: 100 Enter a minimum value for x: -50
Enter the amount to increment x: 0
The increment must be a decimal number greater than 0.