0% found this document useful (0 votes)
30 views45 pages

Pfinal 2

Uploaded by

yash98u6
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)
30 views45 pages

Pfinal 2

Uploaded by

yash98u6
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/ 45

Computer

Project
Term II

Name: Harshangi Gupta


Class: 11 f
ACKNOWLEDGEMENT
I would like to thank my teacher Mr.Siddhartha who gave me this opportunity to
work on this project. I got to learn a lot from this project . I would also like to
thank our school principals.

At last, I would like to extend my heartfelt thanks to my parents because without


their help this project would not have been successful.

THANK YOU
PROGRAM 1
Write a program in JAVA to enter the date in
(dd/mm/yyyy) format and day of the week on which a
project was given. If a college allots 90 days (including
the project giving day) to complete the work, then
calculate the date on which the project is to be
submitted along with the day name. If the final
submission day is SUNDAY then the students get one
extra day and submits the project on the next
day that is MONDAY

import java.util.*;
class Main
{
public static void main(String args[]) throws InputMismatchException
{
Scanner scan = new Scanner(System.in);
int dd, mm, yy, nod, i, tdays = 0, m;
String date, dayName;
System.out.println("Enter the day, month and year of the date of
announcement of the project as (dd/mm/yyyy): ");
date = scan.next();
dd = Integer.parseInt(date.substring(0, 2));
mm = Integer.parseInt(date.substring(3, 5));
yy = Integer.parseInt(date.substring(6));
System.out.println("Enter the no of days given to complete the project: ");
nod = scan.nextInt();
int dom[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (yy % 4 == 0)
dom[1] = 29;
for (i = 0; i < mm - 1; i++)
tdays += dom[i];
tdays += dd;
tdays += nod;
if (yy % 4 == 0)
{
if (tdays > 366)
{
tdays -= 366;
yy++;
}
}
else
{
if (tdays > 365)
{
tdays -= 365;
yy++;
}
}
if (yy % 4 == 0)
dom[1] = 29;
else
dom[1] = 28;
m = 0;
while (tdays > dom[m])
{
tdays -= dom[m];
m++;
}
Calendar c = Calendar.getInstance();
String dayOfWeek = "";
c.set(yy, (m + 1) - 1, tdays);
if (c.get(Calendar.DAY_OF_WEEK) == 1)
{
dayOfWeek = getDayOfWeek(c.get(Calendar.DAY_OF_WEEK) + 1);
System.out.println("Date of submission: " + (tdays + 1) + "/" + (m + 1) + "/" +
yy);
System.out.println("Day of submission: " + dayOfWeek.toUpperCase());
}
else
{
dayOfWeek = getDayOfWeek(c.get(Calendar.DAY_OF_WEEK));
System.out.println("Date of submission: " + tdays + "/" + (m + 1) + "/" + yy);
System.out.println("Day of submission: " + dayOfWeek.toUpperCase());
}

private static String getDayOfWeek(int value) {


String day = "";
switch (value) {
case 1:
day = "Sunday";
break;
case 2:
day = "Monday";
break;
case 3:
day = "Tuesday";
break;
case 4:
day = "Wednesday";
break;
case 5:
day = "Thursday";
break;
case 6:
day = "Friday";
break;
case 7:
day = "Saturday";
break;
}
return day;
}
}
OUTPUT

ALGORITHM
step 1: start
step 2: input date as dd month as mm and year as yy
step 3:take input from the user
step 4:store the input in dd mm yy
step 5:input the deadline from the user
step 6:check if the year is leap year
step 7:check if the month is February
step 8:if it is a leap year then number of days in febuary is 29 else it is 28
step 9:create a switch case to check for the the day for the submission of
project and if it is Sunday then shift to Monday.
step 10:end
PROGRAM 2:
Write a program to declare a square matrix MIII of
order (Nx N) where "N' must be greater than 3 and less
than 10. Allow the user to input positive integers into
this matrix. Perform the following tasks on the matrix:
(a) Sort the boundary elements in ascending order
using any standard sorting technique and rearrange
them in the matrix in clockwise manner.
(b) Calculate the product of the non-boundary
elements.
(c) Display the original matrix, rearranged matrix and
only the non-boundary elements of the rearranged
matrix with their product.

import java.util.Scanner;
public class Main
{
int arr[][], m, product = 1;
static int exeunt()
{
System.out.println("INVALID INPUT");
System.exit(0);
return -1;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Size Of Matrix");
m = ((m = sc.nextInt()) < 4 || m > 10) ? exeunt() : m;
arr = new int[m][m];
fill(sc);
sc.close();
}
void fill(Scanner sc)
{
System.out.println("Enter Positive Numbers : ");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m;)
{
int in = sc.nextInt();
if (in < 0)
System.out.println("Only Positive Numbers");
else
arr[i][j++] = in;
}
}
System.out.println("ORIGINAL MATRIX : ");
display();
}
void sortEdge()
{
int n = (m * 2 + (m - 2) * 2);
int edge[] = new int[n];
for (int i = 0, k = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
if (i == 0 || i == m - 1 || j == 0 || j == m - 1)
edge[k++] = arr[i][j];
}
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (edge[j] > edge[j + 1]) {
int temp = edge[j];
edge[j] = edge[j + 1];
edge[j + 1] = temp;
}
}
}
int k = -1;
for (int j = 0; j < m; j++)
arr[0][j] = edge[++k];
for (int i = 1; i < m; i++)
arr[i][m - 1] = edge[++k];
for (int j = m - 2; j > -1; j--)
arr[m - 1][j] = edge[++k];
for (int i = m - 2; i > 0; i--)
arr[i][0] = edge[++k];
System.out.println("REARRANGED MATRIX");
display();
}
void display()
{
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++)
System.out.print(arr[i][j] + "\t");
System.out.println();
}
}

void displayEnclosing()
{
System.out.println("NON BOUNDRY ELEMENTS");
for (int i = 0; i < m; i++)
{
System.out.print("\t");
for (int j = 0; j < m; j++)
{
if (!(i == 0 || i == m - 1 || j == 0 || j == m - 1))
{
product *= arr[i][j];
System.out.print(arr[i][j] + "\t");
}
}
System.out.println("\t"); // "\t" not required
}
System.out.println("PRODUCT OF NON BOUNDRY ELEMNETS :
“+product);
}
public static void main(String[] args)
{
Main mat = new Main();
mat.accept();
mat.sortEdge();
mat.displayEnclosing();
}

}
OUTPUT:
ALGORITHM
Step 1:start
Step 2:enter the size
Step 3:check if size is more than equal to 4 and less than 10 then print invalid
input
Step 4: input positive numbers and check for positive numbers
Step 5: print the original matrix
Step 6:sort the elements in ascending order
Step 7: calculate the product of non boundary elements
Step 8:print rearranged matrix
Step 9:print the product of non boundary elements
Step 10:end

PROGRAM 3
Write a program to declare a matrix A [][] of order (M x
N) where 'M' is the number of rows and 'N' is the
number of columns such that both M and N must be
greater than 2 and less than 10. Allow the user to input
integers into this matrix. Display appropriate error
message for an invalid input.
Perform the following tasks on the matrix.
(a) Display the input matrix
(b) Create and display the mirror image matrix
(c) Calculate the sum of the four corner elements of the
matrix and display
import java.util.Scanner;
public class Main
{
int arr[][], m, product = 1;
static int exeunt()
{
System.out.println("INVALID INPUT");
System.exit(0);
return -1;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Size Of Matrix");
m = ((m = sc.nextInt()) < 4 || m > 10) ? exeunt() : m;
arr = new int[m][m];
fill(sc);
sc.close();
}
void fill(Scanner sc)
{
System.out.println("Enter Positive Numbers : ");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m;)
{
int in = sc.nextInt();
if (in < 0)
System.out.println("Only Positive Numbers");
else
arr[i][j++] = in;
}
}
System.out.println("ORIGINAL MATRIX : ");
display();
}
void mirrorMatrix()
{
for (int i = 0; i < arr.length; i++)
{
int left = 0, right = arr[i].length - 1;
while (left < right)
{
int temp = arr[i][left];
arr[i][left] = arr[i][right];
arr[i][right] = temp;
left++;
right--;
}
}
System.out.println("MIRROR MATRIX");
display();
}
void display()
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
System.out.print(arr[i][j] + "\t");
System.out.println();
}
}
void displayEnclosing()
{
int sum = 0;
int rows = arr.length;
int columns = arr[0].length;
sum += arr[0][0] + arr[0][columns - 1] + arr[rows - 1][0] + arr[rows -
1][columns - 1];
System.out.println("SUM OF CORNER ELEMENTS : " + sum);
}
public static void main(String[] args)
{
Main mat = new Main();
mat.accept();
mat.mirrorMatrix();
mat.displayEnclosing();
}
}

OUTPUT

ALGORITHM
Step 1: start
Step 2: enter the row and column
Step 3:print invalid input if input for row is less than equal to 1 and for column
is more than equal to 10
Step 4: enter the elements of matrix and store in array
Step 5:print the original matrix
Step 6:run loop (int j=n;j>=0;h++) for mirror matrix
Step 7:print the mirror matrix
Step 8:find the corner sum
Step 9:print the sum of corner elements
Step 10:end

PROGRAM 4
Write a program to accept a sentence which may be
terminated by either.or?" or "! only. Any other
character may be ignored. The words may be separated
by more than one blank space and are in UPPER CASE.
Perform the following tasks:
(a) Accept a sentence and remove all the extra blank
space between two words to a single blank space.
(b) Accept any word from the user along with its
position and insert the word in the given position. (The
position is calculated by place value of each word
where first word is in position 1.second word in
position 2 and so on).
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int i = 0, count = 1, len = 0, pos = 0;
char ch = ' ', last = ' ';
String sen = "", newsen = "", wd = "", wd1 = "";
Scanner sc = new Scanner(System.in);
System.out.print("ENTER A SENTENCE: ");
sen = sc.nextLine();
len = sen.length();
last = sen.charAt(len - 1);
if (last != '.' && last != '?' && last != '!')
{
System.out.println("INVALID INPUT.");
return;
}
sen = sen.toUpperCase();
System.out.print("WORD TO BE INSERTED: ");
wd = sc.next();
wd = wd.toUpperCase();
System.out.print("WORD POSITION IN THE SENTENCE: ");
pos = sc.nextInt();
for (i = 0; i < len; i++)
{
ch = sen.charAt(i);
if (ch == ' ' || ch == '.' || ch == '?' || ch == '!')
{
if ((count) == pos)
{
newsen = newsen + wd +" "+ wd1 + ch;
}
else
{
newsen = newsen + wd1 + ch;
}
wd1 = "";
count++;
}
else
{
wd1 = wd1 + ch;
}
}
System.out.println("NEW SENTENCE:" + newsen);
}
}
OUTPUT

ALGORITHM
Step 1:start
Step 2:initialise variable i,count,Len and pos in integer ch and last in character
and sen,newsen,wd and wd1 in string
Step 3: enter sentence and store in sen
Step 4: store the length in Len
Step 5: check if the last character of sentence is .,?,! Print invalid input
Step 6: input word to be inserted and it's position
Step 7:check for the position and insert the word in the sentence
Step 8:print the sentence
Step 9:end
PROGRAM 5:
A Composite Magic number is a positive integer which
is composite as well as a magic number.
Composite number: A composite number is a number
that has more than two factors. Example: 10
Factors are: 1, 2, 5, 10
Magic number:
A magic number is a number in which the eventual sum
of the digits is equal to 1. Example: 28 = 2+8=10=1+0=1
Accept two positive integers m and n, where m is less
than n as user input. Display the number of Composite
Magic integers that are in the range between m and n
(both inclusive) and output them along with the
frequency, in the format specified below:

import java.io.*;
class Main
{
boolean isComposite(int n) // Function to check for Composite number
{
int count = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0)
count++;
}
if (count > 2)
return true;
else
return false;
}
int sumDig(int n) // Function to return sum of digits of a number
{
int s = 0;
while (n > 0)
{
s = s + n % 10;
n = n / 10;
}
return s;
}

boolean isMagic(int n) // Function to check for Magic number


{
int a = sumDig(n);
while (a > 9) {
a = sumDig(a);
}

if (a == 1)
return true;
else
return false;
}
public static void main(String args[]) throws IOException
{
Main mc = new Main();
Buffered reader br= new Buffered reader(new Input Steam
Reader(System.in));
System.out.print("Enter the lower limit(m) : ");
int m = Integer.parseInt(br.readLine());
System.out.print("Enter the upper limit(n) : ");
int n = Integer.parseInt(br.readLine());
int c = 0;
if (m < n)
{
System.out.println("The Composite Magic Integers are: ");
for (int i = m; i <= n; i++) {
if (mc.isComposite(i) == true && mc.isMagic(i) == true) {
if (c == 0)
System.out.print(i);
else
System.out.print(", " + i);
c++;
}
}
System.out.println("\nThe frequency of Composite Magic Integers is : " + c);
} else
System.out.println("INVALID RANGE");
}
}
OUTPUT

ALGORITHM
Step 1: start
Step 2:enter the lower and upper limit from the user
Step 3: initialise the values in m and n
Step 4:print invalid input if m is more than n
Step 5:enter the composite magic integer
Step 6:check if the number is magic or a Composite number
Step 7: find the sum
Step 8: if the number is equal to the sum the sum =0
Step 9:if number is equal to 1 then true else false
Step 10:if the number is composite then frequency increases
Step 11: print the frequency
Step 12 :end

PROGRAM 6:
Write a program to accept a paragraph containing two
sentences only. The sentences may be terminated by
either.,?" or "! only. Any other character may be
ignored. The words are to be separated by a single
blank space and are in UPPER CASE.
Perform the following tasks:
(a)Accept the paragraph and check for validity
(b) Obtain the length/size of the two sentences
separately (number of words)
(c) Find the common words which occur in both the
sentences
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner Sc = new Scanner(System.in);
System.out.print("Enter a paragraph with only 2 sentences : ");
String P = Sc.nextLine();
P = P.toUpperCase();
StringTokenizer St = new StringTokenizer(P, ".?!");
int c = St.countTokens();
if (c == 2)
{
String S[] = new String[c];
for (int i = 0; i < c; i++)
{
S[i] = St.nextToken();
}
S[0] = S[0].trim();
S[1] = S[1].trim();
String[] A = S[0].split(" ");
System.out.println("Number of words in sentence 1 : " + A.length);
String[] B = S[1].split(" ");
System.out.println("Number of words in sentence 2 : " + B.length);
for (int i = 0; i < A.length; i++)
{
int count = 0;
String W = A[i];
for (int j = 0; j < B.length; j++)
{
if (W.equals(B[j]) == true && B[j].equals("") != true)
{
count++;
}
}
if (count != 0)
{
for (int k = 0; k < A.length; k++)
{
if (W.equals(A[k]) && A[k].equals("") != true)
{
count++;
A[k] = "";
}
}
System.out.println(W + "\t" + count);
}
}
}
else
{
System.out.print("The paragraph does not contain exactly two sentences.");
}
}
}

OUTPUT

ALGORITHM
Step 1:start
Step 2: enter a paragraph from the user containing 2 sentences only and store
in p
Step 3:check if number of token ==2
Step 4: count the number of words in sentence 1
Step 5:count the number of words in sentence 2
Step 6:check if words in sentence 1 equals to words in sentence 2
Step 7:if words are equal then increase count
Step 8: print the number of words in sentence 1 and 2
Step 9:print the common words in sentence 1 and 2
Step 10:end

PROGRAM 7:
An integer number whose digits are arranged in
ascending order from left to right is called an Increasing
Number. At any position, the current digit is always
greater than or equal to the previous digit. For
example. 1233, 13689, 112334566, etc.
An integer number whose digits are arranged in
descending order from left to right is called a
Decreasing Number. Here the current digit is always
less than or equal to the previous digit. For example,
321, 88531.8755321, etc.
A positive integer where the digits are neither in
increasing nor decreasing order is called a Bouncy
number. In other words, if the digits of the number are
unsorted then it is bouncy. For example.123742, 101,
43682, etc.

import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int n;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
n = in.nextInt();
if (n < 100)
{
System.out.println(n + " is not a Bouncy Number.");
return;
}
int t = n;
boolean isIncreasing = true, isDecreasing = true;
int prev = t % 10;
while (t != 0)
{
int d = t % 10;
if (d > prev)
{
isIncreasing = false;
break;
}
prev = d;
t = t/10;
}
t = n;
prev = t % 10;
while (t != 0)
{
int d = t % 10;
if (d < prev)
{
isDecreasing = false;
break;
}
prev = d;
t = t/10;
}
if (isIncreasing)
{
System.out.println(n + " is a Increasing number.");
}

else if (isDecreasing){
System.out.println(n + " is a Decreasing number.");
}
else{
System.out.println(n + " is a bouncy number.");
}
} }

OUTPUT
Decreasing -

Increasing -

Bouncy -

ALGORITHM
step 1: start
step 2: take input from the user
step 3: check if the character if digit
step 4: check if the number is in decreasing order then the number is
decreasing number
step 5: check if the number is in increasing order then the number is increasing
number
step 6:if the number is neither increasing or decreasing then the number is
bouncy
step 7: end

PROGRAM 8:
Write a program to declare a square matrix M[][] of
order 'N' where 'N' must be greater than 3 and less
than 10. Accept the value of N as user input. Display an
appropriate message for an invalid input. Allow the
user to accept three different characters from the
keyboard and fill the matrix according to the
instruction given below:
(i) Fill the four corners of the square matrix by
character 1
(ii) Fill the boundary elements of the matrix (except the
four corners) by character 2
(iii) Fill the non-boundary elements of the matrix by
character 3
(iv) Display the matrix formed by the above instructions

import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter no.of rows M of a square matrix:");
int m = sc.nextInt();
char mat[][] = new char[m][m];
System.out.println("Enter corner elements - 1st character: ");
char ch1 = sc.next().charAt(0);
System.out.println("Enter boundary except corner elements - 2nd character:
");
char ch2 = sc.next().charAt(0);
System.out.println("Enter non boundary - 3rd character: ");
char ch3 = sc.next().charAt(0);
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if ((i == 0 || i == m - 1) && (j == 0 || j == m - 1))
mat[i][j] = ch1;
else if (i == 0 || j == 0 || i == m - 1 || j == m - 1)
mat[i][j] = ch2;
else
mat[i][j] = ch3;
}
}
System.out.println("");
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
System.out.print(mat[i][j] + "\t");
}
System.out.println();
}}}

OUTPUT
ALGORITHM
step 1:start
step 2:input the size of matrix
step 3:check if the size is more than 3 or less than 10 and print invalid input
step 4:take input for the first second and third character
step 5:start loops for the row and column
step 6:fill the first character if ((i==0 || i==m-1) && (j==0 || j==m-1))
step 7:fill the second character if (i==0 || i==m-1 || j==0 || j==m-1)
step 8:fill third character in on boundary elements
step 9:print the new matrix
step 10:end
PROGRAM 9:
An encoded text can be decoded by finding actual
character for the given ASCII code in the encoded
message. Write a program to input an encoded text
having only sequence of ASCII values
without any spaces. Any code or value which is not in
the range 65-90 (A-Z) or 97-122 (a-z) or 32 (for space)
will be ignored and should not appear in the output
message.

import java.util.*;
class Num
{
public static void main (String args [ ] )
{
Scanner sc = newScanner(System.in);
System.out.println("Enter no.);
String s = sc.next();
for( int i=0 ; i<s.length() ; i++ )
{
char ch = s.CharAt(i);
if (Character.isDigit(ch) == false)
{
System.out.print ("Invalid input");
System.exit(0);
}
}
int num = Integer.parseInt(s);
int inc=true ; dec=true ;
int t=num ; prev=t%10 ;
while ( t!=0 )
{
int d=t%10 ;
if (d<prev)
dec = false ;
break ;
}
prev = d ;
t = t / 10 ;
while (t!=0)
{
int d = t%10 ;
if ( d>prev )
{
inc = false
break ;
}
prev = d ;
t = t//10 ;
}
if (inc)
System.out.println("Number in increasing order");
else if (dec)
System.out.println("Number in decreasing order");
else
System.out.println("Bouncy Number");
}
PROGRAM 10
class TitleCase
str - to store the sentence
newstr - to store the sentence in title case
TitleCase() - default constructor
void accept() - to accept a sentence
void toTitleCase() - convert sentence into title case
void display - to display title case sentence
Define main()

import java.util.*;
class TitleCase
{
String str,newstr;
TitleCase()
{
str="";
newstr="";
}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Sentence");
str=sc.nextLine();
}
void toTitleCase()
{
StringTokenizer st = new StringTokenizer(str);
int m=st.countTokens();
for (int i=1;i<=m;i++)
{
String w=st.nextToken();
newstr=newstr+Character.toUpperCase(w.charAt(0))+w.substring(1).toLowerCase()+"
";
}
}
void display()
{
System.out.println("Original Sentence: "+str);
System.out.println("Changed Sentence: "+newstr);
}
public static void main(String arg[])
{
TitleCase ob= new TitleCase();
ob.accept();
ob.toTitleCase();
ob.display();
}
}
ALGORITHM
STEP 1 – START
STEP 2 – Declare variable string and newstring
STEP 3 – Create a default constructor
STEP 4 – Create a method accept to input the sentence
STEP 5 – Create a method toTitleCase to convert the sentence to title case sentence
STEP 6 – Create a method display to display the original sentence and title case sentence
STEP 7 – Create an object of the class to call the functions toTitileCase , accept and display
STEP 8 – END

OUTPUT
THANK
YOU

You might also like