Question
1. Write method headers for methods with the following descriptions. A header means the method name, return type, and parameter types and names. That is,
1. Write method headers for methods with the following descriptions. A header means the method name, return type, and parameter types and names. That is, you need not write code for these.
a. Computing the larger of two integers
b. Computing the smallest of three floating-point numbers
c. Checking whether an integer is a prime number, returning true if it is and false otherwise
d. Checking whether a string is contained inside another string
e. Computing the balance of an account with a given initial balance, an annual interest rate, and a number of years of earning interest
f. Printing the balance of an account with a given initial balance and an annual interest rate over a given number of years
g. Printing the calendar for a given month and year
h. Computing the weekday for a given day, month, and year (as a string such as "Monday")
I. Generating a random integer between 1 and n
2. What is the difference between an argument and a return value? How many arguments can a method call have? How many return values?
3. Consider the following method that is intended to swap the values of two integers:
public static void falseSwap(int a, int b) { int temp = a; a = b; b = temp; }
public static void main(String[] args) { int x = 3; int y = 4; falseSwap(x, y); System.out.println(x + " " + y); }
Why doesnt the falseSwap method swap the contents of x and y?
4) Write a method (For this you will need to submit a code listing and test output file).
public int countVowels(String str).
that returns a count of all vowels in the string str. Vowels are the letters a, e, i, o, and u, and their uppercase variants. Use a helper method isVowel(char ch) that checks whether a character is a vowel. Your program should be interactive (use Scanner).
For optional extra credit create a second version where your countVowels method is declared as static: public static int countVowels(String str).
5) Write a recursive method (For this you will need to submit a code listing and test output file).
public String reverse(String str)
that computes the reverse of a string. For example, reverse("flow") should return "wolf". Hint: Reverse the substring starting at the second character, then add the first character at the end. For example, to reverse "flow", first reverse "low" to "wol", then add the "f" at the end.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started