1 / 4

Exercise Collection

Exercise Collection. 2014/3/17. exercise1.

joelle
Télécharger la présentation

Exercise Collection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exercise Collection 2014/3/17

  2. exercise1 • Write a recursive method definition for a static method that has one parameter n of type int and that returns the nth Fibonacci number. F0 is 1, F1 is 2, F2 is 3, F3 is 5. And in general Fi+2 = Fi + Fi+1 for I = 0,1,2,…Place the method in a class that has a main that tests the method.

  3. ExerCise2 • Define a class called Fraction for performing arithmetic with fractions.Write a main method to test all the functionalities of the Fraction class.Use integer variables to represent the private data of the class-- thenumerator and the denominator. Provide a no-argument constructorthat initializes numerator and denominator to 0 and 1, respectively.Also, provide a two-argument constructor that initializes numeratorand denominator to the initial values given at the declaration of theobject. The constructor should store the fraction in reduced form. For example, the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator.Provide public methods that perform each of the following tasks:(a) Adding two Fraction numbers. The result should be stored in    reduced form.(b) Multiplying two Fraction numbers. The result should be stored in    reduced form.(c) Printing Fraction numbers in the form a/b, where a is the    numerator and b is the denominator.(d) Printing Fraction numbers in floating-point format.

  4. exercise3 • A polynomial consists of a set of terms. Each term is composed of a integer coefficient, a variable, and an positive integer exponent. The simplest way to represent a polynomial is to store the coefficients of the polynomial in an array. For example, -3X4 + 5X2 -2X1 +3 can be represented as an integer array poly with size 5. In the array, -3, 0, 5, -2 and 3 are stored at poly[4], poly[3],poly[2],poly[1],poly[0]. • In the test package, you may find two Java classes Polynomial and TestPolynomial. In Polynomial class, you need to design a constructor which can accept a variable length of integer parameters representing the coefficients of a polynomial. You also need to design an instance method add generating the sum of two polynomials. If you correctly design the constructor and the add method, the output of executing TestPolynomial is as follows. • -10X^3+3X^2+1X^1+2 • 2X^4+4X^3+6X^2+5X^1-4 • 2X^4-6X^3+9X^2+6X^1-2

More Related