0% found this document useful (0 votes)
4 views

CS110-Lab9_Student-1446

The document outlines a lab exercise for CS110T focused on programming methods, including predefined and user-defined methods. It consists of exercises that involve validating method calls, debugging code segments, and writing Java programs for various scenarios such as random number guessing and a bakery pricing system. Additionally, it includes assignment problems related to checking even/odd numbers and implementing a simple banking system.

Uploaded by

a4lla2024
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CS110-Lab9_Student-1446

The document outlines a lab exercise for CS110T focused on programming methods, including predefined and user-defined methods. It consists of exercises that involve validating method calls, debugging code segments, and writing Java programs for various scenarios such as random number guessing and a bakery pricing system. Additionally, it includes assignment problems related to checking even/odd numbers and implementing a simple banking system.

Uploaded by

a4lla2024
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CS110T: Programming Language1

Lab 9: Methods

Lab Objectives:
In this lab, the student will practice:
✔ Using predefined methods (Math Class)
✔ Declare and call User-Defined methods

1st Semester 2024-2025


Lab Exercise 1: Program Output
Consider the following statements:

double double1, double2, double3;


int int1, int2, int3;
double Total;
double1 = 5.0; double2= 6.5; double3 =3.0; int1 = 4; int2 =
7; int3 = 8;
and the method heading:
public static double sum (double a, int b, double c)

Which of the following statements are valid? If they are invalid, explain why.
a. Total = sum (double1, 15.0, double3);

b. System.out.println(sum (double1, int2 , int3));

c. System.out.println(sum (double1, int2, (int)double2));

d. System.out.println(sum (6.0, 8.0, 10.5));

e. System.out.println(double1 + " " + double2);

f. System.out.println(sum (double1, int2));

g. Total = sum (double1, int2, double3);

h. value = sum (7, 8, 9);

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_2A {

public static void main(String[] args) {


int name = DisplayName();
}

public static String DisplayName() {


String MyName = "Sara";
System.out.println(MyName);
return MyName;

}
}
public class EX_2B {

public static void main(String[] args) {


int ID = DisplayID(1111111111);
}

public static void DisplayID(int id) {


return id;
System.out.println(id);
}
}
public
publicclass
classEX_2C {
MathExample {
public static void main(String[] args) {
public static void main(String[] args) {
double result1 = Math.pow(2);
int num = 5;
double result2 = Math.sqroot(16);
String course = "CS110";
double result3 = Math.max(5, 10);;
printcourse(num, course);
} int result4 = Math.absValue(-5);
int randomValue = Math.random();
}
public static void printcourse(String c, int n) {
} for (int i = 0; i < num; i++) {
System.out.println(course);
}
}
}

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 static void displayNum(int num) {


System.out.println("The number is: " + num);
}
}

public class EX_3B {

public static void main(String[] args) {


System.out.println("The result is: "+sum(4, 3));
}

public static int sum(int num1, int num2) {


return num1 + num2;

}
}
public class EX_3c {

public static void main(String[] args) {


double result = div(10, 5);
System.out.println("The Result is :" + result);
}

public static double div(int num1, int num2) {


if (num2!=0)
return num1/num2;
else
System.out.println("Can’t divide by zero");

}
}
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:

Enter the lower limit: 0


Enter the upper limit: 10
Guess the number between 0 and 10: 10
Sorry! The correct number was: 4

Lab Excercise 5: Code Writing (2)


Problem Description: Imagine you are the owner of a small bakery called “Sweet Treats.” Every
day, you prepare various baked goods and serve them to your customers. Today, you want to help
your customers easily calculate the total price of their orders. Your bakery offers three main types
of treats: Cakes, Cookies, and Cupcakes. You need a method that will calculate the total price
based on the type of treat ordered and the quantity requested. Write a Java method named
calculateTotalPrice that performs the following:

The method should take two parameters:


- A String parameter for the type of treat (Cakes, Cookies, Cupcakes).
- An int parameter for the quantity of the treat.

Calculating the total price based on the following prices:


- Cakes: $15 each
- Cookies: $5 each
- Cupcakes: $3 each

The method should return the final price after calculating the total
Output sample:

Welcome to Sweet Treats Bakery!


Enter the type of treat (Cakes, Cookies, Cupcakes): Cakes
Enter the quantity: 4
Total price for 4 Cakes: $60.0

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:

Enter an integer number: 20 Enter an integer number: 3


20 is even 3 is odd

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:

You might also like