Question

1. Write a JAVA method that expands a given binomial (ax by), where integers a, b, n are user inputs. For example, if a 2, b--12, n- 4 are entered the method should print or return (2x 12y)44- 16x*4 - 384xA3y 3456x 2y 2 13824xy 3 + 20736y 4 Use the Pascals triangle method using only one 1-dim array to calculate all binomial coefficients. Your main program should use an appropriate loop for repeated inputs.
Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b, n are user inputs. For example, if a = 2, b = -12, n = 4 are entered the method should print or return
(2x - 12y)^4 = 16x^4 - 384x^3y + 3456x^2y^2 – 13824xy^3 + 20736y^4
Use the Pascal’s triangle method using only one 1-dim array to calculate all binomial coefficients.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If any thing more you want me to do in this code or any issues with the code, please comment and I will be there. :). Please up-vote, it means a lot to me. Happy coding!

Please let me know if any problem occurs.

package abc;

import java.util.*;

public class Binomial {

//Get coefficients.

static ArrayList<Long> getCoefficients(int n,ArrayList<Long> list){

for(int i = 1;i<=n;i++) {

int C = 1;

if(i==n)

for(int j =1;j<=i;j++) {

list.add((long)C);

C = C*(i-j)/j;

}

}

return list;

}

static void BinomialExpansion(int a, int b, int n) {

//(ax+by)^n

ArrayList<Long> list = new ArrayList<Long>();

list = getCoefficients(n+1,list);//the list of coefficients.

System.out.println(list);

String res = "";

//print the series.

for(int i = 0;i<list.size();i++) {

//constant term.

long constant = list.get(i)*(long)(Math.pow(a,n-i))*(long)(Math.pow(b,i));

String term = constant+"x^"+(n-i)+"y^"+i;

if(constant>0)

term = "+"+term;

res +=term;

}

System.out.println(res);

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner scan = new Scanner(System.in);

int t = scan.nextInt();

for(int i =0;i<t;i++) {

int a = scan.nextInt();

int b = scan.nextInt();

int n = scan.nextInt();

BinomialExpansion(a,b,n);

}

scan.close();

}

}

//output

Add a comment
Know the answer?
Add Answer to:
Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b,...
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
  • Write a program in JAVA that asks the user to enter an integer. Store the integers...

    Write a program in JAVA that asks the user to enter an integer. Store the integers in an array. Allow for up to 10 integers. When the user enters -1, the program should end. Print the number of integers entered as well as the number of times each integer was entered.

  • (A and C) Exercise 1.14. If n and k are integers, define the binomial coeffi- cient...

    (A and C) Exercise 1.14. If n and k are integers, define the binomial coeffi- cient (m), read n choose k, by n! if 0 <k <n, = 0 otherwise. k!(n - k)! (a) Prove that ("#") = (m) + (-2) for all integers n and k. (b) By definition, () = 1 if k = 0 and 0 otherwise. The recursion relation in (a) gives a computational procedure, Pascal's triangle, for calculating binomial coefficients for small n. Start with...

  • Write a Java program to prompt for inputting an integer N, then enter N integers (a...

    Write a Java program to prompt for inputting an integer N, then enter N integers (a loop is needed), print the numbers user entered, and the amount of even and odd numbers (zero is even number). (1) Prompt for the user to input an integer and output the integer. (1 pts) Enter an integer: You entered: 5 (2) Prompt for the user to input N integers, output the numbers entered and the amount of even and odd numbers (9 pts)...

  • by netBeans C++ java by netBeans C++ java by netBeans C++ java 1. Answer the following...

    by netBeans C++ java by netBeans C++ java by netBeans C++ java 1. Answer the following question a) (4 marks) Write a Java method, which takes an integer array and two integers x and y as input parameters. The method should exchange the value in position x with the value in the position y in the array. Example: If x = 1, y=2 Input Array Output Array b) (4 marks) Write the main method to do the following: 1. Define...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • In Java Implement a program that reads two integers (n and k). The program must output...

    In Java Implement a program that reads two integers (n and k). The program must output the binomial coefficient as follows: Inn! (k)k!(n. - (n k > 0) (1) Also, print every coefficient that is before binomial coefficient for given k; Example: Insert n: 5 Insert k: 3 Result: 10 Coefficient-array: 1 5 10 10

  • Write a JAVA program to display a “*” triangle and a half diamond of size n,...

    Write a JAVA program to display a “*” triangle and a half diamond of size n, where n is the input of the program. For example, when the user enters 4 as n’s value, the program should print the following shapes of “*”: A triangle of size 4: * *** ***** ******* A half diamond of size 4: * *** ***** ******* ***** *** * Extra Credit (+5) Print the following pattern instead. A triangle of size 4, using *:...

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • 4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array...

    4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...

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