Section9Problems 1

# Section9Problems 1 - CSE143 Section For each of these loop...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSE143 Section For each of these loop to solve the problems, problem; #9 Problems loop, for loop or dolwhiLe you may NOT use a while you MUST use recursion. 1. Write a method factorial that uses recursion to compute the * 2 * 3 ... *n n! :1 By definition, 0 ! is if passed a negative takes an integer n as a parameter and that (a1so known as n!) . val-ue of n factorial The method 1. value. t.hat should throw an IllegalArgumentExceptio 2. Write a method integers. For multiplyEvens example: returns returns returns returns an returns the product of the first n even (1) multiplyEvens muLtiplyEvens(2) (3) multiplyEvens multiplyEvens(4) Your than 3. 2 B (2 " 4l 48 (2 * 4 " 6) 384 (2 * 4 * 6 't 8) IllegalArgumentException is passed a value less method should throw or equal to 0. Write a method starStrinqr returns a stringr of stars For exampl-e: (0 ) starString (1) starString (2) starString (3) starString StarString(4) shoul-d shouLd should shoul-d ShOuId that takes (asterisks) an integer 2nn long n as (L.e., a parameter and that 2 to the nth power) return return return return return , *' (because 2^0 1) ' r* r r' r ( b e c a u s e 2 ^ ! : : 2') rr**',r*rr (because 2^2 4) rr****)k***" (because 2^3 :: 8) rr*t**rrr****rr******tt (beCaUSe 2^4 16) Your method should take a single parameter integer that The method should throw an fllegalArgumentException of 2. value l-ess than 0. 4. Wrj-te a method sumTo that takes sum of the first n reciprocafs. sumTo(n) returns (1 + I/2 an integer In other + I/3 + L/4 parameter words: + specifies the power if passed a returns n and that the + 7/n\ return if . For example, sumTo(2) shouLd return the val-ue 1.5. The method shou]d passed the value 0 and shoul-d throw an IJ-legalArgumentException 0.0 if passed a val-ue less than 0. 5. Write writes order a method writeNums that takes an integer n as a parameter and that the first n integers with 1 to System.out starting in sequential separated by commas. For example, the following calls: writeNums (5) ; System. out .println writeNums (12) ; System.out.printlnO; () ; t.o complete to complete // // the the l-ine line of of output output shoul-d produce L, 1, 2, 2, 3, 3, the 4, 4, 5 5, output: 7, 6, 8, 9, 10, Lr, of 12 the examples above. Your method if passed a value less than 1. You must exactly reproduce the format should throw an IllegalArgumentException ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online