Full Transcript

Lecture 9: Methods COMP102: Computer Programming August 2024 Agenda - Why do we use methods? - Methods in Java - Exercise 1 Consider the Following Problem Prompt a user to enter two integers. Then determine whether they have the same parity. The parity of an integer is its attribute of being even...

Lecture 9: Methods COMP102: Computer Programming August 2024 Agenda - Why do we use methods? - Methods in Java - Exercise 1 Consider the Following Problem Prompt a user to enter two integers. Then determine whether they have the same parity. The parity of an integer is its attribute of being even or odd. In Pseudocode get first integer get second integer determine if first number is even/odd (display message) determine if second number is even/odd (display message) determine if they have the same parity (display message) Solution in Python 1. num_one = int(input('Enter first integer: ')) 2. num_two = int(input('Enter second number: ‘)) 3 4. if(num_one % 2 == 0): 5. print(num_one, 'is even.’) 6. num_one_even = True 7. else: 8. print(num_one, 'is odd.’) 9. num_one_even = False 10. 11. if(num_two % 2 == 0): 12. print(num_two, 'is even.’) 13. num_two_even = True 14. else: 15. print(num_two, 'is odd.’) 16. num_two_even = False 17. 18. if(num_one_even == num_two_even): 19. print('Numbers have same parity.’) 20. else: 21. print('Numbers do not have same parity.') In Python Using Functions 1. num_one = int(input('Enter first integer: ')) 2. num_two = int(input('Enter second number: ‘)) 4. 5. if(is_even(num_one) == is_even(num_two)): 6. print('Number have the same parity.’) 7. else: 8. print('Number do not have same parity.’) 9. 10. def is_even(num): 11. if(num % 2 == 0): 12. print(num, 'is even.’) 13. return True 14. else: 15. print(num, 'is odd.’) 16. return False Benefits of Functions - Less code! We always want to write the least amount of code to solve a problem - Minimise the repetition of code – leverage the same function - Centralised point of change - Code looks much neater Template of Java Method public static returnType name(dataType parameter) { // body of method } Components of Method - The public and static keywords - The return type, use void keyword when method returns nothing + If you have a return type, you need to return some value using return keyword + The returned type has to match the declared type - The name of the method - The method parameter(s) isEven() Method Declaration public static Boolean isEven(int num) { // body of method } isEven() Method in Full public static Boolean isEven(int num) { if(num % 2 == 0){ System.out.println(num + " is even."); return true; } else { System.out.println(num + " is odd."); return false; } } Calling methods It is only legal to call a method if is has been defined. The order of parameters in the method definition must match the order of the arguments for those parameters when the method is called. Methods can call other methods within them if the called method is in a valid scope/context to be called in the other method calling it. Question 1: Palindrome A palindrome is a word that has the same spelling when the letters are reverse. For example, the following are all palindromes: “radar”, “civic”, “defied”, “level”, “rotator” Write a method that receives a word and determines whether it is a palindrome or not. It should return the Boolean value “true” if it is, and “false” if it isn’t. To test your method by prompting a user for a word and telling them whether it is a palindrome or not. Ends.

Use Quizgecko on...
Browser
Browser