Question

/*    Write a function that takes as an argument the value x,    and returns...

/*

   Write a function that takes as an argument the value x,

   and returns the result of the expression: 20x^4 - 12x^3 - 3x^2 + 15x + 3

   Note: No importing; use the power function given to you, and use it

   as a reference for writing functions (note also that main itself

       if a function!).

*/

public class Exercise9_1

{

public static void main(String[] args)

{

int num = 5, pow = 2;

System.out.println(num + "^" + pow + " == " + power(num,pow));

}

  

// power function: Calculate x^n, return result

public static double power(double x, int n)

{

double product = 1;

      

// If to the power of 0, just simply return 1

if(n == 0)

return product;

      

// Otherwise calculate the product and return the result

for(int i = 0; i < n; i++)

product *= x;

      

return product;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Exercise9_1 {
  
   public static void main(String[] args)
   {
   int num , pow = 2;
   System.out.println("Enter the value of x: ");
   Scanner sc = new Scanner(System.in);
   num = sc.nextInt();
   //System.out.println(num + "^" + pow + " == " + power(num,pow));
   System.out.println("\nExpresion 20x^4 - 12x^3 - 3x^2 + 15x + 3, For x= " + num + " is " + cal_expression(num,pow));
     
   }
  
   public static double cal_expression(double x, int n)
   {
       double value=(20*power(x,4))-(12*power(x,3))-(3*power(x,2))+(15*power(x,1))+3;
       return value;
   }
   // power function: Calculate x^n, return result
   public static double power(double x, int n)
   {
   double product = 1;
  
   // If to the power of 0, just simply return 1
   if(n == 0)
   return product;
  
   // Otherwise calculate the product and return the result
   for(int i = 0; i < n; i++)
   product *= x;
   return product;
   }

}

Code Image:

3 import java.util.Scanner; 4 5 public class numbers 6 public static void main (String args) 8 9 10 int num , pow 2; System.o

Output Image:

<terminated» numbers [Java Application] /usr/lib/jvm/java-8-openjdk-amd64/bin/java (19-Dec-2016, 2:48:17 PM) Enter the value

Add a comment
Know the answer?
Add Answer to:
/*    Write a function that takes as an argument the value x,    and returns...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I just need to add comment for the code blow. Pleas add comment for each method...

    I just need to add comment for the code blow. Pleas add comment for each method and class if comments left out. package exercise01; /*************************************************************************** * <p> This program demonstrates the technique of recursion and includes * recursive methods that are defined for a variety of mathematical * functions. *    * <br>A recursive method is one that directly or indirectly calls itself * and must include: * <br>(1) end case: stopping condition * which terminates/ends recursion * <br>(2) reduction:...

  • Write a Java method that should take an ArrayList as a parameter, print its element in...

    Write a Java method that should take an ArrayList as a parameter, print its element in the reverse order. The main function that calls the method is the following: import java.util.*; public class ReverseGen {   public static void main(String[] args) {     Scanner input = new Scanner(System.in);       System.out.println("How many numbers do you want to input?");       int num = input.nextInt();       ArrayList<Double> d = new ArrayList<Double>(num);       for(int i = 0 ; i < num; i++){           System.out.print("Enter...

  • /** this is my code, and I want to invoke the method, doubleArraySize() in main method....

    /** this is my code, and I want to invoke the method, doubleArraySize() in main method. and I need to display some result in cmd or terminal. also, I have classes such as Average and RandomN needed for piping(pipe). please help me and thank you. **/ import java.util.Scanner; public class StdDev { static double[] arr; static int N; public static void main(String[] args) { if(args.length==0){ arr= new double[N]; Scanner input = new Scanner(System.in); int i=0; while(input.hasNextDouble()){ arr[i] = i++; }...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • java 1. Write a method header on line three with the following specs: Returns: a boolean...

    java 1. Write a method header on line three with the following specs: Returns: a boolean Name: beTrue Parameters: none public class Main {       {        return true;    } } 2. Write a method header on line two with the following specs: Returns: a String Name: makeCapital Parameters: a String named "name" You should not be writing code on any line other than #2 public class Main {       {    String ans = name.toUpperCase();...

  • Write a program that stores a phrase as an array of words, and then prints it...

    Write a program that stores a phrase as an array of words, and then prints it backwards. The main method calls method getInput , which asks the user how many words there are, stores them in an array, and returns this array. printBackwards then takes this array of words, and prints it in reverse. Code Example: import java.util.Scanner; public class L17Num1{    public static void main(String[] args) {    String[] sArray=getInput(); printBackwards(sArray); } public static String[] getInput() { System.out.println("How many...

  • JAVA - Write a recursive function that takes a linked list as input and returns all...

    JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • Java 7 Write a constructor for the MightyByte class so it can accept a String argument...

    Java 7 Write a constructor for the MightyByte class so it can accept a String argument and set the bits field. Complete the for loop in the getDecimalValue method. class Main { public static void main(String[] args) { MightyByte myByte = new MightyByte("00000101"); System.out.println(myByte.getDecimalValue());    } } class MightyByte { private String bits;    // write the constructor here    public int getDecimalValue() { int decimalValue = 0; int intValue; int power = 7; for (int i = 0; i...

  • Java BNF How to write pseudocode for this snippet? Java code for finding a prime number...

    Java BNF How to write pseudocode for this snippet? Java code for finding a prime number public class Prime { public static void main(String[] args) { int num = 29; boolean flag = false; for(int i = 2; i <= num/2; ++i) { // condition for nonprime number if(num % i == 0) { flag = true; break; } } if (!flag) System.out.println(num + " is a prime number."); else System.out.println(num + " is not a prime number."); } }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT