CMPS 260 Programming Assignment #2

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (3 votes)

Assignment description
First, create an Intellij Java project and name it “pa2_your-id”.
In the Main class (in file “Main.java”), write the Java code for the following methods:
• Write a public static method that receives nothing and returns nothing. When called, this method
outputs your name on one line and your ULID on another line. There is no input from the user in the
method.
• Write a public static method that receives three double values in parameters a, b and c. The method
returns nothing. When called, this method solves for the two roots of the quadratic equation ax2 +
bx + c = 0 by use of the following formulas
r1 = (­ b + sqrt (b^2 ­ 4ac)) /2a
r2 = (­ b ­ sqrt(b^2 ­ 4ac)) / 2a
b^2 ­ 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has
two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots.
If the discriminant is positive, the method outputs two roots. If the discriminant is 0, the method
outputs one root. Otherwise, the method outputs the string “The equation has no real roots”. There
is no input from the user in the method.
Tip: Math.sqrt(x) or Math.pow(x, 0.5) to compute sqrt(x).
• Write a public static int method that receives two int parameters and returns the sum of the integers
from the smallest parameter value to the largest parameter value. The method uses a for loop to
compute the sum.
(continued on next page)
CMPS 260 Spring 2019 Programming Assignment #2 (2019.02.04) 2
• Write a public static double method to compute the following simple number series:
m(i) = 1/2 + 2/3 +……+ i/(i+1)
For example, (depending on the number of decimal place formating)
m(1) results in .5
m(2) results in 1.1666666666666665
m(3) results in 1.9166666666666665
m(4) results in 2.716666666666667
m(5) results in 3.5500000000000003

m(19) results in 16.40226034285632
m(20) results in 17.354641295237272

The method receives a single double parameter (the value of i) and uses a while loop to compute
m(i). The result is then returned. If the value if i is less than 1, 0 is returned. There is no input
from or output to the user in the method. Note that the value passed to the method should be an
integer, but will need to be used as a double in the method.
In the method main of the Main class, write the Java code to
• call the method that outputs your name and ULID.
• use a do-while loop to repeatedly present a menu to the user until the user elects to exit the program.
The menu will prompt the user to select between calling the method to compute quadratic roots, the
method to sum the integers, the method to calculate the simple number series and exiting the
program. When the user makes a selection of a method to execute,
◦ the user is then prompted to enter values for the actual parameters to be passed to the method,
◦ the method is called and passed the values the user entered. (Note that the value passed to the
number series method should be an integer, even though it will be used as a double.)
◦ if the methods return results, the result returned from the method called is displayed,
◦ the menu is again displayed and the user is prompted to enter another selection.
Requirements
• A reference variable and instance object of class java.util.Scanner must be used to read the input
from the user.
• Prompts for user input must be clear and relevant. Output to the user must include appropriate
labels.
CMPS 260 Spring 2019 Programming Assignment #2 (2019.02.04) 3
Example Run (with user input in red):
Name: A. Programmer
ULID: c00000000
0­Exit, 1­Quadratic, 2­Sum, 3­Series: 1
Enter a, b and c separated by spaces: 1 2 3
The equation has no real roots
0­Exit, 1­Quadratic, 2­Sum, 3­Series: 1
Enter a, b and c separated by spaces: 1 2 1
There is one real root: ­1.0
0­Exit, 1­Quadratic, 2­Sum, 3­Series: 1
Enter a, b and c separated by spaces: 1 4 2
There are two real roots:
­0.5857864376269049
­3.414213562373095
0­Exit, 1­Quadratic, 2­Sum, 3­Series: 2
Enter two integers separated by spaces: 4 6
Sum: 15
0­Exit, 1­Quadratic, 2­Sum, 3­Series: 2
Enter two integers separated by spaces: 6 4
Sum: 15
0­Exit, 1­Quadratic, 2­Sum, 3­Series: 3
Enter a integer value for i: 0
Sum: 0.0
0­Exit, 1­Quadratic, 2­Sum, 3­Series: 3
Enter a integer value for i: 1
Sum: 0.5
0­Exit, 1­Quadratic, 2­Sum, 3­Series: 3
Enter a integer value for i: 2
Sum: 1.1666666666666665
0­Exit, 1­Quadratic, 2­Sum, 3­Series: 3
Enter a integer value for i: 3
Sum: 1.9166666666666665
0­Exit, 1­Quadratic, 2­Sum, 3­Series: 3
Enter a integer value for i: 4
Sum: 2.716666666666667
0­Exit, 1­Quadratic, 2­Sum, 3­Series: 3
Enter a integer value for i: 19
Sum: 16.40226034285632
0­Exit, 1­Quadratic, 2­Sum, 3­Series: 0
Bye!
CMPS 260 Spring 2019 Programming Assignment #2 (2019.02.04) 4
Additional Requirements
(a) Identifiers must be descriptive, i. e. must self document. The only exception granted is in the case of a
“for variable”, that is a variable created as a simple counter as in the control variable in a “for” loop
statement.
(b) Indention of all code blocks (compound statements, anything in braces), including single statements
following selection or while statements, is required.
(c)The main “.java” file [the one with the method public static void main(String[] args)] of your Intellij
projects must contain this minimal documentation as the first lines of the file:
// Your Name
// Your ID
// CMPS 260
// Programming Assignment : #
// Due Date :
// Program Description: (a brief description of actions of your code)
// Certificate of Authenticity:
(Choose one of the two following forms:)
// I certify that the code in the method function main of this project is entirely my own
// work.
{or}
// I certify that the code in method function main of this project is entirely my own work., but I
// received some assistance from {name}. Follow this with a description of the type of assistance.
// (For example, if you consulted a book, and your solution incorporates ideas found in the book,
// give appropriate credit; that is, include a bibliographical reference.) Note: You do not have to
// list the text, the author of the course text or the instructors examples.
Submitting
In Intellij, select File, Export to Zip File and click OK to save the Zip archive file. Finally, upload the zip
archive file to Moodle.
Helpful Hint: Keep a backup copy of your project folder on a Google Drive, a Drop Box Account, a USB
memory device, etc., or even on Moodle! Finally, once you turn in your final version, create a copy
before the due date and do not change this copy in any way. This final copy can be consulted if there is
an upload disaster, but only if the “.java” files have not been changed in any way after the due date.