[go: up one dir, main page]

0% found this document useful (0 votes)
11 views10 pages

CA Paper comp

The document contains multiple Java classes that implement various functionalities such as calculating EMI, checking if digits of a number are in ascending order, performing binary search on a sorted array, handling 2D arrays to find composite numbers, sorting words from a sentence, and displaying patterns using characters and numbers. Each class has methods for input, processing data, and displaying results. The main method in each class creates an instance and calls the relevant methods to execute the functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

CA Paper comp

The document contains multiple Java classes that implement various functionalities such as calculating EMI, checking if digits of a number are in ascending order, performing binary search on a sorted array, handling 2D arrays to find composite numbers, sorting words from a sentence, and displaying patterns using characters and numbers. Each class has methods for input, processing data, and displaying results. The main method in each class creates an instance and calls the relevant methods to execute the functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Q3. import java.util.

Scanner;

class EMICalc {

// Data members

double p; // Purchase amount

double r; // Rate of interest

int n; // Number of years of tenure

// Default constructor to initialize members with legal default values

EMICalc() {

p = 0.0;

r = 0.0;

n = 0;

// Method to accept input

void get() {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the purchase amount: ");

p = sc.nextDouble();

System.out.print("Enter the number of years for the tenure: ");

n = sc.nextInt();

// Method to calculate the amount as per rate of interest

double calcAmt() {

if (p < 25000) {

r = 12; // Rate of interest 12%

} else {

r = 15; // Rate of interest 15%

return p * Math.pow((1 + r / 100), n);

// Method to display details

void display() {

double totalAmount = calcAmt();

int months = n * 12; // Convert years to months

int EMI = (int) Math.round(totalAmount / months); // EMI rounded to nearest integer

System.out.println("Purchase amount\tRate of interest\tAmount with interest\tEMI");


System.out.println(p + "\t" + r + "\t" + totalAmount + "\t" + EMI);

public static void main(String[] args) {

// Create an object 'ob'

EMICalc ob = new EMICalc();

// Call the methods

ob.get();

ob.display();

Q4. import java.util.Scanner;

class AscendingNumber {

int n;

// Method to take input

void get() {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number (maximum 5 digits): ");

n = sc.nextInt();

// Method to check if digits are in ascending order

boolean isAscending() {

int b = 10; // Initialize to a value greater than any digit

int temp = n;

while (temp > 0) {

int d = temp % 10; // Extract the last digit

if (d > b) {

return false; // Not ascending

b = d;

temp /= 10; // Remove the last digit

return true;

// Method to display result

void display() {
if (isAscending()) {

System.out.println(n + " is an Ascending number.");

} else {

System.out.println(n + " is NOT an Ascending number.");

public static void main(String[] args) {

AscendingNumber ob = new AscendingNumber();

ob.get();

ob.display();

Q5. import java.util.*;

//Descending sorted array binary search

class Binary{

String[] list = {"A", "%", "B", "c", "$", "E", "!", "p", "^", "W"};

String s;

//get method for accepting a character to search

void get() {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the value to search: ");

s = sc.next();

//Sorting in descending order before binary search

void sortDescending() {

for (int i = 0; i < list.length - 1; i++) {

for (int j = i + 1; j < list.length; j++) {

if (list[i].compareTo(list[j]) < 0) {

String temp = list[i];

list[i] = list[j];

list[j] = temp;

}
//binary search method

void binarySearch() {

int low = 0;

int high = list.length - 1;

boolean found = false;

while (low <= high) {

int mid = (low + high) / 2;

if (list[mid].compareTo(s) == 0) {

found = true;

break;

} else if (list[mid].compareTo(s) > 0) {

low = mid + 1;

} else {

high = mid - 1;

if (found) {

System.out.println("Search successful");

} else {

System.out.println("Search element not found");

public static void main(String[] args) {

Binary ob = new Binary();

ob.sortDescending();

ob.get();

ob.binarySearch();

Q6. import java.util.*;

class Array2D {

int[][] arr;

int rows, cols;

void get() {

Scanner sc = new Scanner(System.in);


System.out.print("Enter number of rows: ");

rows = sc.nextInt();

System.out.print("Enter number of columns: ");

cols = sc.nextInt();

arr = new int[rows][cols];

System.out.println("Enter elements of the array:");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

arr[i][j] = sc.nextInt();

void display() {

System.out.println("The array is:");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

System.out.print(arr[i][j] + " ");

System.out.println();

int findMaxComposite() {

int max = -1;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

if (isComposite(arr[i][j]) && arr[i][j] > max) {

max = arr[i][j];

return max;

int findMinComposite() {

int min = -1; // Initialize to -1 for cases where no composite numbers are found

boolean firstCompositeFound = false;


for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

if (isComposite(arr[i][j])) {

if (!firstCompositeFound) {

min = arr[i][j];

firstCompositeFound = true;

} else if (arr[i][j] < min) {

min = arr[i][j];

return min;

boolean isComposite(int num) {

if (num < 4) return false;

for (int i = 2; i <= Math.sqrt(num); i++) {

if (num % i == 0) return true;

return false;

void findDifference() {

int maxComposite = findMaxComposite();

int minComposite = findMinComposite();

System.out.println("Maximum Composite Number: " + maxComposite);

System.out.println("Minimum Composite Number: " + minComposite);

if (maxComposite == -1 || minComposite == -1) {

System.out.println("Absolute Difference: -1");

} else {

System.out.println("Absolute Difference: " + Math.abs(maxComposite - minComposite));

public static void main(String[] args) {

Array2D ob = new Array2D();

ob.get();
ob.display();

ob.findDifference();

Q7. import java.util.*;

class Sentence {

String sentence;

void get() {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a sentence with at least 4 words:");

sentence = sc.nextLine();

void convert() {

// Manually parse words

String[] words = new String[100];

int wordCount = 0;

String currentWord = "";

for (int i = 0; i < sentence.length(); i++) {

char ch = sentence.charAt(i);

if (ch == ' ') {

if (!currentWord.equals("")) {

words[wordCount++] = currentWord;

currentWord = "";

} else {

currentWord += ch;

// Add the last word if any

if (!currentWord.equals("")) {

words[wordCount++] = currentWord;

// Check if at least 4 words are present

if (wordCount < 4) {

System.out.println("Sentence must contain at least 4 words!");


return;

System.out.println("Original Sentence: " + sentence);

// Sort the words in descending order

for (int i = 0; i < wordCount - 1; i++) {

for (int j = 0; j < wordCount - i - 1; j++) {

if (words[j].compareTo(words[j + 1]) < 0) {

String temp = words[j];

words[j] = words[j + 1];

words[j + 1] = temp;

// Print the sorted words

System.out.print("Words in descending order: ");

for (int i = 0; i < wordCount; i++) {

System.out.print(words[i] + " ");

System.out.println();

// Print the second word

System.out.println("Second word: " + words[1]);

public static void main(String[] args) {

Sentence ob = new Sentence();

ob.get();

ob.convert();

Q8. import java.util.*;

class PatOverload {

private int n;

private char p;

// Method to take input from the user

void get() {

Scanner sc = new Scanner(System.in);


System.out.println("Enter the number of lines (n):");

n = sc.nextInt();

System.out.println("Enter the character (p) for pattern:");

p = sc.next().charAt(0);

// Overloaded method 1: Display pattern with character p

void pat(int n, char p) {

System.out.println("Pattern using character " + p + ":");

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

for (int j = 0; j < n; j++) {

if (j == n - i - 1) {

System.out.print(p);

} else {

System.out.print("#");

System.out.println();

// Overloaded method 2: Display alphabet pattern

void pat() {

char startChar = 'A';

for (int i = 0; i < 5; i++) { // 5 lines as per the example

for (int j = 0; j < 5; j++) {

System.out.print((char) (startChar + j));

startChar++;

System.out.println();

// Overloaded method 3: Display numeric sequence pattern

void pat(int n) {

int[] numbers = {1, 2, 4, 7, 11};

n = numbers.length;

// Upper part of the pattern


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

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

System.out.print(numbers[j] + " ");

System.out.println();

// Lower part of the pattern

for (int i = n - 2; i >= 0; i--) {

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

System.out.print(numbers[j] + " ");

System.out.println();

public static void main(String[] args) {

PatOverload ob = new PatOverload();

ob.get();

ob.pat(ob.n, ob.p); // Display pattern with character p

ob.pat(); // Display alphabet pattern

ob.pat(ob.n); // Display numeric sequence pattern

You might also like