Você está na página 1de 12

Home | Contact

aashray | Logout

Home
Resources
Class X
Class XII
Videos
Gallery
Forum
Fun

Friends (0)
Friend Requests
You have no new friend request.
Tutorials

Programming In Java

English

Computer Practical

Viva-Voce
Quick Links

For-loop

While-loop

Do-while loop

Arrays 1D

Arrays 2D

Strings

Functions

Function overloading

Recursive Functions

ISC Computer Practical 2009 Solved


1. Design a program to accept a day number (between 1 and 366), year (in 4
digits) from the user to generate and display the corresponding date. Also
accept 'N' (1<=N<=100) from the user to compute and display the future date

corresponding to 'N' days after the generated date. Display error message if the
value of the day number, year and N are not within the limit or not according to
the condition specified. Test your program for the following data and some
random data.
Example:
INPUT : DAY NUMBER : 233 YEAR : 2008 DATE AFTER(N) : 17
OUTPUT: 20TH AUGUST 2008 DATE AFTER 17 DAYS : 6TH SEPTEMBER 2008
INPUT : DAY NUMBER : 360 YEAR : 2008 DATE AFTER(N) : 45
OUTPUT: 25TH DECEMBER 2008 DATE AFTER 45 DAYS : 8TH FEBRUARY 2009

View/Hide Solution

import java.util.*;
class ISCprac2009q01{
public static void main(String args[])
throws InputMismatchException{

Scanner scan=new Scanner(System.in);


System.out.println("ENTER DAY NUMER(>=1 AND
<=366) : ");
int day_number=scan.nextInt();
System.out.println("ENTER YEAR(4 DIGIT) : ");
int year=scan.nextInt();
System.out.println("ENTER DATE AFTER(N)(>=1 AND
<=100) : ");
int n=scan.nextInt();

if(day_number<1 || day_number>366)
System.out.println("INVALID DAY NUMBER.");
else if(year<1000 || year >9999)
System.out.println("INVALID YEAR");

else if(n<1 || n>100)


System.out.println("INVALID DATE AFTER VALUE.");
else{
//INITIALIZE MONTH NAMES AND NUMBER OF DAYS IN EACH MONTH
String month_names[]={"JANUARY", "FEBRUARY","MARCH",
"APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER",
"OCTOBER","NOVERMBER","DECEMBER"};
int month_days[]={31,28,31,30,31,30,31,31,30,31,30,31};
int i, day, month,day_after;
String suffix;

//IF IT IS A LEAP YEAR FEBRURAY SHOULD HAVE 29 DAYS


if(year%400==0 || (year%100!=0 && year%4==0))
month_days[1]=29;

i=0;
//FIND THE DATE CORRESPONDING TO THE DAY NUMBER
day=day_number;
while(day>month_days[i])
{
day-=month_days[i];
i++;
}
month=i;
//ADD SUFFIX AS PER THE DAY
if(day%10==1 && day/10!=1)
suffix="ST";
else if(day%10==2 && day/10!=1)
suffix="ND";

else if(day%10==3 && day/10!=1)


suffix="RD";
else
suffix="TH";
System.out.println("OUTPUT:");
//FIRST PART OF THE OUTPUT
System.out.println(day+suffix+" "+
month_names[month]+" "+year);

//TO CALCULATE DATE AFTER N DAYS


day_after=day_number+n;
i=0;
while(day_after>month_days[i])
{
day_after-=month_days[i];
i++;
if(i==12){
i=0;
year++;
if(year%400==0 || (year%100!=0 && year%4==0))
month_days[1]=29;
}
}
day=day_after;
month=i;
//ADD SUFFIX AS PER THE DAY
if(day%10==1 && day/10!=1)
suffix="ST";
else if(day%10==2 && day/10!=1)

suffix="ND";
else if(day%10==3 && day/10!=1)
suffix="RD";
else
suffix="TH";
//SECOND PART OF THE OUTPUT
System.out.println(day+suffix+" "+
month_names[month]+" "+year);
}
}
}

2. Write a program to declare a matrix A[ ][ ] of order (m*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 20.
Allow the user to input positive integers into this matrix. Perform the following
tasks on the matrix:
(a) Sort the elements of the outer row and column elements in ascending order
using any standard sorting technique.
(b) Calculate the sum of the outer row and column elements.
(c) Output the original matrix, rearranged matrix, and only the boundary
elements of the rearranged array with their sum.
Test your program for the following data and some random data.
1. Example :
3.

INPUT : M=3, N=3

4.

5.

6.

7.

OUTPUT :

8.

ORIGINAL MATRIX :

9.

10.

11.

12.

REARRANGED MATRIX :

13.

14.

15.

16.

BOUNDARY ELEMENTS :

17.

18.

19.

4
5

SUM OF OUTER ROW AND OUTER COLUMN = 43

View/Hide Solution

import java.util.*;
class ISCprac2009q02{
public static void main(String args[])
throws InputMismatchException{
Scanner scan=new Scanner(System.in);

System.out.println("Enter the number of rows and columns(>2


and <20) : ");
int m=scan.nextInt();
int n=scan.nextInt();

if(m<2 || n<2 || m>20 || n>20)


System.out.println("Rows and columns should be more than 2
and less than 20.");

else{
int a[][]=new int[m][n];
int i,j,x,t,c;
System.out.println("Enter "+m*n+" elements for the matrix : ");
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
a[i][j]=scan.nextInt();
}
System.out.println("\nOriginal Matrix : ");
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
System.out.print(a[i][j]+" ");

System.out.println();
}

int b[]=new int[2*m+2*n-4];


x=0;
//STORE THE ELEMENTS OF THE OUTER ROW AND COLUMN
//IN A 1-D ARRAY
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
// CONDITION FOR BOUNDARY ELEMENTS
if(i==0 || j==0 || i==m-1 || j==n-1)
b[x++]=a[i][j];
}

//SORT THE 1-D ARRAY IN ASCENDING ORDER


for(i=0;i< x;i++)
{
for(j=i+1;j< x;j++)
{
if(b[i]>b[j])
{
t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}

//PLACE THE SORTED ELEMENTS FROM 1-D


//ARRAY INTO THE 2-D ARRAY
c=0;
for(i=0;i< n;i++)
a[0][i]=b[c++];

for(i=1;i< m;i++)
a[i][n-1]=b[c++];

for(i=n-2;i>=0;i--)
a[m-1][i]=b[c++];

for(i=m-2;i> 0;i--)
a[i][0]=b[c++];

System.out.println("REARRANGED MATRIX : ");


for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
System.out.print(a[i][j]+" ");

System.out.println();
}
System.out.println("BOUNDARY ELEMENTS : ");
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
if(i==0 || j==0 || i==m-1 || j==n-1)
System.out.print(a[i][j]+" ");
else
System.out.print("

");

System.out.println();
}
}
}//end of main
}//end of class

20. Read a single sentence which terminates with a full stop(.). The words are to be
separated with a single blank space and are in lower case. Arrange the words
contained in the sentence according to the length of the words in ascending
order. If two words are of the same length then the word occurring first in the
input sentence should come first. For both, input and output the sentence must
begin in upper case.

Test your program for the following data and some random data.
INPUT : The lines are printed in reverse order.
OUTPUT : In the are lines order printed reverse.
INPUT : Print the sentence in ascending order.
OUTPUT : In the print order sentence ascending.

View/Hide Solution

import java.io.*;
public class ISC2009q03
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter the string : ");
String str=br.readLine();
int l=str.length();
String a[]=new String[l];
int p=0,x=0,i,j;
String t;
for(i=0;i< l;i++)
{
char ch=str.charAt(i);
if(ch==' '||ch=='.')
{
a[x]=str.substring(p,i);
System.out.print(a[x]+" ");
x++;
p=i+1;
}

}
System.out.println();
for(i=0;i< x;i++)
{
for(j=0;j< x-i-1;j++)
{
if(a[j].length() > a[j+1].length())
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}

String s=a[0];
char ch=s.charAt(0);
System.out.print(Character.toUpperCase(ch));
System.out.print(s.substring(1));
for(i=1;i< x;i++)
{
System.out.print(" "+a[i]);
}
System.out.print(".");
}}
Recently Registered

Guess Questions

Ask a Question
View All Questions
Do you know?
The strongest muscle in proportion to its size in the human body is the tongue.
Quote Of The Day
Much education today is monumentally ineffective. All too often we are giving young people cut
flowers when we should be teaching them to grow their own plants. - Mae West
View all quotes
2014 Mentors. All Rights Reserved. Privacy Policy and Terms of Use
Mon Sep 01 2014 18:23:03 GMT+0530 (India Standard Time)

Você também pode gostar