SOURCE CODE
QUESTION 1:
Write a java program to take input as a command line argument. Your name, course,
university rollno and semester. Display the information.
import [Link];
class ques1 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter information
[Link]("Enter your name:");
String name = [Link]();
[Link]("Enter your course:");
String course = [Link]();
[Link]("Enter your university roll number:");
String rollNo = [Link]();
[Link]("Enter your semester:");
String semester = [Link]();
// Display the information
[Link]("Name: " + name + "\tCourse: " + course);
[Link]("University Roll No: " + rollNo +"\t Semester: " + semester);
[Link]("**********************************************************************
***");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("**********************************************************************
***");
[Link]();
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.1
OUTPUT
QUESTION 1:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.2
SOURCE CODE
QUESTION 2:
Program to find if the given numbers are Friendly pair or not (Amicable or not). Friendly
Pair are two or more numbers with a common abundance. Input & Output format:
Input consists of 2 integers.
The first integer corresponds to number 1 and the second integer corresponds to number 2.
If it is a Friendly Pair display Friendly Pair or displays Not Friendly Pair.
import [Link];
public class ques2 {
public static void main(String[] args) {
Scanner cs = new Scanner([Link]);
int num1, num2, i;
[Link]("Enter two numbers:");
num1 = [Link]();
num2 = [Link]();
int sum1 = 0;
int sum2 = 0;
for (i = 1; i < num1; i++) {
if (num1 % i == 0) {
sum1 = sum1 + i;
}
}
for (i = 1; i < num2; i++) {
if (num2 % i == 0) {
sum2 = sum2 + i;
}
}
boolean isFriendlyPair = (double) num1 / num2 == (double) sum1 / sum2;
[Link]("It is a Friendly Pair: " + isFriendlyPair);
[Link]("**********************************************************************
***");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("**********************************************************************
***");
[Link]();
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.3
OUTPUT
Question 2:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.4
SOURCE CODE
QUESTION 3:
The problem to rearrange positive and negative numbers in an array . Method: This approach
moves all negative numbers to the beginning and positive numbers to the end but changes
the order of appearance of the elements of the array. Steps: 1. Declare an array and input the
array elements. 2. Start traversing the array and if the current element is negative, swap the
current element with the first positive element and continue traversing until all the elements
have been encountered. 3. Print the rearranged array.
import [Link];
public class ques3 {
// Function to rearrange positive and negative numbers
public static void rearrange(int[] arr) {
int n = [Link];
int positiveIndex = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
if (arr[i] < 0) {
// Swap negative element with the first positive element
int temp = arr[i];
arr[i] = arr[positiveIndex];
arr[positiveIndex] = temp;
positiveIndex++;
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input the array elements
[Link]("Enter the elements of the array separated by spaces:");
String input = [Link]();
String[] inputArray = [Link](" ");
int[] arr = new int[[Link]];
for (int i = 0; i < [Link]; i++) {
arr[i] = [Link](inputArray[i]);
}
// Rearrange the array
rearrange(arr);
// Print the rearranged array
[Link]("Rearranged array:");
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.5
for (int num : arr) {
[Link](num + " ");
}
[Link](" \
n*************************************************************************");
[Link]("Implemented By: brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("
*************************************************************************");
[Link]();
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.6
OUTPUT
Question 3:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.7
SOURCE CODE
QUESTION 4:
Program to find the saddle point coordinates in a given matrix. A saddle point is an element
of the matrix, which is the minimum element in its row and the maximum in its column. For
example, consider the matrix given below Mat[3][3] int mat[] = { {1,2,3},{4,5,6},{7,8,9}}; 1
2 3 4 5 6 7 8 9 Here, 7 is the saddle point because it is the minimum element in its row and
maximum element in its column. Steps to find the saddle point coordinates in a given matrix
Input the matrix from the user.
Use two loops, one for traversing the row and the other for traversing the column.
If the current element is the minimum element in its row and maximum element in its
column, then return its coordinates.
Else, continue traversing.
import [Link];
public class ques4 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input the matrix dimensions
[Link]("Enter the number of rows: ");
int rows = [Link]();
[Link]("Enter the number of columns: ");
int cols = [Link]();
// Input the matrix
int[][] matrix = new int[rows][cols];
[Link]("Enter the elements of the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = [Link]();
}
}
// Find the saddle point
int saddleRow = -1;
int saddleCol = -1;
for (int i = 0; i < rows; i++) {
int minRow = Integer.MAX_VALUE;
int minColIndex = -1;
for (int j = 0; j < cols; j++) {
if (matrix[i][j] < minRow) {
minRow = matrix[i][j];
minColIndex = j;
}
}
boolean isMaxInColumn = true;
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.8
for (int k = 0; k < rows; k++) {
if (matrix[k][minColIndex] > minRow) {
isMaxInColumn = false;
break;
}
}
if (isMaxInColumn) {
saddleRow = i;
saddleCol = minColIndex;
break;
}
}
// Print the saddle point coordinates
if (saddleRow != -1 && saddleCol != -1) {
[Link]("Saddle point found at position: (" + (saddleRow + 1) + ", " + (saddleCol + 1) +
")");
[Link]("Value: " + matrix[saddleRow][saddleCol]);
} else {
[Link]("No saddle point found.");
}
[Link]("
*************************************************************************");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("
*************************************************************************");
[Link]();
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.9
OUTPUT
Question 4:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.10
SOURCE CODE
QUESTION 5:
The daily maximum temperatures recorded in 5 cities during the month of April (for first 10
days) have been tabulated as follows:
import [Link].*;
public class temp {
public static void main(String[] args) {
String cities[] = {"Days "," Delhi ","Mumbai ", "Kolkata ", "Chennai ", "Dehradun"};
int temp[][] = {{16, 18, 35, 42, 23}, {18, 38, 31, 24, 43}, {11, 38, 35, 42, 13}, {17, 28, 45, 22, 6}, {26,
19, 37, 33, 13}};
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int minr = -1, minc = -1, maxr = -1, maxc = -1;
for(int i = 0; i < 6; i++)
[Link](cities[i]);
[Link]();
for (int i = 0; i < 5; i++) {
[Link]((i+1)+"\t");
for (int j = 0; j < 5; j++) {
if (max < temp[i][j]) {
max = temp[i][j];
maxr = i;
maxc = j;
}
if (min > temp[i][j]) {
min = temp[i][j];
minr = i;
minc = j;
}
[Link](temp[i][j] + "\t");
}
[Link]();
}
[Link]();
[Link]("City with maximum temperature is " + max + " on Day " + (maxc + 1) + " in " +
cities[maxr]);
[Link]("City with minimum temperature is " + min + " on Day " + (minc + 1) + " in " +
cities[minr]);
[Link]("
*************************************************************************");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("
*************************************************************************");
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.11
OUTPUT
Question 5:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.12
SOURCE CODE
QUESTION 6:
Write a program that allows the user to enter the names of five candidates in a local election
and the number of votes received by each candidate. The program should then produce the
following output:
Each candidate’s name, the number of votes received.
The percentage of total votes received by each candidates
Display the Winner of the election.
import [Link];
public class ques6 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Arrays to store candidate names and their corresponding votes
String[] candidates = new String[5];
int[] votes = new int[5];
int totalVotes = 0;
// Input candidate names and votes
for (int i = 0; i < 5; i++) {
[Link]("Enter candidate name: ");
candidates[i] = [Link]();
[Link]("Enter number of votes received: ");
votes[i] = [Link]();
[Link](); // Consume newline character
totalVotes += votes[i];
}
// Display header
[Link]("Candidate Name\tVotes Received\tPercentage of Total Votes Received");
// Display candidate names, votes received, and percentage of total votes
for (int i = 0; i < 5; i++) {
double percentage = (double) votes[i] / totalVotes * 100;
[Link]("%-15s\t%-14d\t%.2f%%\n", candidates[i], votes[i], percentage);
}
// Find the winner
int maxVotesIndex = 0;
for (int i = 1; i < 5; i++) {
if (votes[i] > votes[maxVotesIndex]) {
maxVotesIndex = i;
}
}
// Display the winner
[Link]("\nThe Winner Candidate name is: " + candidates[maxVotesIndex]);
[Link]("
*************************************************************************");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("
*************************************************************************");
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.13
}
}
OUTPUT
Question 6:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.14
SOURCE CODE
QUESTION 7:
Write a Java program to print all permutations of a given string with repetition. The given
string is: PQR The permuted strings are: PPP PPQ PPR ... RRP RRQ RRR
import [Link];
public class ques7 {
// Function to print all permutations of a string with repetition
public static void printPermutations(String str, String prefix, int length) {
// Base case: if length of prefix is equal to required length, print it
if ([Link]() == length) {
[Link](prefix + " "); // Print the permutation followed by a space
return;
}
// Recursive case: add each character of the string to the prefix and call the function recursively
for (int i = 0; i < [Link](); i++) {
printPermutations(str, prefix + [Link](i), length);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input the string
[Link]("Enter the string: ");
String input = [Link]();
// Print all permutations with repetition
[Link]("The permuted strings are:");
printPermutations(input, "", [Link]());
[Link]();
[Link]("
*************************************************************************");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("
*************************************************************************");
// Close the scanner
[Link]();
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.15
OUTPUT
Question 7:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.16
SOURCE CODE
QUESTION 8:
Write a java program to reverse each word of string. e.g. Input- I love my India Output – I
evol ym aidnI)
import [Link].*;
public class ques8{
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String str1,str2="";
[Link]("Enter the string to reverse the string");
str1=[Link]();
int n= [Link]();
int prespace=-1;
for(int i=0;i<n;i++)
{
if([Link](i)==' '|| i==n-1)
{
int j=i;
while(j>prespace)
{
str2=str2+[Link](j);
j--;
}
str2+=" ";
prespace=i;
}
}
str2=[Link]();
[Link]();
[Link](str2);
[Link]("
*************************************************************************");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("
*************************************************************************");
[Link]();
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.17
OUTPUT
Question 8:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.18
SOURCE CODE
QUESTION 9:
Write a Java program to count the occurrences of a given string in another given string.
Sample Output: aa' has occured 3 times in 'abcd abc aabc baa abcaa'.
import [Link].*;
public class ques9{
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the main string: ");
String mstring = [Link]();
[Link]("Enter the string to count: ");
String search = [Link]();
int occurrences = countOccurrences(mstring, search);
[Link]("Occurrences of '" + search + "' in '" + mstring + "': " + occurrences);
[Link]("
*************************************************************************");
[Link]("Implemented By: Sagar Giri\t Class Roll No. 51\t CSE4(A2)");
[Link]("
*************************************************************************");
[Link]();
}
private static int countOccurrences(String mstring, String search) {
int count = 0;
int index = 0;
int n=[Link]();
while ((index = [Link](search, index)) != -1) {
count++;
index += n;
}
return count;
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.19
OUTPUT
Question 9:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.20
SOURCE CODE
QUESTION 10:
Write a program to sort characters of a given string s, sort it in decreasing order based on the
frequency of the characters. The frequency of a character is the number of times it appears in
the string. Return the sorted string. If there are multiple answers, return any of them.
import [Link].*;
public class ques10 {
public static String frequencySort(String s) {
Map<Character, Integer> frequencyMap = new HashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
PriorityQueue<Character> maxHeap = new PriorityQueue<>((a, b) -> [Link](b) -
[Link](a));
[Link]([Link]());
StringBuilder sortedString = new StringBuilder();
while (![Link]()) {
char currentChar = [Link]();
int frequency = [Link](currentChar);
for (int i = 0; i < frequency; i++) {
[Link](currentChar);
}
}
return [Link]();
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the string: ");
String input = [Link]();
String result = frequencySort(input);
[Link]("Output: " + result);
[Link]();
[Link]("********************************************************************");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("********************************************************************");
[Link]("\n");
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.21
OUTPUT
Question 10:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.22
SOURCE CODE
QUESTION 11:
A class Telcall calculates the monthly phone bill of a consumer. Some of the members of the
class are given below: Class name: Data members/instance variable : phno(phone Number),
sname(subscriber Name ) n(number of calls made) and amt (bill amount). Member
function/methods: TelCall() : Parameterized constructor to assign values to data members.
Void compute( ) : to calculate the phone bill amount base on the slabs given below. Void
display( ) : to display the details in the specified format. Number of calls Rate 1 – 100 Rs.
500/- rental charge only 101 – 200 Rs 1.00 per call + rental charge 201-300 Rs. 1.20 per call
+ rental charge Above 300 Rs. 1.50 per call + rental charge
import [Link];
public class ques11 {
private String phno;
private String sname;
private int n;
private double amt;
public ques11(String phno, String sname, int n) {
[Link] = phno;
[Link] = sname;
this.n = n;
}
public void compute() {
if (n <= 100) {
amt = 500;
} else if (n <= 200) {
amt = 500 + (n - 100) * 1;
} else if (n <= 300) {
amt = 500 + 100 + (n - 200) * 1.20;
} else {
amt = 500 + 100 + 100 * 1.20 + (n - 300) * 1.50;
}
}
public void display() {
[Link]("Subscriber Name: " + sname);
[Link]("Phone Number: " + phno);
[Link]("Number of calls made: " + n);
[Link]("Bill Amount: Rs. " + amt);
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.23
[Link]("Enter Subscriber Name: ");
String sname = [Link]();
[Link]("Enter Phone Number: ");
String phno = [Link]();
[Link]("Enter Number of calls made: ");
int n = [Link]();
ques11 telCall = new ques11(phno, sname, n);
[Link]();
[Link]();
[Link]();
[Link]("\n");
[Link]("********************************************************************");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("********************************************************************");
[Link]("\n");
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.24
OUTPUT
Question 11:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.25
SOURCE CODE
QUESTION 12:
Design a class to represent bank account. Includes the following members: Name of
depositor Account number Type of account Balance amount in the account Methods:
To assign initial values To deposit an amount To withdraw an amount after checking
balance. To display the name and balance.
Write a program to incorporate the constructor to provide initial values, use this keyword and
also instantiate its object.
import [Link];
public class ques12 {
// Members
private String depositorName;
private String accountNumber;
private String accountType;
private double balance;
public ques12(String depositorName, String accountNumber, String accountType, double initialBalance)
{
[Link] = depositorName;
[Link] = accountNumber;
[Link] = accountType;
[Link] = initialBalance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
[Link]("Successfully deposited " + amount + " into account " + accountNumber);
} else {
[Link]("Invalid deposit amount");
}
}
public void withdraw(double amount) {
if (amount > 0) {
if (balance >= amount) {
balance -= amount;
[Link]("Successfully withdrew " + amount + " from account " + accountNumber);
} else {
[Link]("Insufficient balance");
}
} else {
[Link]("Invalid withdrawal amount");
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.26
public void display() {
[Link]("Depositor Name: " + depositorName);
[Link]("Account Number: " + accountNumber);
[Link]("Account Type: " + accountType);
[Link]("Balance: " + balance);
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter depositor name: ");
String name = [Link]();
[Link]("Enter account number: ");
String number = [Link]();
[Link]("Enter account type: ");
String type = [Link]();
[Link]("Enter initial balance: ");
double balance = [Link]();
ques12 account = new ques12(name, number, type, balance);
[Link]();
[Link]("Choose an operation:");
[Link]("1. Deposit");
[Link]("2. Withdraw");
[Link]("Enter your choice: ");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter amount to deposit: ");
double depositAmount = [Link]();
[Link](depositAmount);
break;
case 2:
[Link]("Enter amount to withdraw: ");
double withdrawAmount = [Link]();
[Link](withdrawAmount);
break;
default:
[Link]("Invalid choice");
}
[Link]();
[Link]("********************************************************************");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("********************************************************************");
[Link]();
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.27
}
OUTPUT
Question 12:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.28
SOURCE CODE
QUESTION 13:
Write a program to display the information about the student in tabulated form using an array
of objects.
import [Link];
class Student {
private int rollNumber;
private String name;
private int age;
private String course;
public Student(int rollNumber, String name, int age, String course) {
[Link] = rollNumber;
[Link] = name;
[Link] = age;
[Link] = course;
}
public int getRollNumber() {
return rollNumber;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getCourse() {
return course;
}
}
public class ques13 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of students: ");
int numStudents = [Link]();
[Link]();
Student[] students = new Student[numStudents];
for (int i = 0; i < numStudents; i++) {
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.29
[Link]("\nEnter details for student " + (i + 1) + ":");
[Link]("Roll Number: ");
int rollNumber = [Link]();
[Link]();
[Link]("Name: ");
String name = [Link]();
[Link]("Age: ");
int age = [Link]();
[Link]();
[Link]("Course: ");
String course = [Link]();
students[i] = new Student(rollNumber, name, age, course);
}
[Link]("\nStudent Information:");
[Link]("----------------------------------------------");
[Link]("| Roll No | Name | Age | Course |");
[Link]("----------------------------------------------");
for (Student student : students) {
[Link]("| %-8d| %-6s | %-3d | %-17s |\n", [Link](), [Link](),
[Link](), [Link]());
}
[Link]("----------------------------------------------");
[Link]("
*************************************************************************");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("
*************************************************************************");
[Link]();
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.30
OUTPUT
Question 13:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.31
SOURCE CODE
QUESTION 14:
Write a program to incorporate the constructor to provide initial values, use this
keyword and also instantiate its object.
import [Link].*;
public class ques14 {
private String name;
private int age;
// Constructor with parameters using 'this' keyword
public ques14(String name, int age) {
[Link] = name;
[Link] = age;
}
// Method to display the details of the person
public void displayDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
public static void main(String[] args) {
// Instantiating an object of the ques14 class using the constructor
ques14 person1 = new ques14("brahmi", 18);
// Displaying details of person1
[Link]("Details of person1:");
[Link]();
// Instantiating another object of the ques14 class using the constructor
ques14 person2 = new ques14("guddu", 19);
// Displaying details of person2
[Link]("\nDetails of person2:");
[Link]();
[Link]("
*************************************************************************");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t
CSE4(A2)");
[Link]("
*************************************************************************");
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.32
OUTPUT
Question 14:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.33
SOURCE CODE
QUESTION 15:
Demonstrate all possible use of super keyword in a single program
class Animal {
String name;
public Animal(String name) {
[Link] = name;
}
public void makeSound() {
[Link]("Generic animal sound");
}
}
class Dog extends Animal {
boolean isGoodBoy;
public Dog(String name, boolean isGoodBoy) {
super(name);
[Link] = isGoodBoy;
}
@Override
public void makeSound() {
[Link]();
[Link]("Woof!");
}
public void accessSuperField() {
[Link]("My name is: " + [Link]);
}
}
public class ques15 {
public static void main(String[] args) {
Dog dog = new Dog("Buddy", true);
[Link]();
[Link]();
[Link]("
*************************************************************************");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("
*************************************************************************");
[Link]("\n");
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.34
OUTPUT
Question 15:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.35
SOURCE CODE
QUESTION 16:
Write a program to create a class named shape. In this class we have three sub classes circle,
triangle and square each class has two member function named draw () and erase (). Create
these using runtime polymorphism concepts.
class Shape {
public void draw() {
[Link]("Drawing a shape");
}
public void erase() {
[Link]("Erasing a shape");
}
}
class Circle extends Shape {
@Override
public void draw() {
[Link](); // Call parent class draw method
[Link]("Drawing a circle");
}
@Override
public void erase() {
[Link](); // Call parent class erase method
[Link]("Erasing a circle");
}
}
class Triangle extends Shape {
@Override
public void draw() {
[Link](); // Call parent class draw method
[Link]("Drawing a triangle");
}
@Override
public void erase() {
[Link](); // Call parent class erase method
[Link]("Erasing a triangle");
}
}
class Square extends Shape {
@Override
public void draw() {
[Link](); // Call parent class draw method
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.36
[Link]("Drawing a square");
}
@Override
public void erase() {
[Link](); // Call parent class erase method
[Link]("Erasing a square");
}
}
public class ques16 {
public static void main(String[] args) {
// Create objects using polymorphism
Shape shape1 = new Circle();
Shape shape2 = new Triangle();
Shape shape3 = new Square();
// Call draw and erase methods on each shape object
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]("\n");
[Link]("
*************************************************************************");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("
*************************************************************************");
[Link]("\n");
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.37
OUTPUT
Question 16:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.38
SOURCE CODE
QUESTION 17:
Solve the following problem by using runtime polymorphism: Following tables outlines the
major credit cards you might want to validate, along with their allowed prefixes and lengths.
Major Credit Cards, Their Prefixes, and Lengths .
import [Link];
class CreditCard {
String cardNumber;
CreditCard(String cardNumber) {
[Link] = cardNumber;
}
boolean isValid() {
return false;
}
}
class MasterCard extends CreditCard {
MasterCard(String cardNumber) {
super(cardNumber);
}
@Override
boolean isValid() {
return [Link]("^5[1-5]\\d{14}$");
}
}
class Visa extends CreditCard {
Visa(String cardNumber) {
super(cardNumber);
}
@Override
boolean isValid() {
return [Link]("^4\\d{12}(\\d{3})?$");
}
}
class AmericanExpress extends CreditCard {
AmericanExpress(String cardNumber) {
super(cardNumber);
}
@Override
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.39
boolean isValid() {
return [Link]("^3[47]\\d{13}$");
}
}
public class ques17 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input credit card numbers
[Link]("Enter MasterCard number: ");
String masterCardNumber = [Link]();
[Link]("Enter Visa number: ");
String visaNumber = [Link]();
[Link]("Enter American Express number: ");
String amexNumber = [Link]();
[Link]();
// Validate credit cards
CreditCard masterCard = new MasterCard(masterCardNumber);
CreditCard visa = new Visa(visaNumber);
CreditCard amex = new AmericanExpress(amexNumber);
// Display validation results
[Link]("MasterCard valid? " + [Link]());
[Link]("Visa valid? " + [Link]());
[Link]("American Express valid? " + [Link]());
// Display credits
[Link]("
*************************************************************************");
[Link]("Implemented By: Brahmi bora\t Class Roll No. 22\t CSE4(A2)");
[Link]("
*************************************************************************");
[Link]("\n");
}
}
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.40
OUTPUT
Question 17:
Implemented by: Brahmi bora Class Roll No : 22 CSE(4)A2
Page no.41