Oopass 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Roll Number: 2021F-BCE-199 Section: B Name:Muhammad Khalil Ashraf

Department: Computer Engineering Program: B.S (CE)

Assignment 1
CE – 225T : Object Oriented Programming

Announced Date:21-3-2021 Due Date:28-3-2021 Total Marks: 03 Marks


Marks Obtained: ________

Teacher Name: Dr. Umme Laila / Aneeta Siddiqui

Sr. No Course Learning Outcomes PLOs Blooms


Taxonomy

CLO_1 Interpret an understanding of object- PLO_1


C2
oriented paradigm and their mapping to (Engineering
(Understanding)
object-oriented programming language Knowledge)

1. List down most popular object oriented languages. Describe important features of Java
Language.

Some of the most popular object-oriented programming languages include Java,


C++, Python, Ruby, and C#. Java is a versatile language that can run on many
different devices and is designed to be safe and reliable. It has features like
automatic memory management and a lot of built-in tools that make programming
easier. It's used in many different applications, from big servers to small mobile
devices.
2. Define MyMath Class and which associated functions can be used for the following task with
proper function syntax and description should be also given.
 To get absolute value
 To get ceil of number
 To get floor of number
 To get max of two values
 To get min of two values
 To generate random number
 To calculate power for a number
 To calculate square root of number

1
ANS:

MyMath class is a programming class that contains different mathematical


functions which can be used to perform calculations in a program.

Here are some examples of functions and their descriptions:

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

public class ReverseArray {


private int[] Data = new int[5];

public static void main(String[] args) {


ReverseArray array = new ReverseArray();
System.out.println("Welcome to Reverse Array");

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

java.util.Scanner input = new java.util.Scanner(System.in);


int choice = input.nextInt();

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.");
}
}
}

public void populateData() {


Random random = new Random();
for (int i = 0; i < 5; i++) {
Data[i] = random.nextInt(10);
}
System.out.println("The array is populated.");
}

public void printContent() {


System.out.print("Array Data: ");
for (int i = 0; i < 5; i++) {
System.out.print(Data[i] + " ");
}
System.out.println();
}

public void reverse() {


int[] BackWards = Reverse(Data);
System.out.print("Array Revered: ");
for (int i = 0; i < 5; i++) {
System.out.print(BackWards[i] + " ");
}
System.out.println();
}

public static int[] Reverse(int[] arr) {


int[] BackWards = new int[5];
for (int i = 0; i < 5; i++) {
BackWards[4-i] = arr[i];
}
return BackWards;
}
}

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;

public class StringMatcher {

public static boolean match(String str1, String str2) {


// Check if the two strings have the same length
if (str1.length() != str2.length()) {
return false;
}

// Check each character in both strings


for (int i = 0; i < str1.length(); i++) {
char c1 = str1.charAt(i);
char c2 = str2.charAt(i);
5
// Check for wild card
if (c1 != c2 && c1 != '?' && c2 != '?') {
return false;
}
}

// If we haven't returned yet, the two strings match


return true;
}

public static void main(String[] args) {


Scanner input = new Scanner(System.in);

System.out.print("Enter string 1: ");


String str1 = input.nextLine();

System.out.print("Enter string 2: ");


String str2 = input.nextLine();

if (match(str1, str2)) {
System.out.println("They are equal");
} else {
System.out.println("They are different");
}
}

You might also like