Computer Project Term 2
Computer Project Term 2
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]
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.
2
Pass this array to a function number(int a[]). Display only those
numbers whose sum of digit is prime.
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:
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}
};
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);
6
}
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];
// 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
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]);
}
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();
}
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;
16
public static void main(String[] args) {
Matrix obj = new Matrix();
obj.getdata();
obj.rowsum();
obj.column();
}
}
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];
18
System.out.print(trans[i][j] + " ");
}
System.out.println();
}
}
}
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();
}
20
// Swap elements
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
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;
}
// 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);
}
}
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;
}
// Main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
25
P10 obj = new P10();
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;
27
bill += 1500;
}
}
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};
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;
30
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine();
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;
32
}
}
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;
33
String searchWord = sc.next();
// Count occurrences
int count = 0;
for(String word : words) {
if(word.equals(lowerSearch)) {
count++;
}
}
VDT:
34