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...
(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...
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...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
using java Create a class Rectangle with attributes length and width both are of type double....
using java Create a class Rectangle with attributes length and width both are of type double. In your class you should provide the following: Provide a constructor that defaults length and width to 1. Provide member functions that calculate the area, perimeter and diagonal of the rectangle. Provide set and get functions for the length and width attributes. The set functions should verify that the length and width are larger than 0.0 and less that 50.0. Provide a member function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT