CS110-Lab9_Student-1446
CS110-Lab9_Student-1446
Lab 9: Methods
Lab Objectives:
In this lab, the student will practice:
✔ Using predefined methods (Math Class)
✔ Declare and call User-Defined methods
Which of the following statements are valid? If they are invalid, explain why.
a. Total = sum (double1, 15.0, double3);
2
Lab Exercise 2: Debugging
Executing the following code segment will result in syntax errors. Identify the error in each
segment and correct it.
}
}
public class EX_2B {
3
Lab Exercise 3: Expected Output
Executing the following code and show the output in the following:
public class EX_3A {
public static void main(String[] args) {
int num = 12;
displayNum(num);
}
}
}
public class EX_3c {
}
}
public class
public class MathExample
EX_3d { {
public static void main(String[] args) {
int number
public = -8;main(String[] args) {
static void
double
double result == Math.sqrt(25)+Math.min
result1 Math.abs(number); (10, 5)-Math.abs(-3);
double result2 = Math.pow(2,
System.out.println("The result3);is: "+result);
double result3 = Math.sqrt(25);
}
System.out.printf("Result 1: %.1f\nResult 2: %.1f\nResult 3:
%.1f\n", result1, result2, result3); } }
4
Lab Exercise 4: Code Writing (1)
Problem Description: Write a program that ask the user to enter two integer number
upper_Limit and Lower_Limit of a random number.
Steps:
1- ask the user to enter a range of two limit for numbers
2- ask the user to guess the number between lower and upper numbers
3- Print "Congratulations! You guessed the number correctly." If the guess equals the random number,
Overwise print "Sorry! The correct number was: " and display randomNumber.
Hint: int randomNumber = (int) (Math.random() * (upperLimit - lowerLimit + 1) +
lowerLimit);
Output sample:
The method should return the final price after calculating the total
Output sample:
5
Week 9: Assignment Problems:
Problem 1 Description:
Write a program that ask the user to enter a number and check whether the number is even or
odd. The program should have a method named isEven with type boolean, which takes an
integer as an argument and returns true if the number is even. Finally, print to the user the
number type as in samples.
Output samples:
Problem 2 Description:
Write a Java program to implement a simple banking system with two methods.
• The deposit method should take a double parameter representing the amount to deposit and
add it to the account balance. If the amount is negative or zero, it should display an error
message.
• The withdraw method should take a double parameter representing the amount to withdraw
and subtract it from the account balance. If the amount is negative, zero, or greater than the
account balance, it should display an error message.
The program should display menu for the user, allowing them to choose between depositing,
withdrawing, or exiting the banking system. The menu should loop until the user chooses to
exit.
Output samples: