Revision 1
Revision 1
Revision 1
OOP Concepts:
forms or behaviors.
parent class.
level and can be accessed and used throughout the entire class, including
• Private: an access modifier that restricts access to only within the same
class.
System.out.println(Math.pow(3, 2));
9.0
System.out.println(Math.ceil(3.4));
4.0
System.out.println(Math.floor(3.7));
3.0
Show the output of the following code: (write the output
next to each println statement).
System.out.println(Math.rint(3.5));
4.0
System.out.println(Math.rint(3.2));
3.0
System.out.println(Math.rint(3.8));
4.0
Show the output of the following code: (write the output
next to each println statement if the println statement is
executed in the program).
System.out.println(35 % 7);
true
false
Show the output of the following code: (write the output
next to each println statement).
if (number % 7 == 0)
System.out.println(2 * number);
else
System.out.println(3 * number);
45
Show the output of the following code: (write the output next to
each println statement).
int x = 1942;
System.out.println(x / 100);
19
System.out.println(x % 100);
42
1942 is even
Show the output of the following code: (write the output next to
each println statement).
System.out.println(str.substring(3, 7));
gram
System.out.println(str.substring(15));
JAVA
System.out.println(str.substring(0));
Programming in JAVA
Show the output of the following code: (write the output next to
each println statement).
int value=0;
while(value < 5)
value+=2;
int value=0;
do
value+=2;
while(value<7);
int value=0;
value+=2;
int value=1;
value*=2;
int value=0;
value++;
int value=0;
value--;
return area;
}
Write a Java method to find the sum of all even numbers
between two given numbers.
public static int sumEvenNumbers(int start, int end)
{
int sum = 0;
for (int i = start; i <= end; i++)
{
if (i % 2 == 0) {
sum += i;
}
}
return sum;
}
Write a Java method to count the number of vowels in a given
string.
public static int countVowels(String str) {
str = str.toLowerCase();
int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u’)
{
count++;
}
}
return count;
}
Write a Java method that takes a double score as input and
returns a character representing the corresponding grade
according to the following grading scale:
return 'A';
return 'B';
return 'C';
return 'D';
else
return 'F';
}
Answer(Using Switch Case):
public static char getGrade(double score) {
int grade = (int) (score / 10);
switch (grade) {
case 10:
case 9:
return 'A';
case 8:
return 'B';
case 7:
return 'C';
case 6:
return 'D';
default:
return 'F';
}
}
Write a Java method to return the greatest common divisor
(gcd) of two integers.
public static int gcd(int n1, int n2)
{
int gcd = 1;
int k = 1;
while (k <= n1 && k <= n2)
{
if (n1 % k == 0 && n2 % k == 0)
gcd = k;
k++;
}
return gcd;
}
Write a Java method that calculates the factorial of an integer
value:
int result = 1;
result *= i;
return result;
}
Write a Java method that calculates the factorial of an integer
value (Using Recursion):
if (n == 0) {
return 1;
} else {
}
Write a Java method to determine whether a given string is a
palindrome (reads the same backward or forward.).
public static boolean isPalindrome(String str) {
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
Write a Java method that determines whether a given positive
integer is prime.
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
Write a Java method that finds the maximum and minimum
elements in a given array.
Write a Java method that counts the number of even and odd
elements in a given array.
Write a Java method to find a key in an array using the linear
search algorithm.
Write a Java method to reverse a given array.
Write a Java method to sort a given array using the selection sort
algorithm.
Write a Java method that calculates the sum of two matrices and
returns the result as a new matrix.
- Design a Java class named ATM to emulate an Automated
Teller Machine, incorporating attributes: accountNumber and
balance and methods: deposit, withdraw, transferMoney, and
checkBalance. Implement setter and getter functions
(setAccountNumber, getAccountNumber, setBalance,
getBalance) to regulate access to private attributes.