辅导案例-CSC116

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
!"#$!%&' ( )*%+,-"%&. ( /0)1"2&+'+ ( )343)3&'.567-0789%: ( )343);;<)3=8 ( )343);;<)3=8)>&"?'16)*'+1&5@65"A
343)3&'.567-0789%:
343);;<)3=8)>&"?'16)*'+1&5@65"A
CSC116: Credit-By-Exam Program
Polynomial
Learning Objectives
The following is a list of learning outcomes that are covered in this assignment:
Apply classic problem-solving techniques to simple computational and information-management problems, specifically:
Breaking large problems into smaller ones,
Sequential analysis of solution steps,
Logical analysis of alternative cases,
Use a programming language to write code that selects one of several alternatives based on more than one predicate,
Use a programming language to write a loop whose exit depends on more than one predicate,
Find and correct logical programming errors using debugging printout, pencil-and-paper tracing, and systematic search (to located where an incorrect decision or value first appears),
Implement an object-oriented design that has at least two interacting classes,
Write and document programs that adhere to specific coding and documentation standards (e.g., Javadoc for documentation; conventions regarding the naming of classes and methods, definition of constants, indention, etc.), and
Construct and use arrays with one and two dimensions.
Polynomial Gradesheet
Value Description
+10 points Polynomial program compiles
+40 points Program passes teaching staff black box tests and contains proper formatting of output (-5 points ifoutput is not formatted nicely)
+35 points Program passes automated tests
+5 points Polynomial program files meet CSC Department style guidelines
+5 points Polynomial program files contain correct Javadoc comments as per the CSC Department guidelines
+5 points Polynomial program files contain NO magic numbers

TOTAL
Polynomial
In this Assignment you will learn a method for manipulating polynomials: computing values at various points, computing differences over an interval, and finding roots.
Program Requirements
Your program will begin by printing the message "Type in the polynomials in increasing powers:". It will then read a sequence of floating-point numbers, one per line, ending with a blank line. These numbers define a polynomial
a0 x0 + a1 x1 + ... + an-1 xn-1 = y
where a0, a1, ..., an-1 are the n input numbers provided by the user. The polynomial's coefficient input is stopped by the user entering a blank line.
If the user enters a non-double value for a polynomial's coefficient, the program should print "Invalid input, please try again.", and wait for additional coefficient input or a blank line.
After the blank line, the program will print the polynomial in the following format:
an-1 x^n-1 + ... + a1 x^1 + a0 x^0
where an-1, ..., a0 and n-1 are actual numbers and the +'s are replaced by –'s when the corresponding coefficients are negative. In other words, if you enter -3 and 2 for coefficients, your program should print 2 x^1 - 3 x^0 instead of 2 x^1 + –3 x^0. Terms with coefficients of 0 should be omitted. If
the coefficient associated with the highest power is negative, then the coefficient should be printed as follows: -4 x^4.
The program will then make a sequence of input requests, each having three parts. Each request looks like the following where the user response is in red italics:
Type in the range:
Lower Bound (double): start
Upper Bound (double): end
Number Of Intervals (int): intervals
The input for the lower and upper bounds should be doubles. If the user enters a non-double value, the program should print "Invalid input", and reprompt the user for the specific value. The input for the number of intervals should be an integer strictly greater than 0. If the user enters a non-integer
value or if the input is less than or equal to 0, the program should print "Invalid input", and reprompt the user for the specific value.
The input for the lower bound should be strictly less than the input for the upper bound. If the lower bound is greater than or equal to the upper bound, the program should print "Invalid bounds" and the user should be reprompted for the lower and upper bounds.
After each three-part request, the program will print a table. Each row of the table contains the following data:
x p(x) diff(index)
where x is the current value of x, p(x) is the value of the polynomial at x, and diff is p(x) – p(xprev), where xprev is the previous value of x in the table. The number of rows in the table is determined by the value of intervals as supplied by the user. The values of x for the rows are spaced evenly,
starting with lower and ending with upper.
Anywhere that the polynomial has a root between two successive values of p(x) that are on opposites sides of the x-axis, your program will insert an extra line into the table between the successive values indicating that there is a root and give the two values of x. If the polynomial has a root at both
of the values of x in the the table, a line after the corresponding lower value table line and before the corresponding upper value table line indicates that this is so. Otherwise, if the polynomial has a root at only one of the values of the table, a line after the corresponding table line indicates that this
is so. Each root should only be listed once.
When the table has been printed, your program will ask the user if they want another table with the message: "Do you want to redefine your table? (Y/N)". If the user responds yes, (e.g., with a value of 'y' or 'Y'), the program will request a new set of range data and print the table appropriate for the
new data. The request for the range data will be repeated after each table is printed until the user responds no (e.g., with a value of 'n' or 'N' or any other value that is not 'y' or 'Y') to the request for a new table.
A sample run of this program would be:
Type in the polynomials in increasing powers:
2.2
-.2
0
4
4.0 x^3 – 0.2 x^1 + 2.2 x^0 Type in the range:
Lower Bound (double): -10
Upper Bound (double): 10
Number Of Intervals (int): 10
index p(index) diff(index)
-10.000 -3995.800 2911.600
-8.000 -2044.200 1951.600
-6.000 -860.600 1183.600
-4.000 -253.000 607.600
-2.000 -29.400 223.600
Root found between -2.000 and 0.000
0.000 2.200 31.600
2.000 33.800 31.600
4.000 257.400 223.600
6.000 865.000 607.600
8.000 2048.600 1183.600
10.000 4000.200 1951.600
Do you want to redefine your table? (Y/N)
y
Type in the range:
Lower Bound (double): -2
Upper Bound (double): 0
Number Of Intervals (int): 14
index p(index) diff(index)
-2.000 -29.400 7.330
-1.857 -23.050 6.350
-1.714 -17.609 5.441
-1.571 -13.008 4.601
-1.429 -9.176 3.831
-1.286 -6.044 3.132
-1.143 -3.542 2.502
-1.000 -1.600 1.942
-0.857 -0.148 1.452
Root found between -0.857 and -0.714
-0.714 0.885 1.033
-0.571 1.568 0.683
-0.429 1.971 0.403
-0.286 2.164 0.193
-0.143 2.217 0.053
-0.000 2.200 -0.017
OR ( -0.000 or 0.000 is OK for this line)
0.000 2.200 -0.017
Do you want to redefine your table? (Y/N)
n
Program Design
The Polynomial program will be written across two classes: Polynomial.java and PolynomialUI.java. The program will be started as follows:
% java PolynomialUI
Polynomial.java
The Polynomial class represents a Polynomial object. A Polynomial knows its coefficients, and the coefficients for a polynomial must be stored in an array. A Polynomial object should store no more than ten (10) coefficients.
The class will have private fields (instance variables) sufficient to keep track of the coefficients and the portion of the coefficient array that is filled and it must have the following methods and use the exact headers as given below:
public Polynomial()
The constructor will initialize all of the private fields (instance variables). The coefficient array should be initialized to a length of ten (10). The portion of the array that is filled should be maintained.
public void addCoefficient(double coefficient)
In this method you will implement code that will add a new coefficient to the coefficient array. To do this, you will need to keep track of the portion of the array that is filled and add any new coefficient into the next unfilled slot. Any coefficient that overfills the array should be ignored.
The way polynomials are stored in this program are as illustrated by the following example: if you are given 4 x^3 – 0.2 x + 2.2, then the final poly[] contents will be:
i poly[i]
0 2.2
1 –0.2
2 0
3 4
public double[] getPolynomial()
All this method needs to do is return the polynomial array. Returning an array is similar to returning any other variable type, in this case, instead of the return being double, it is a double[]. Use this method to retrieve the array.
public String toString()
This method will return a String containing a visual representation of the polynomial. The polynomial must be printed in the following format:
an-1 x^n-1 + ... + a1 x^1 + a0 x^0
Starting with the coefficient at the highest power, the coefficient will be printed followed by a space, then the character x, followed by a ^, and the power of the value. The +'s are replaced by –'s when the corresponding coefficients are negative. In other words, if you enter -3 and 2 for coefficients,
your program should print 2 x^1 - 3 x^0 instead of 2 x^1 + –3 x^0. Terms with coefficients of 0 should be omitted. If the coefficient associated with the highest power is negative, then the coefficient should be printed as follows: -4 x^4.
An example of the String that should be returned for the polynomial example above is provided below.
4.0 x^3 – 0.2 x^1 + 2.2 x^0
public double getValue(double x)
This method returns the double value of the polynomial at a location x. So if your polynomial is 4 x^3 – 0.2 x + 2.2 and you give x a value of 1, this method should return 6.0.
public int findRoot(double lower, double upper)
This method will return an integer that identifies where the root was found. It needs to check for two conditions: 1) Whether the root is located at the lower bound, upper bound, or both. 2). Whether there is a root between the values, for the purposes of this assignment assume there is only a root
between indexes if the value of polynomial changes signs. The following integer values should be returned based on identifying the root.
If the root is found at the lower bound, findRoot should return -1.
If the root is found at the upper bound, findRoot should return 1.
If the root is found at both the lower and upper bound, findRoot should return 2.
If the root is found between the lower and upper bound, as signified by a sign change (e.g. the sign of the polynomial's value at the lower bound is negative and the sign of the polynomial's value at the upper bound is positive), findRoot should return 0.
If the root is not found between the lower and upper bound, or there are an even number of sign changes, findRoot should return -2.
PolynomialUI.java
The PolynomialUI class represents the command line user interface for the Polynomial object. This class will prompt the user for input, process the input appropriately, and provide output to the user of the results. The PolynomialUI will require an instance of Polynomial. Information about the lower,
upper, and intervals should also be maintained by the PolynomialUI.
public PolynomialUI()
The PolynomialUI constructor will create and initialize the Polynomial object and all other program state.
public void runPolynomial()
runPolynomial() controls the main flow of the program. The program flow is as follows:
1. Prompt the user for all polynomial coefficients, until the user enters a blank line,
2. Print the polynomial for the user,
3. Prompt the user for lower and upper bound and an interval within the lower to upper bound range,
4. Print the values of the polynomial at each interval within the lower and upper bound range, inclusive of the lower and upper bounds. Any roots should be identified.
5. Prompt the user to redefine their table. If the user says yes, go to step 3, otherwise quit.
See the program output under the Program Requirements for an example of the program flow.
public void setPolynomial()
setPolynomial() prompts the user to "Type in the polynomials in increasing powers:" and processes the polynomials provided by the user using Scanner until the user enters a blank line. If the user enters a non-double value, the program should print "Invalid input, please try again.", and wait for
additional coefficient input.
All valid coefficient values that are read in by the setPolynomial() method should be added to the Polynomial object.
Two examples of the I/O for this method are provided below. The black text is printed by the program and the red and bold text is user input. The first example shows valid polynomial input, the second example shows an invalid input.
Type in the polynomials in increasing powers:
2.2
-.2
0
4
Type in the polynomials in increasing powers:
2.2
-.2
0
a
Invalid input, please try again.
4
public void printPolynomial()
printPolynomial() prints out a visual representation of the polynomial, as shown below.
4.0 x^3 – 0.2 x^1 + 2.2 x^0
public void setRange()
In this method you will use a Scanner to prompt the user for the range of the table to be printed. The lower and upper bounds can both be any double value, provided lower < upper. However the number of intervals must be an integer > 0. This method will update the fields (instance variables) for
the lower bound, upper bound, and intervals. You will want to reset the fields (instance variables) for the lower and upper bounds to 0 when first running the method. The user should be reprompted if they enter invalid input as described in the requirements. The output from this method should look
like:
Type in the range:
Lower Bound (double): -10
Upper Bound (double): 10
Number Of Intervals (int): 10
public void printTable()
Your table will have three columns, the first is the index (the x value), the second column is the value of the polynomial at the value of the index, and the third is the difference between the current value of the polynomial and the value at the previous index. The table in this method will make use of
the start value, end value, and number of intervals to determine the amount to increment x by. Since the double values can grow very large very rapidly, using printf is recommended with the flags for any float being "%.3f". This will round the floating point number to three decimal places. For the
table, you will want to use %9.3f to create the columns.
Between every index, there should be a check for roots.
If one root is found at x, the root statement (below) should be printed AFTER the corresponding x value's line in the table.
Root found at x
If the root is between values x and y, the root statement (below) should be printed AFTER the corresponding x value's line and BEFORE the y value's corresponding line in the table.
Root found between x and y
If the root is at both x and y, the root statement (below) should be printed AFTER the corresponding x value's line and BEFORE the y value's corresponding line in the table.
Root found at x and y
The overall output of this method should look something like:
index p(index) diff(index)
-10.000 -3995.800 2911.600
-8.000 -2044.200 1951.600
-6.000 -860.600 1183.600
-4.000 -253.000 607.600
-2.000 -29.400 223.600
Root found between -2.000 and 0.000
0.000 2.200 31.600
2.000 33.800 31.600
4.000 257.400 223.600
6.000 865.000 607.600
8.000 2048.600 1183.600
10.000 4000.200 1951.600
Submission Guidelines
You will have one week to complete the program. All programs are due by 11:45 PM of the last day in the week.
Before you submit your work, make sure the program:
1. behaves as specified in this document.
2. is thoroughly tested. Be sure to know what results should be displayed for each major scenario.
3. satisfies the gradesheet for this assignment.
Your code should follow the CSC Department's Style Guidelines document. This includes the proper formatting of your source code and the use of Javadoc comments for all classes, methods, and fields (instance variables).
Additionally, magic numbers should not be used. Magic numbers are constant values that are hard coded into program logic. Instead, constants should store the magic numbers. This allows for easy reuse and maintainance of your code.
Submit your code (Polynomial.java and PolynomialUI.java files) via Moodle. Programs submitted after the deadline will not be accepted.
If you are found violating NC State University's Code of Student Conduct, your program will not be accepted, and the incident will be reported to the Office of Student Conduct.

Last modified: Mon May 25 12:36:35 EDT 2020
3":@#'6')%A.)+2-:56)0"2&)>"#0A":5%#B?%C%)%A.)>"#0A":5%#DEB?%C%)2+5AF)6,')#5AG)-'#"H)-0)6,').2').%6'(65:'B
◀)IAA"2A1':'A6+ J2:@)6"BBB 343);;<)3=8)>&"?'16)▶︎
K"2)%&')#"FF'.)5A)%+)K592%A)K%AF)LM"F)"26N
343)3&'.567-0789%:
*%6%)&'6'A65"A)+2::%&0
O'6)6,'):"-5#')%@@
! >&"?'16+ " # K592%A)K%AF )

欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468