Java Ex 05
Java Ex 05
1. Java Does not support default arguments by default. Explain how to use method
overloading techniques to create default arguments indirectly. Refer the link :
https://stackoverflow.com/questions/997482/does-java-support-default-parameter-
values
2. Explain the term “stepwise refinement” using the following scenario.
“When printing a check, it is customary to write the check amount both as a number
(“$274.15”) and as a text string (“two hundred seventy-four dollars and 15 cents”).
Write a program to turn a number into a text string”
a. Rewrite it so the function returns the final value to the main program and then
displays it.
b. Can you make it return a double ex: return (double) sum1;
4. Write a function which will take in a number and give back two times the number given.
Write the main body of the program to call the function, and when a value is returned the
main body should display it.
5. Write a program that will take a number and return the result of multiplying all the
numbers together from 1 to the number given. (factorial). Write the main body of the
program to call the function, and when a value is returned the main body should display
it.
6. What does this display?
//swaps parameter names around in function call
public static void main(String[] args) {
int a = 2;
int b = 5;
int c = 4;
mixup(a, b, 3);
}
Software Development II
private static void mixup(int b, int c, int a ) {
a = b + c - a;
System.out.println(a + " " + c);
}
Software Development II
For example
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120
10. A nonnegative integer is called a palindrome if it reads forward and backward in the same
way. For example, the numbers 5, 121, 3443, and 123454321 are palindromes. Write a
method that takes as input a nonnegative integer and returns true if the number is a
palindrome; otherwise, it returns false. Also write a program to test your method.
Software Development II