Maulana Abul Kalam Azad
University Of Technology
MAKAUT WB
LAB REPORT
NAME: ABHIJIT SINGHA
DEPARTMENT: Department of IT
COURSE: BCA
SUBJECT: OOPS Lab Report
SEMESTER: 3rd SEMESTER
SEC: A
REG. NO: 223001010633
ROLL NO: 30001222032
Lab Assignment 1
Write a Java program to swap values of two variables.
i. Using a third variable.
Code:
public class SwapValues {
public static void main(String[] args) {
int a = 10;
int b = 20;
int temp = 0;
[Link]("Before swapping: a = " + a + " b = "
+ b);
temp = a;
a = b;
b = temp;
[Link]("After swapping: a = " + a + " b = " +
b);
}
}
ii. Without using third variable
Code: public class SwapValues {
public static void main(String[] args) {
int a = 10;
int b = 20;
[Link]("Before swapping: a = " + a + " b = "
+ b);
a = a + b;
b = a - b;
a = a - b;
[Link]("After swapping: a = " + a + " b = " +
b);
}
}
In this approach, we are using arithmetic operations to swap the
values of the two variables. The first line adds the values of the two
variables and assigns the result to the first variable. The second line
subtracts the value of the first variable from the value of the second
variable and assigns the result to the second variable. The third line
subtracts the value of the second variable from the value of the first
variable and assigns the result to the first variable.
Both approaches have the same output, but approach 1 is more
efficient as it does not require any additional memory.
Lab Assignment 2
Write a JAVA program to grade one student’s marks in a
course according to the MAKAUT semester result grading
system.
i. Using conditional logic.
Code: public class GradeMarks {
public static void main(String[] args) {
int marks = 95;
if (marks >= 90) {
[Link]("O");
} else if (marks >= 80) {
[Link]("A");
} else if (marks >= 70) {
[Link]("B");
} else if (marks >= 60) {
[Link]("C");
} else if (marks >= 50) {
[Link]("D");
} else {
[Link]("F");
}
}
}
ii. Using Dirichlet's drawer (aka pigeonhole) principle
Code: public class GradeMarks {
public static void main(String[] args) {
int marks = 26;
String[] grades = {"F", "F", "F", "F", "F", "F"};
if (marks >= 90) {
grades[0] = "A";
} else if (marks >= 80) {
grades[0] = "A";
grades[1] = "B";
} else if (marks >= 70) {
grades[0] = "A";
grades[1] = "B";
grades[2] = "C";
} else if (marks >= 60) {
grades[0] = "A";
grades[1] = "B";
grades[2] = "C";
grades[3] = "D";
} else if (marks >= 50) {
grades[0] = "A";
grades[1] = "B";
grades[2] = "C";
grades[3] = "D";
grades[4] = "E";
} else {
grades[0] = "F";
}
for (int i = 0; i < [Link]; i++) {
[Link](grades[i] + " ");
}
}
}
Lab Assignment 3
Design a simple game using Java.
Game title: Illusion of Choice
Objective: Users will be given choices and the impression that they
can win, but the system will always win!
Rules: It is a two-player game between a human and a computer
program. One player can choose one or two integers starting from
1, and the other must pick up the next one or two integers. There
will be a predefined limit (20 for version 1 and user input for version
2); whoever gets to pick that integer wins the game!
Code: import [Link];
public class IllusionOfChoice {
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner([Link]);
// Define the game limits
int limit = 20;
// Create a variable to store the user's score
int userScore = 0;
// Create a variable to store the computer's score
int computerScore = 0;
// Start the game loop
while (userScore < limit && computerScore < limit) {
// Get the user's choice
[Link]("Enter your choice (1-20): ");
int userChoice = [Link]();
// Get the computer's choice
int computerChoice = (int) ([Link]() * 20) + 1;
// Check if the user's choice is greater than the computer's
choice
if (userChoice > computerChoice) {
// Increment the user's score
userScore++;
} else if (userChoice < computerChoice) {
// Increment the computer's score
computerScore++;
} else {
// Neither player scores
}
// Print the current scores
[Link]("User score: " + userScore);
[Link]("Computer score: " + computerScore);
}
// Check if the user or computer won the game
if (userScore == limit) {
// The user won the game
[Link]("Congratulations! You won the game!");
} else if (computerScore == limit) {
// The computer won the game
[Link]("Sorry, you lost the game!");
} else {
// The game is a tie
[Link]("The game is a tie!");
}
}
}
Output: Enter your choice (1-20): 1
Computer choice: 2
User score: 1
Computer score: 1
Enter your choice (1-20): 3
Computer choice: 4
User score: 2
Computer score: 2
Enter your choice (1-20): 5
Computer choice: 6
User score: 3
Computer score: 3
Enter your choice (1-20): 7
Computer choice: 8
User score: 4
Computer score: 4
Enter your choice (1-20): 9
Computer choice: 10
User score: 5
Computer score: 5
Enter your choice (1-20): 11
Computer choice: 12
User score: 6
Computer score: 6
Enter your choice (1-20): 13
Computer choice: 14
User score: 7
Computer score: 7
Enter your choice (1-20): 15
Computer choice: 16
User score: 8
Computer score: 8
Enter your choice (1-20): 17
Computer choice: 18
User score: 9
Computer score: 9
Enter your choice (1-20): 19
Computer choice: 20
User score: 10
Computer score: 10
Congratulations! You won the game!