Question

In: Computer Science

using Java Implement a class called Binomial_Coefficient o Your class has 2 int attributes, namely K...

using Java

Implement a class called Binomial_Coefficient

o Your class has 2 int attributes, namely K and n
o Create an overloaded constructor to initialize the variables into any positive integers such that n > k > 0o Create a method that calculates the value of binomial coefficient, C(n,k) , using the following rule:

▪ For an array of size (n+1) X (k+1)
▪ Array [n][k] = Array[n-1][ k-1]+ Array[n-1] [k]▪ Array[n][0]= Array[n][n] = 1
▪ Hence, C(n,k) = array [n][k]

o Create a method that prints the content of your array
➢ Write a main method to test your logic with different combinations of n & k.
➢ Make sure in your main function tests the the values of n & k to meet the condition n > k > 0➢ If the user enters wrong combination prompt the user to reenter valid values of n & k

For your information:

  • Binomial coefficients are coefficients of the binomial formula:
    (a + b)n = C(n,0)anb0 + . . . + C(n,k)an-kbk + . . . + C(n,n)a0bn

  • Binomial Coefficient are calculated as follow:

  • C(n,k) = nCk = (nk) = n!/(k! * (n-k)! )

  • C(n,k) = C(n-1,k) + C(n-1,k-1) for n > k > 0

  • C(n,0)=1, C(n,n)=1 forn0

Solutions

Expert Solution

Screenshot

Program

import java.util.Scanner;

//Create a binomial class
public class Binomial_Coefficient {
   //Instance variables
   private int k;
   private int n;
   //Constructor
   public Binomial_Coefficient(int n,int k){
       Scanner sc=new Scanner(System.in);
       while(!(n>k && k>0)) {
           System.out.println("Enter value of k: ");
           k=sc.nextInt();
           System.out.println("Enter value of n: ");
           n=sc.nextInt();
       }
       this.k=k;
       this.n=n;
   }
   //Coefficient calculation function
   public void calculateCoefficients() {
       int coefficientsArray[][]=new int[n+1][k+1];
       for(int i=0; i<=n; i++) {
           int min = i<k? i:k;
           for(int j = 0; j <= min; j++) {
                 if(j==0 || j == i) {
                   coefficientsArray[i][j] = 1;
                 }
                 else {
                   coefficientsArray[i][j] = coefficientsArray[i-1][j-1] + coefficientsArray[i-1][j];
                 }
           }
        }
       printArray(coefficientsArray,n,k);
   }
   // Create a method that prints the content of your array
   public void printArray(int[][] arr,int n,int k) {
       System.out.println("Coefficients Array: ");
       for(int i=0;i<n;i++) {
           for(int j=0;j<k;j++) {
               System.out.print(arr[i][j]+" ");
           }
           System.out.println();
       }
       System.out.println();
   }
   //Main method
   public static void main(String[] args) {
       //Create test
       Binomial_Coefficient b=new Binomial_Coefficient(7,3);
       b.calculateCoefficients();
   }
}

-------------------------------------------------------------------------------

Output

Coefficients Array:
1 0 0
1 1 0
1 2 1
1 3 3
1 4 6
1 5 10
1 6 15


Related Solutions

Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color....
(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color to “white”; this is called a default constructor. Next, create a second constructor that takes in two parameters. The second constructor should assign those parameters to the attributes. Then, in main, create two Rabbit objects. For the first Rabbit object, call the first constructor. For the second...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
Using JAVA Create a class Client. Your Client class should include the following attributes: Company Name...
Using JAVA Create a class Client. Your Client class should include the following attributes: Company Name (string) Company id (string) Billing address (string) Billing city (string) Billing state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyClient that inherits from the Client class.   HourClient must use the inherited parent class variables and add in hourlyRate and hoursBilled. Your Hourly Client class should contain a constructor that calls the constructor from the Client class to initialize...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the...
JAVA Specify, design, and implement a class called PayCalculator. The class should have at least the following instance variables: employee’s name reportID: this should be unique. The first reportID must have a value of 1000 and for each new reportID you should increment by 10. hourly wage Include a suitable collection of constructors, mutator methods, accessor methods, and toString method. Also, add methods to perform the following tasks: Compute yearly salary - both the gross pay and net pay Increase...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k)...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k) { this.i=i; this.j=j; this.k=k; } public String toString() { return "A("+i+","+j+","+k+")"; } } class Main { public static void main(String[] args) { ArrayList<A> aL=new ArrayList<A>(); Random rand= new Random(1000); //1000 is a seed value for (int p=0; p<10; p++) { int i = rand.nextInt(100); int j = rand.nextInt(200); int k = rand.nextInt(300); aL.add(new A(i, j, k)); } System.out.println("----- Original arraylist------"); for (A a: aL)...
JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
Implement Java source code for a class called Temperature that does the following: a. Prompts the...
Implement Java source code for a class called Temperature that does the following: a. Prompts the user to input a temperature b. Prints “Too Hot!” when the input temperature is above 75 c. Prints “Too Cold!” when the input temperature is below 40 d. Prints “Just Right!” when the input temperature is 40 to 75 e. Be precise, include comments, prologue, etc. if needed.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT