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

Revision 1

Uploaded by

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

Revision 1

Uploaded by

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

Computer Programming-2 CS213

Revision 1
OOP Concepts:

• Class: is a template that encapsulates data and behavior to create

objects with similar characteristics and functionalities.

• Object: is an instance of a class.

• Constructor: is invoked to create an object.

• Multiple constructors can be defined in a class.

• Constructors do not have a return type, not even void.

• Constructors must have the same name as the class itself.


OOP Concepts:

• Inheritance: is the process of extending and acquiring characteristics

from a parent class to a child class.

• Polymorphism: can be defined as the ability of an object to take on many

forms or behaviors.

• Method Overriding: is the process of providing a different

implementation of a method in a subclass that is already defined in its

parent class.

• Method Overloading: is the capability of defining multiple methods with

the same name but different parameters in a class.


OOP Concepts:

• Local Variable: can be defined as a variable that is declared and used

within a specific scope, such as a method, constructor, or block, and is

only accessible within that scope.

• Global variable: can be defined as a variable that is declared at the class

level and can be accessed and used throughout the entire class, including

its methods, constructors, and blocks.


OOP Concepts:
• Public: an access modifier that allows unrestricted access from any other
class or package.

• Private: an access modifier that restricts access to only within the same
class.

• Protected: an access modifier that allows access within the same


package and by subclasses.

• Default or Package-Private: allows access within the same package but


restricts access from classes in different packages.
Show the output of the following code: (Write the output
next to each println statement).

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

System.out.println(3 + 4.5 * 2 < 2 * 9.5);

true

System.out.println(3 + 4.5 * 4 < 2 * 9.5);

false
Show the output of the following code: (write the output
next to each println statement).

int number = 15;

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

System.out.println(x + " is " + ((x % 2 == 0) ? "even" : "odd"));

1942 is even
Show the output of the following code: (write the output next to
each println statement).

String str= "Programming in JAVA";

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;

System.out.println("The final value equals "+value);

The final value equals 6


Show the output of the following code: (write the output next to
each println statement).

int value=0;

do

value+=2;

while(value<7);

System.out.println("The final value equals "+value);

The final value equals 8


Show the output of the following code: (write the output next to
each println statement).

int value=0;

for (int i=0; i< 6; i++)

value+=2;

System.out.println("The final value equals "+value);

The final value equals 12


Show the output of the following code: (write the output next to
each println statement).

int value=1;

for (int i=0; i<3; i++)

value*=2;

System.out.println("The final value equals "+value);

The final value equals 8


Show the output of the following code: (write the output next to
each println statement).

int value=0;

for (int i=0; i< 5; i++)

for (int j=0; j<6; j++)

value++;

System.out.println("The final value equals "+value);

The final value equals 30


Show the output of the following code: (write the output next to
each println statement).

int value=0;

for (int i=4; i< 6; i++)

for(int j=2; j<5; j++)

value--;

System.out.println("The final value equals "+value);

The final value equals -6


Write a Java method to calculate the area of a circle given its
radius.

public static double calculateCircleArea(double radius)

double area = Math.PI * radius * radius;

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:

A: score >= 90.0

B: score >= 80.0 and score < 90.0

C: score >= 70.0 and score < 80.0

D: score >= 60.0 and score < 70.0

F: score < 60.0


Answer:
public static char getGrade(double score) {

if (score >= 90.0)

return 'A';

else if (score >= 80.0)

return 'B';

else if (score >= 70.0)

return 'C';

else if (score >= 60.0)

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:

public static int factorial(int n) {

int result = 1;

for (int i = 2; i <= n; i++) {

result *= i;

return result;

}
Write a Java method that calculates the factorial of an integer
value (Using Recursion):

public static int factorial(int n) {

if (n == 0) {

return 1;

} else {

return n * factorial(n - 1);

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

- Showcase functionality through instances of the ATM class,


engaging in transactions like deposits, withdrawals, money
transfers, and balance checks.
ATM Class:
ATM Class:
ATM Class:
ATM Class:
ATM Class:
Computer Programming-2 CS213

Copyright © 2020 Pearson Education, Inc. All Rights Reserved

You might also like