0% found this document useful (0 votes)
5 views34 pages

Computer Project Term 2

Uploaded by

tinafoxxfil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views34 pages

Computer Project Term 2

Uploaded by

tinafoxxfil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Angeera Mondal

1
Serial Program Name Page
No. no.

1. If arrays M and M + N are as shown below, write a program in Java to find the array 3
N.
M = {{-1, 0, 2}, M + N = {{-6, 9, 4},
{-3, -1, 6}, {4, 5, 0},
{4, 3, -1}} {1, -2, -3}}

2. A Class Teacher wants to keep the records of 40 students of her class along with 5
their names and marks obtained in English, Hindi, Maths, Science and Computer
Science in a Double Dimensional Array (DDA) as M[40][5].
When the teacher enters the name of a student as an input, the program must
display the name, marks obtained in the 5 subjects and the total. Write a program
in Java to perform the task.
[done taking input as x in place of 40]

3. A double dimensional array is defined as N[4][4] to store numbers. Write a 8


program to find the sum of all even numbers and product of all odd numbers of the
elements stored in Double Dimensional Array (DDA).

4. The annual examination result of 50 students in a class is tabulated in a Single 10


Dimensional Array (SDA) is as follows:
Roll No. Subject A Subject B Subject C
....... ....... ....... .......
....... ....... ....... .......
....... ....... ....... .......
Write a program to read the data, calculate and display the following:
(a) Average marks obtained by each student.
(b) Print the roll number and the average marks of the students whose average is
above 80.
(c) Print the roll number and the average marks of the students whose average is
below 40.
[input taken as n for number of students instead of 50]

5. Write a program in Java to store 10 numbers (including positive and negative 12


numbers) in a Single Dimensional Array (SDA). Display all the negative numbers
followed by the positive numbers without changing the order of the numbers.

6. Write a class program with the following specifications: 14


Class name — Matrix
Data members — int array m[][] with m x m
Member functions:
1. void getdata() — to accept the numbers in the array
2. void rowsum() — to find and print the sum of the numbers of each row
3. void colsum() — to find and print the sum of numbers of each column
Use a main function to create an object and call member methods of the class

7. Write a program to accept numbers in mxm matrix (dda) and perform the following: 16
i. display the original matrix
ii. transpose the given matrix
iii. display the resultant matrix generated after transposing.

8. Write a program to input 15 integer elements in an array and sort them 19


in ascending order using the bubble sort technique

9. Write a Java program to store n numbers in an one dimensional array. 21

2
Pass this array to a function number(int a[]). Display only those
numbers whose sum of digit is prime.

10. Define a class to overload the method perform as follows: 23


double perform (double r, double h) — to calculate and return the value
of curved surface area of cone
CSA=πrl l=r2+h2
void perform (int r, int c) — Use NESTED FOR LOOP to generate the
following format
r = 4, c = 5
output
12345
5432
123
54
1
void perform (int m, int n, char ch) — to print the quotient of the
division of m and n if ch is Q else print the remainder of the division of
m and n if ch is R

11. DTDC a courier company charges for the courier based on the weight of 26
the parcel. Define a class with the following specifications:
Class name: courier
Member variables:
name – name of the customer
weight – weight of the parcel in kilograms
address – address of the recipient
bill – amount to be paid
type – 'D'- domestic, 'I'- international
Member methods:
void accept ( ) — to accept the details using the methods of the Scanner
class only.
void calculate ( ) — to calculate the bill as per the following criteria:

Weight in Kgs Rate per Kg


First 5 Kgs Rs.800
Next 5 Kgs Rs.700
Above 10 Kgs Rs.500

An additional amount of Rs.1500 is charged if the type of the courier is I


(International)
void print ( ) — To print the details
void main ( ) — to create an object of the class and invoke the methods

12. Define a class pin code and store the given pin codes in a single 28
dimensional array. Sort these pin codes in ascending order using the
Selection Sort technique only. Display the sorted array.
110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033

13. Write a program to input a sentence and arrange words of the string in order of 29
their lengths from shortest to longest using array.

14. Write a program to input a string and print each word of the string in the 31
reverse order.

15. Write a program to enter a sentence from the keyboard and count the number of 32
times a particular word occurs in it. Display the frequency of the search word.

Question 1:

3
Algorithm:

Step 1: Start.
Step 2:Declare matrix M = {{-1, 0, 2}, {-3, -1, 6}, {4, 3, -1}}.
Step 3:Declare matrix MN = {{-6, 9, 4}, {4, 5, 0}, {1, -2, -3}}.
Step 4:Declare empty matrix N[3][3].
Step 5:For i = 0 to 2.
Step 6:For j = 0 to 2.
Step 7:N[i][j] = MN[i][j] - M[i][j].
Step 8:End for.
Step 9:End for.
Step 10:Print "Matrix N".
Step 11:For i = 0 to 2.
Step 12:For j = 0 to 2.
Step 13:Print N[i][j].
Step 14:End for.
Step 15:Print new line.
Step 16:End for.
Step 17:Stop.

source code:

class P1 {
public static void main(String[] args) {
// matrix M
int[][] M = {
{-1, 0, 2},
{-3, -1, 6},
{4, 3, -1}
};

// matrix M + N
int[][] MN = {
{-6, 9, 4},
{4, 5, 0},
{1, -2, -3}
};

// create matrix N with same size


int[][] N = new int[3][3];

// subtract M from (M+N) to get N


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

4
for (int j = 0; j < 3; j++) { // loop cols
N[i][j] = MN[i][j] - M[i][j]; // formula
}
}

// print matrix N
System.out.println("Matrix N:");
for (int i = 0; i < 3; i++) { // loop rows
for (int j = 0; j < 3; j++) { // loop cols
System.out.print(N[i][j] + " "); // print element
}
System.out.println(); // new line
}
}
}

Output:

VDT:

5
Question 2:

Algorithm:

Step 1: Start.
Step 2:Create arrays: n[40] for names, m[40][5] for marks.
Step 3:For i = 0 to 39, input n[i] and m[i][0] to m[i][4].
Step 4:Input search name s.
Step 5:Set f = false.
Step 6:For i = 0 to 39, if n[i] equals s (ignore case), set f = true.
Step 7:Print name n[i].
Step 8:Print marks m[i][0] to m[i][4].
Step 9:Calculate total t = m[i][0] + m[i][1] + m[i][2] + m[i][3] + m[i][4].
Step 10:Print total t.
Step 11:Break loop.
Step 12:If f is false, print "Not found".
Step 13:Stop.

Source code:

import java.util.Scanner;

public class P2 {
public static void main(String[] args) {
// Create scanner for input
Scanner sc = new Scanner(System.in);

// Arrays for names and marks


String[] n = new String[40];
int[][] m = new int[40][5];
String[] sub = {"English", "Hindi", "Maths", "Science", "CS"};

// Input data for 40 students


for (int i = 0; i < 40; i++) {
System.out.print("Enter name for student " + (i+1) + ": ");
n[i] = sc.nextLine();

// Input marks for 5 subjects


for (int j = 0; j < 5; j++) {
System.out.print("Enter marks for " + sub[j] + ": ");
m[i][j] = sc.nextInt();
}
sc.nextLine(); // Clear buffer

6
}

// Get search name from user


System.out.print("Enter name to search: ");
String s = sc.nextLine();
boolean f = false;

// Search for student in records


for (int i = 0; i < 40; i++) {
if (n[i].equalsIgnoreCase(s)) {
f = true;
System.out.println("Name: " + n[i]);
int t = 0;

// Display marks and calculate total


for (int j = 0; j < 5; j++) {
System.out.println(sub[j] + ": " + m[i][j]);
t += m[i][j];
}
System.out.println("Total: " + t);
break;
}
}

// Display if student not found


if (!f) {
System.out.println("Student not found.");
}
}
}

Input and Output:

7
VDT:

Question 3:

Algorithm:

Step 1: Start.
Step 2:Declare a 4x4 array N to store numbers.
Step 3:Initialize sum = 0 for even numbers and prod = 1 for odd numbers.
Step 4:Use nested loops to traverse the array (i=0 to 3, j=0 to 3).

8
Step 5:For each element N[i][j], check if it is even or odd.
Step 6:If even, add it to sum.
Step 7:If odd, multiply it with prod.
Step 8:After processing all elements, print sum and prod.
Step 9:Stop.

Source code:

import java.util.Scanner;

public class P3 {
public static void main(String[] args) {
// Create scanner for input
Scanner sc = new Scanner(System.in);
int[][] N = new int[4][4];

// Input 4x4 matrix


System.out.println("Enter 4x4 matrix elements:");
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
N[i][j] = sc.nextInt();
}
}

int s = 0; // Sum of even numbers


int p = 1; // Product of odd numbers

// Process each element in matrix


for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
// Check if number is even
if(N[i][j] % 2 == 0) {
s += N[i][j]; // Add to sum
} else {
p *= N[i][j]; // Multiply to product
}
}
}

// Display results
System.out.println("Sum of all even numbers: " + s);
System.out.println("Product of all odd numbers: " + p);
}
}

9
Input and Output

VDT:

10
Question 4:

Algorithm:

Step 1: Start.
Step 2:Create arrays: r[50] for roll numbers, m[50][3] for marks in 3 subjects.
Step 3:For i = 0 to 49, input r[i] and m[i][0], m[i][1], m[i][2].
Step 4:For each student i, calculate average a = (m[i][0] + m[i][1] + m[i][2]) / 3.
Step 5:Print roll number r[i] and average a for each student.
Step 6:Print students with average > 80.
Step 7:Print students with average < 40.
Step 8:Stop.

Source Code:

import java.util.Scanner;

public class P4 {
public static void main(String[] args) {
// Create scanner for input
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n");
int n=sc.nextInt();
int[] r = new int[n]; // Roll numbers
int[][] m = new int[n][3]; // Marks for 3 subjects
double[] a = new double[n]; // Average marks

// Input data for n students


System.out.println("Enter data for "+n+" students:");
for(int i=0; i<n; i++) {
System.out.print("Enter Roll No: ");
r[i] = sc.nextInt();
System.out.print("Enter marks for 3 subjects: ");
for(int j=0; j<3; j++) {
m[i][j] = sc.nextInt();
}
}

// Calculate average for each student


for(int i=0; i<n; i++) {
a[i] = (m[i][0] + m[i][1] + m[i][2]) / 3.0;
}

// Print all averages

11
System.out.println("\nAverage marks of each student:");
for(int i=0; i<n; i++) {
System.out.println("Roll No: " + r[i] + " Average: " + a[i]);
}

// Students with average above 80


System.out.println("\nStudents with average above 80:");
for(int i=0; i<n; i++) {
if(a[i] > 80) {
System.out.println("Roll No: " + r[i] + " Average: " + a[i]);
}
}

// Students with average below 40


System.out.println("\nStudents with average below 40:");
for(int i=0; i<n; i++) {
if(a[i] < 40) {
System.out.println("Roll No: " + r[i] + " Average: " + a[i]);
}
}
}
}

Input and Output:

12
VDT:

Question 5:

Algorithm:

Step 1: Start.
Step 2:Create an array n[10] to store numbers.
Step 3:Input 10 numbers into the array.
Step 4:First, print all negative numbers in order.
Step 5:Then, print all positive numbers in order.

13
Step 6:Stop.

Source Code:

import java.util.Scanner;

public class P5 {
public static void main(String[] args) {
// Create scanner for input
Scanner sc = new Scanner(System.in);
int[] n = new int[10];

// Input 10 numbers
System.out.println("Enter 10 numbers:");
for(int i=0; i<10; i++) {
n[i] = sc.nextInt();
}

// First pass: print negative numbers


System.out.print("Output: ");
for(int i=0; i<10; i++) {
if(n[i] < 0) {
System.out.print(n[i] + " ");
}
}

// Second pass: print positive numbers


for(int i=0; i<10; i++) {
if(n[i] >= 0) {
System.out.print(n[i] + " ");
}
}
}
}

Input and output

14
VDT:

Question 6:

Algorithm:
Step 1:Start.
Step 2:Create a class Matrix with a 2D array m[][].
Step 3:Define getdata() method to input matrix elements.
Step 4:Define rowsum() method to calculate and print sum of each row.
Step 5:Define column() method to calculate and print sum of each column.
Step 6:In main(), create object and call methods.
Step 7:Stop.

Source Code:

15
import java.util.Scanner;

class P6 {
int[][] m;
int size;

// Method to input matrix data


void getdata() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter matrix size: ");
size = sc.nextInt();
m = new int[size][size];

System.out.println("Enter matrix elements:");


for(int i=0; i<size; i++) {
for(int j=0; j<size; j++) {
m[i][j] = sc.nextInt();
}
}
}

// Method to calculate row sums


void rowsum() {
System.out.println("Row sums:");
for(int i=0; i<size; i++) {
int sum = 0;
for(int j=0; j<size; j++) {
sum += m[i][j];
}
System.out.println("Row " + (i+1) + ": " + sum);
}
}

// Method to calculate column sums


void column() {
System.out.println("Column sums:");
for(int j=0; j<size; j++) {
int sum = 0;
for(int i=0; i<size; i++) {
sum += m[i][j];
}
System.out.println("Column " + (j+1) + ": " + sum);
}
}

16
public static void main(String[] args) {
Matrix obj = new Matrix();
obj.getdata();
obj.rowsum();
obj.column();
}
}

Input and Output:

VDT:

Question 7:

Algorithm:
Step 1:Start.
Step 2:Input m x m matrix.
Step 3:Display original matrix.

17
Step 4:Create transpose by swapping rows and columns.
Step 5:Display transposed matrix.
Step 6:Stop.

Source Code:

import java.util.Scanner;

public class P7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter matrix size: ");
int m = sc.nextInt();
int[][] arr = new int[m][m];

// Input matrix elements


System.out.println("Enter matrix elements:");
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
arr[i][j] = sc.nextInt();
}
}

// Display original matrix


System.out.println("Original Matrix:");
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}

// Transpose the matrix


int[][] trans = new int[m][m];
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
trans[j][i] = arr[i][j];
}
}

// Display transposed matrix


System.out.println("Transposed Matrix:");
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {

18
System.out.print(trans[i][j] + " ");
}
System.out.println();
}
}
}

Input and Output:

VDT:

19
Question 8:

Algorithm:
Step 1:Start.
Step 2:Create array of 15 integers.
Step 3:Input 15 numbers.
Step 4:Use bubble sort to arrange in ascending order.
Step 5:Display sorted array.
Step 6:Stop.

Source Code:

import java.util.Scanner;

public class P8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[15];

// Input 15 numbers
System.out.println("Enter 15 numbers:");
for(int i=0; i<15; i++) {
arr[i] = sc.nextInt();
}

// Bubble sort algorithm


for(int i=0; i<14; i++) {
for(int j=0; j<14-i; j++) {
if(arr[j] > arr[j+1]) {

20
// Swap elements
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

// Display sorted array


System.out.println("Sorted array:");
for(int i=0; i<15; i++) {
System.out.print(arr[i] + " ");
}
}
}

Input and Output:

VDT:

21
Question 9:

Algorithm:
Step 1:Start.
Step 2:Input n numbers in array.
Step 3:Pass array to function number().
Step 4:For each number, calculate sum of digits.
Step 5:Check if sum is prime number.
Step 6:Display numbers with prime digit sum.
Step 7:Stop.

Source Code:

import java.util.Scanner;

public class P9 {
// Function to check if number is prime
static boolean isPrime(int n) {
if(n < 2) return false;
for(int i=2; i<=n/2; i++) {
if(n % i == 0) return false;
}
return true;
}

22
// Function to calculate sum of digits
static int digitSum(int num) {
int sum = 0;
while(num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}

// Function to display numbers with prime digit sum


static void number(int a[]) {
for(int i=0; i<a.length; i++) {
int sum = digitSum(a[i]);
if(isPrime(sum)) {
System.out.print(a[i] + " ");
}
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter how many numbers: ");
int n = sc.nextInt();
int[] arr = new int[n];

// Input numbers
System.out.println("Enter " + n + " numbers:");
for(int i=0; i<n; i++) {
arr[i] = sc.nextInt();
}

// Call function
System.out.println("Numbers with prime digit sum:");
number(arr);
}
}

Input and Output:

23
VDT:

Question 10:

Algorithm:
Step 1:Start.
Step 2:Create class with overloaded perform() methods.
Step 3:First method calculates cone surface area.
Step 4:Second method generates number pattern.
Step 5:Third method calculates quotient or remainder.
Step 6:Test all methods in main().

24
Step 7:Stop.

Source Code:

import java.util.Scanner;

class P10 {
// Method 1: Calculate curved surface area of a cone
double perform(double r, double h) {
double l = Math.sqrt(r * r + h * h);
double csa = 3.14 * r * l;
return csa;
}

// Method 2: Generate the required number pattern


void perform(int r, int c) {
for (int i = 1; i <= r; i++) {
if (i % 2 != 0) {
for (int j = 1; j <= c; j++) {
System.out.print(j + " ");
}
} else {
for (int j = c; j >= 1; j--) {
System.out.print(j + " ");
}
}
c--;
System.out.println();
}
}

// Method 3: Print quotient or remainder


void perform(int m, int n, char ch) {
if (ch == 'Q')
System.out.println("Quotient: " + (m / n));
else if (ch == 'R')
System.out.println("Remainder: " + (m % n));
else
System.out.println("Invalid choice");
}

// Main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

25
P10 obj = new P10();

// Testing first method


System.out.println("Curved Surface Area of Cone: " + obj.perform(5.0, 12.0));

// Testing second method (pattern)


obj.perform(4, 5);

// Testing third method


obj.perform(15, 4, 'Q');
obj.perform(15, 4, 'R');
}
}

Input and Output:

VDT:

Question 11:

26
Algorithm:
Step 1:Start.
Step 2:Create class Courier with member variables.
Step 3:Define accept() method to input details.
Step 4:Define calculate() method to compute bill.
Step 5:Define print() method to display details.
Step 6:Test in main() method.
Step 7:Stop.

Source Code:

import java.util.Scanner;

class P11 {
String name, address;
double weight, bill;
char type;

// Method to accept details


void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter customer name: ");
name = sc.nextLine();
System.out.print("Enter weight (kg): ");
weight = sc.nextDouble();
sc.nextLine();
System.out.print("Enter address: ");
address = sc.nextLine();
System.out.print("Enter type (D/T): ");
type = sc.next().charAt(0);
}

// Method to calculate bill


void calculate() {
if(weight <= 5) {
bill = weight * 800;
} else if(weight <= 10) {
bill = 5*800 + (weight-5)*700;
} else {
bill = 5*800 + 5*700 + (weight-10)*500;
}

if(type == 'T' || type == 'I') {

27
bill += 1500;
}
}

// Method to print details


void print() {
System.out.println("\nCustomer Name: " + name);
System.out.println("Weight: " + weight + " kg");
System.out.println("Address: " + address);
System.out.println("Type: " + (type=='D'?"Domestic":"International"));
System.out.println("Total Bill: Rs." + bill);
}

public static void main(String[] args) {


Courier obj = new Courier();
obj.accept();
obj.calculate();
obj.print();
}
}
Input and Output:

VDT:

28
Question 12:

Algorithm:
Step 1:Start.
Step 2:Store pin codes in array.
Step 3:Use selection sort to arrange in ascending order.
Step 4:Display sorted pin codes.
Step 5:Stop.

Source Code:
public class P12 {
public static void main(String[] args) {
int[] pins = {110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033};

// Selection sort algorithm


for(int i=0; i<pins.length-1; i++) {
int minIndex = i;
for(int j=i+1; j<pins.length; j++) {
if(pins[j] < pins[minIndex]) {
minIndex = j;
}
}
// Swap elements
int temp = pins[minIndex];
pins[minIndex] = pins[i];
pins[i] = temp;
}

29
// Display sorted pins
System.out.println("Sorted pin codes:");
for(int pin : pins) {
System.out.print(pin + " ");
}
}
}

Output:

VDT:

Question 13:

Algorithm:
Step 1:Start.
Step 2:Input a sentence.
Step 3:Split into words array.
Step 4:Sort words by length using bubble sort.
Step 5:Display words in length order.
Step 6:Stop.

Question 13

Algorithm:
Step 1: Start.
Step 2: Input a sentence from the user.
Step 3: Split the sentence into words and store them in an array.
Step 4: Use bubble sort to arrange the words in ascending order of their lengths.
Step 5: Display the sorted words.
Step 6: Stop.

Source Code:

import java.util.Scanner;

public class P13 {

30
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine();

// Split sentence into words


String[] words = sentence.split(" ");

// Sort words by length using bubble sort


for(int i=0; i<words.length-1; i++) {
for(int j=0; j<words.length-1-i; j++) {
if(words[j].length() > words[j+1].length()) {
String temp = words[j];
words[j] = words[j+1];
words[j+1] = temp;
}
}
}

// Display sorted words


System.out.println("Words sorted by length:");
for(String word : words) {
System.out.print(word + " ");
}
}
}

Input and Output:

VDT:

31
Question 14:

Algorithm:
Step 1:Start.
Step 2:Input a string.
Step 3:Split into words.
Step 4:Reverse each word.
Step 5:Display reversed words.
Step 6:Stop.

Source Code:

import java.util.Scanner;

public class P14 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = sc.nextLine();

// Split into words


String[] words = input.split(" ");

// Reverse each word


for(int i=0; i<words.length; i++) {
String reversed = new StringBuilder(words[i]).reverse().toString();
System.out.print(reversed + " ");
}

32
}
}

Input and Output:

VDT:

Question 15:

Algorithm:
Step 1:Start.
Step 2:Input sentence and search word.
Step 3:Convert to lowercase for case-insensitive search.
Step 4:Count occurrences of search word.
Step 5:Display frequency count.
Step 6:Stop.

Source Code:

import java.util.Scanner;

public class P15 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine();
System.out.print("Enter word to search: ");

33
String searchWord = sc.next();

// Convert to lowercase for case-insensitive search


String lowerSentence = sentence.toLowerCase();
String lowerSearch = searchWord.toLowerCase();

// Split sentence into words


String[] words = lowerSentence.split(" ");

// Count occurrences
int count = 0;
for(String word : words) {
if(word.equals(lowerSearch)) {
count++;
}
}

System.out.println("Search word occurs " + count + " times");


}
}

Input and Output:

VDT:

34

You might also like