Docsity

Lab Report #5: More Practice with Conditionals and Loops, Study Guides, Projects, Research of Object Oriented Programming

Partial preview of the text

Download Lab Report #5: More Practice with Conditionals and Loops and more Study Guides, Projects, Research Object Oriented Programming in PDF only on Docsity!

LAB REPORT:

  • SUBJECT: More practice writing methods

SUBMITTED BY: SAMIA

REG. NO: 17JZBCS

DEPARTMENT: CS&IT

  • TITLE: More practice with conditionals and loops

Exercise 1:

- In BlueJ, create a new project called Lab - Create a class in that project called MoreMethods - Write a method in that class called driver that takes no parameters and returns no value - In driver, print the following statement This method calls the others. Stay tuned. - Compile the code and run the method. SOLUTION: Figure 1

Exercise 2:

- Write a method called max that accepts two double parameters and returns the larger of the two.

Figure 3

Exercise 3:

- Write a method called sum that accepts one int parameter and returns the sum of the numbers from 1 to the parameter. - In the driver method, print the results of two calls to the sum method: The sum of 1 to 10 is xxx The sum of 1 to 100 is xxx SOLUTION: Figure 4

OUTPUT:

Figure 5

Exercise 4:

- Write a method called doubleIt that accepts one String parameter and returns a string that is the parameter concatenated with itself with "XXX" in between. - So calling doubleIt with "Java" will return "JavaXXXJava" - Calling doubleIt with "VT" will return "VTXXXVT" - In the driver method, print the results those two calls.

- Calling halfIt with "Hokies" will return "Hke" - To do this, the method should build the result string one character at a time (use a for loop). - In the driver method, print the results those two calls. SOLUTION: Figure 8 OUTPUT: Figure 9

Exercise 6:

- Write a method called doIt that accepts one String parameter and one int parameter, and returns a String - If the int parameter is greater than the number of characters in the String parameter, the method should return the result of calling doubleIt on the string. - Otherwise, it should return the result of calling halfIt on the string. - In the driver method, print the results of the following two calls: doIt("Voracious", 7) doIt("Efficacious", 20) SOLUTION: Figure 10 OUTPUT:

SOLUTION:

  • Figure 12 OUTPUT:

Figure 13 -