Question

In: Computer Science

Create a new folder called CSCI130_A3_yourname. Create all of the files indicated below within this folder....

Create a new folder called CSCI130_A3_yourname. Create all of the files indicated below within this folder.

For this assignment you will be creating a total of 2 classes that meet the definitions below. This assignment will allow the user to enter in the coefficients for a polynomial function of degree 2. The user will then be asked to enter a number. The program will use that number as an argument to the function, and will print the corresponding function value.

Keep in mind that a second degree polynomial function in general form looks like:

    f(x) = ax2 + bx + c (here 'a' is the coefficient of the quadratic term, 'b' is the coefficient of the linear term, and 'c' is the constant)

Refer to the image at the bottom of this page to get a better understanding of the logic flow.

CLASSES:

  1. Polynomial - this class should be in a file named Polynomial.java
  2. Assignment3 - this class should be in a file named Assignment3.java

CLASS DEFINITIONS:

class Polynomial

Data (instance variables) - all have private access

  • double quadraticCoefficient (this is the coefficient of the quadratic term of the polynomial)
  • double linearCoefficient (this is the coefficient of the linear term of the polynomial)
  • double constant (this is the constant of the polynomial)

Methods (behaviors) - all have public access

constructor:

  • Arguments:
    • None
  • Return
    • None
  • Behavior
    • Calls Polynomial's constructPolynomial instance method

constructPolynomial:

  • Arguments:
    • None
  • Return
    • None
  • Behavior
    • Allows user to enter the necessary coefficients and the constant term for the polynomial, and sets the attributes accordingly

functionValue:

  • Arguments:
    • double arg
  • Return
    • double
  • Behavior
    • Calculates and returns the function value using the argument arg (for example, if the user entered the polynomial 3x2 + 2x + 7, then functionValue(3) would return 40)

displaySelf:

  • Arguments:
    • None
  • Return
    • None
  • Behavior
    • Neatly displays the polynomial entered by the user

class Assignment3

Methods

public static void main(String[] args):

  • Arguments:
    • String[] args
  • Return:
    • None
  • Behavior (see image below for clarification):
    • The polynomial is constructed
    • The user is asked for the argument to the function
    • The polynomial is displayed
    • The function value (based on the argument and Polynomial entered) is displayed

After writing the code, do the following:

  • Compile and execute the code. Fix any errors that occur at either stage. Continue this process until the program runs correctly.
  • Document the code by putting your name, the date, assignment number, and instructor in comments at the top of the Driver.java file.
  • Submit your assignment be either:
    • uploading both files within the CSCI130_A3_yourname folder to Canvas
      OR
    • zipping the entire CSCI130_A3_yourname folder and uploading the zipped file to Canvas

Solutions

Expert Solution

Code in Java:

class Polynomial
{
   private double quadraticCoefficient;
   private double linearCoefficient;
   private double constant;
  
   Polynomial()
   {
       constructPolynomial(); //constructor calls this instance method
   }
  
   public void constructPolynomial()
   {
       Scanner sc = new Scanner(System.in); //to input values from user
       System.out.println("Enter quadraticCoefficient: ");
       quadraticCoefficient = sc.nextDouble();
      
       System.out.println("Enter linearCoefficient: ");
       linearCoefficient = sc.nextDouble();
      
       System.out.println("Enter constant: ");
       constant = sc.nextDouble();
   }
  
   public double functionValue(double arg)
   {
       double v1 = quadraticCoefficient*quadraticCoefficient*arg;
      
       return((quadraticCoefficient*quadraticCoefficient*arg) + (linearCoefficient*arg) + (constant));
   }
  
   public void displaySelf()
   {
       System.out.println(quadraticCoefficient + "x2" + " + " + linearCoefficient + "x + " + constant);
   }
}


public class Assignment3{
   public static void main(String[] args)
   {
       Polynomial poly = new Polynomial();
       poly.displaySelf();
       System.out.println(poly.functionValue(3));

//since not mentioned in question, function value - 3 is fed hereitself. However, if required to be taken from user, the same //Scanner function used earlier for taking the coefficients can be used to take this value here itself.
   }
}


Related Solutions

In the root of the project create a folder called res. In that folder create a...
In the root of the project create a folder called res. In that folder create a file called DemoFile.txt Create a class with a static main that tests the ability to resolve and print a Path: Create an instance of a FileSystem class. Create an instance of the Path interface for the DemoFile.txt file. Print the constructed Path with System.out.println() method. Create a class that does the following: Using a pre-Java 7 solution, create a class that tests streams in...
JAVA There is a folder named Recursive folder at the same location as these below files,...
JAVA There is a folder named Recursive folder at the same location as these below files, which contains files and folders. I have read the data of the Recursive folder and stored in a variable. Now they are required to be encrypted and decrypted. You may copy past the files to three different files and see the output. I am confused on how to write code for encrypt() and decrypt() The names of files and folders are stored in one...
Create a class Student (in the separate c# file but in the project’s source files folder)...
Create a class Student (in the separate c# file but in the project’s source files folder) with those attributes (i.e. instance variables): Student Attribute Data type (student) id String firstName String lastName String courses Array of Course objects Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table) In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method...
I have multiple .txt files in a folder, and i want to combine all their contents...
I have multiple .txt files in a folder, and i want to combine all their contents into a single .txt file How do I go about such an operation in Python?
I. At the beginning, create a folder named "test" and add the folder into the version...
I. At the beginning, create a folder named "test" and add the folder into the version control tool tracking system. Then create one file named "001" under this folder. Commit any change if necessary. II. Next, add another file named "002" in the same folder and commit the change ("adding") in the version control tool. III. Followed by adding that file, create a branch (namely, "branch-A") in the version control tool and make some changes to the contents in file...
Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors.
Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors. In each case, determine the problem and fix the program. After you correct the errors, save each file using the same filename preceded with Fix. For example, DebugFifteen1.java will become FixDebugFifteen1.java. a. DebugFifteen1.java b. DebugFifteen2.java c. DebugFifteen3.java d. DebugFifteen4.java    
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and save all your files for this lab into this new folder. 2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs 3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide. Problems [30 marks] Problem 1: The Account class...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and save all your files for this lab into this new folder. 2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs 3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide. Problem : The Fraction class (Filename: TestFraction.java) Design...
You must create two Java files. One is called LastNameFirstNameWeek6Prog.java, and the other is called  Museum.java. Ensure...
You must create two Java files. One is called LastNameFirstNameWeek6Prog.java, and the other is called  Museum.java. Ensure you include ALL files required to make your program compile and run. I would like to see your .java files only. 5% Museum.java Class File 1.    Create a new class called Museum that describes a Museum and includes the functionality below 2.    The new class has the following five attributes: a. officialName – the museum’s given name (i.e.:“ New Orleans Museum of Arts”,etc.), type...
Week 4 Project - Submit Files Hide Submission Folder Information Submission Folder Week 4 Project Instructions...
Week 4 Project - Submit Files Hide Submission Folder Information Submission Folder Week 4 Project Instructions Changes in Monetary Policy Assume that the Bank of Ecoville has the following balance sheet and the Fed has a 10% reserve requirement in place: Balance Sheet for Ecoville International Bank ASSETS LIABILITIES Cash $33,000 Demand Deposits $99,000 Loans 66,000 Now assume that the Fed lowers the reserve requirement to 8%. What is the maximum amount of new loans that this bank can make?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT