Oopass 1
Oopass 1
Assignment 1
CE – 225T : Object Oriented Programming
1. List down most popular object oriented languages. Describe important features of Java
Language.
1
ANS:
abs(): To get the absolute value of a number, which means its distance from
zero. Syntax: MyMath.abs(number)
ceil(): To round up a number to the next integer. Syntax:
MyMath.ceil(number)
floor(): To round down a number to the previous integer. Syntax:
MyMath.floor(number)
max(): To get the maximum of two numbers. Syntax:
MyMath.max(number1, number2)
min(): To get the minimum of two numbers. Syntax:
MyMath.min(number1, number2)
random(): To generate a random number between 0 and 1. Syntax:
MyMath.random()
pow(): To calculate the power of a number. Syntax: MyMath.pow(base,
exponent)
sqrt(): To calculate the square root of a number. Syntax:
MyMath.sqrt(number)
3. Define a class named ReverseArray. Your program should have an array of five integers
named Data. Use the Random method of Math class to generate a random number in the range of
0 through9 for each element in the array and call printContent() method to print elements of the
array. The program then call the Reverse() method which reverse the elements of array and
display their contents. Write the following methods in the program:
Public static void printContents(int [] arr) – This method prints each elementin the
array.
Public static int[]Reverse(int[]arr)- This method will reverse the contents of array and
return thenewly created array called BackWards[].
Here are two sample interactions. The user’s input is shown in bold.
Example 1
welcome to Reverse Array
1. Populate Array Data Randomly
2
2. Print Array Data
3. Reverse Array
your choice: 1
The Array is populated.
your choice: 2
Array Data: 3 4 5 6 7
your choice: 3
Array Revered:
your choice: 2
Reverse Data: 7 6 5 4 3
ANS:
import java.util.Random;
while (true) {
System.out.println("1. Populate Array Data Randomly");
System.out.println("2. Print Array Data");
System.out.println("3. Reverse Array");
System.out.println("4. Exit");
System.out.print("Your choice: ");
switch (choice) {
case 1:
array.populateData();
break;
case 2:
array.printContent();
break;
case 3:
array.reverse();
break;
case 4:
System.exit(0);
3
default:
System.out.println("Invalid choice.");
}
}
}
4
4. Define a static boolean method match which takes two Strings and returns true if and only if
the two Strings are of equal length and are equal, character for character, except that a ‘?’ in
either String counts as a wild card which matches any character in the corresponding position of
the other String. The Strings may be any length, including 0.
For example:
string1 string2 Result
“abc” “ab false
“” “” true
“abc” “abc” true
“abc” “aeb” False
“ab?” “abd” True
“a?c” “adc” True
“ab?” “a?c” True
Write a main method that asks the user to enter two strings, calls the method match, and displays
the result. The following are sample interactions. The user’s input is shown in bold.
Example 1
enter string 1: ab?
enter string 2: a?p
they are equal
Example 2
enter string 1: abc
enter string 2: abcd
they are different
Example 3
enter string 1: ab?de
enter string 2: abzde
they are equal
ANS:
import java.util.Scanner;
if (match(str1, str2)) {
System.out.println("They are equal");
} else {
System.out.println("They are different");
}
}