Lab 08
Lab 08
Lab 08
Introduction
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
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.
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).
What to Submit
The file StarGraph.java should be submitted to eLC for grading.
Instructions
1. The class should be declared as public and 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).
Additional Requirements
These are things that make the graders lives easier, and ultimately, you will see in the real world
as well. Remember the teaching staff does not want to touch your code after they gave you
requirements; they want to see the perfect results they asked for! Here is a checklist of things you
can lose points for:
• (-1 point) If the source file(s)/class(es) are named incorrectly (case matters!)
• (-1 point) If your source file(s) have a package declaration at the top
• (-1 point) If any source file you submit is missing your Statement of Academic Honesty
at the top of the source file. All submitted source code files must contain your Statement
of Academic Honesty at the top of each file.
• (-1 point) If you have more than one instance of Scanner in your program. Your program
should only have one instance of Scanner.
• (-1 point) Inconsistent I/O (input/output) that does not match our instructions or examples
exactly (unless otherwise stated in an assignment’s instructions). Your program’s I/O
(order, wording, formatting, etc.) must match our examples and instructions.
If any of the above do not make sense to you, talk to a TA or ask your lab instructor.
• Students may earn 3 points for lab attendance and 7 points if their program passes various test
cases that we use for grading. Some test cases may use values taken from the examples in this
document and some test cases may not. You should come up with additional test cases to check
that your program is bug free.
• All source code must adhere to good Java programming style standards as discussed in lecture
class and in the course textbook readings.
• Points will be deducted for not following any of the aforementioned instructions or requirements.
Example 1
Values
x: 0.000, y: 0.000
x: 0.250, y: 4.948
Graph
:
:****
:*********
:*************
:****************
:******************
:*******************
:*******************
:******************
:***************
Example 2
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
:
:******
:***********
:****************
:*******************
:********************
:*******************
:****************
:***********
:******
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
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
:********************
:**************
:
:**************
:********************
:**************
:
:**************
:********************
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
:
:****
:*********
:*************
:****************
:******************
:*******************
:*******************
:******************
:***************
:***********
:*******
:**
:**
:*******
:***********
:***************
:*****************
:*******************
:*******************
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
:
:**************
:********************
:**************
:
:**************
:********************
:**************
:
:**************
:********************
:**************
:
:**************
:********************
:**************
:
:**************
:********************
:**************
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
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
Graph
:********************
:*******************
:*******************
:*****************
:****************
:**************
:***********
:*********
:******
:***
:
:***
:******
:*********
:***********
:**************
:****************
:*****************
:*******************
:*******************
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
Example 11