0% found this document useful (0 votes)
33 views3 pages

Java Ex 05

1. Java does not support default arguments, but they can be simulated using method overloading. 2. "Stepwise refinement" is the process of breaking a problem down into smaller steps. For example, turning a number into text for a check amount by first writing the number, then spelling out the dollars, then cents parts. 3. The program multiplies a value by 2 but does not return or display the result, as the parameter is passed by value rather than by reference. It should return the final value and display it in main.

Uploaded by

Nithin Kaneshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

Java Ex 05

1. Java does not support default arguments, but they can be simulated using method overloading. 2. "Stepwise refinement" is the process of breaking a problem down into smaller steps. For example, turning a number into text for a check amount by first writing the number, then spelling out the dollars, then cents parts. 3. The program multiplies a value by 2 but does not return or display the result, as the parameter is passed by value rather than by reference. It should return the final value and display it in main.

Uploaded by

Nithin Kaneshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tutorial 4

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”

3. What does this display?


public static void main(String[] args) {
int aValue = 3;
multiply(aValue, 2);
}
private static void multiply(int sum1, int sum2){
sum1=sum1*sum2;
System.out.println(sum2+" "+sum1);
}

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);
}

7. What does this display


public class Main { // demonstrate scope and functions
static int aValue = 5;
public static void main(String[] args) {
int aValue = 6;
int bValue = 4;
process(bValue);
System.out.println(aValue);
}
private static void process(int aValue) { //AA
aValue = aValue + 4;
System.out.println(aValue);
}
}

a. How different is the display if line AA above was:


private static void process(int cValue) { //AA
b. Can you turn the procedure into a function that returns an int and then displays it.
c. Can you turn the procedure into a function to return a double?

8. Does this multiply 3*2 or add 3 to 2? Why?


public static void main(String[] args) {
double Num1 = 3.0;
int Num2 = 2;
double total;
total = processA( Num1, Num2);
System.out.println(total);
}
private static double processA(double Sum, int Count) {
double newAnswer = Sum + (double) Count;
return newAnswer;
} this program continues on next page >
private static double processA(int Sum, double Count) {
double newAnswer = (double) Sum * Count;
return newAnswer;
}
a. How would you make them both return back an ‘int’?

9. Write the factorial function in question 5 using recursion.

Note: Factorial of n is the product of all positive descending integers. Factorial of n is


denoted by n!.

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

You might also like