Sunteți pe pagina 1din 56

Computer project

NAME : RAJDIP
CHAKRABORTY

CLASS : X

SECTION : A

ROLL NUMBER : 13

SUBJECT : COMPUTER

CONTENTS:-
Page | 1
Computer project

Serial no. Title Page no.

1. Introduction to Java 3.

2. Body 4-54.

3. Conclusion 55.

Introduction :-

Page | 2
Computer project

Java is a simple and yet powerful object


oriented programming language and it is in many respects
similar to C++ a programming language .Java created by
James Gosling from Sun Microsystems(Sun) in 1991 .The target
of Java is to write a program once and then run this program
on multiple operating systems.

A Java code will run on any JVM (Java Virtual Machine). A Java
code is not dependent upon Processor Architecture. A Java
application compiled on 64 bit architecture of any platform will
run on 32 bit system without any issue.

Java is a Dynamic programming language which means


it executes many programming behavior at Runtime and don’t
need to be passed at compile time as in the case of static
programming. Java supports distributed System which means
we can access files over Internet just by calling the methods. It
is a robust programming language which means it can cope
with error while the program is executing as well as keep
operating with abnormalities to certain extent.

Inventor of Java :-

Java was invented by James Gosling. James Arthur Gosling is a


Canadian computer scientist, best known as the founder and
lead designer behind the Java Programming Language. He was
born on May 19, 1955 near Calgary, Alberta. He now lives at
San Francisco Bay Area, California, U.S. He is a Canadian by
nationality. He studied at several institutions namely – Sun
Microsystems, Oracle Corporation, Google, Liquid Robotics,
Type safe Inc., Amazon Web Services. He is best known as the
founder and lead designer behind the Java Programming
Language.

Page | 3
Computer project

Body:-
Program 1:-
Write a program in Java to display the pattern are as follows :-

*
* * *
* * * * *
* * *
*

Program code:-

//to print the above pattern

public class pattern1

public static void main()

int i,j,k=2,m; // declaring variables

Page | 4
Computer project

for(i=1;i<=5;i+=2)

for(m=1;m<=k;m++)

System.out.print(" ");

for(j=1;j<=i;j++)

System.out.print("*");

System.out.println();

k--;

k+=2;

for(i=3;i>=1;i-=2)

for(m=1;m<=k;m++)

System.out.print(" ");

Page | 5
Computer project

for(j=1;j<=i;j++)

System.out.print("*");

System.out.println();

k++;

Variable description:-

Variable Data type Usage

i Integer For running loop

j Integer For running loop

m Integer For running loop

k Integer As counter variable

Page | 6
Computer project

Output : -

Program 2:-
Write a program in Java to display the pattern as follows :-

abcdcba
abc cba
ab ba
a a
ab ba
Page | 7
Computer project

abc cba
abcdcba
Program code:-

//to print the above pattern

public class pattern

public static void main()

int n=97,i,j,k; // declaring variables

String j1="",j2=" ";

for(i=4;i>=1;i--)

n=97;

for(j=0;j<i;j++)

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

System.out.print(j1);

j1=j1+" ";

Page | 8
Computer project

for(k=i-1;k>=0;k--)

if(i==4 && k==i-1)

k=k-1;

System.out.print((char)(n+k));

System.out.print("\n");

if(i==4)

j1=" ";

for(i=2;i<=4;i++)

n=97;

for(j=0;j<i;j++)

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

if(i<4)

System.out.print(j2);

Page | 9
Computer project

j2=" ";

for(k=i-1;k>=0;k--)

if(i==4 && k==i-1)

k=k-1;

System.out.print((char)(n+k));

System.out.print("\n");

Variable description:-

Variable Data type Usage

n Integer For storing small


alphabet’s ASCII value

i Integer For running loop

j Integer For running loop

Page | 10
Computer project

k Integer For running loop

Output : -

Program 3:-
Write a program in Java that implements interest class which
stores three variables rate, time and principal. Calculate the Simple
Interest. [use Function calling]:-

Program code:-

//to find interest using function calling

import java.util.*;

public class interest

public static void main()

Page | 11
Computer project

interest ob=new interest();

Scanner ss=new Scanner(System.in);

double principal,rate,time; // declaring variables

System.out.println("Enter the Principal Amount : ");

principal=ss.nextDouble();

System.out.println("Enter the Rate : ");

rate=ss.nextDouble();

System.out.println("Enter the time in years : ");

time=ss.nextDouble();

ob.simpleInterest(principal,rate,time);

void simpleInterest(double p,double r,double t)

double si;

si=(p*t*r)/100;

System.out.println("SIMPLE INTEREST : "+si);

Page | 12
Computer project

Variable description:-

Variable Data type Usage

principal Double Storing principal amount

rate Double Storing rate of interest

time Double Storing time period of


interest

si Double Storing the simple


interest earned

Output : -

Page | 13
Computer project

Program 4:-
Write a program in Java that invokes method toWord() that
receives a single digit number and displays it in words:-

Program code:-

import java.util.*;

public class digit_in_word

public static void main()

digit_in_word ob=new digit_in_word();

Scanner ss=new Scanner(System.in);

int d;

Page | 14
Computer project

System.out.println("Enter a single digit number : ");

d=ss.nextInt();

ob.toWord(d);

void toWord(int digi)

switch(digi)

case 1: System.out.println("ONE");

break;

case 2: System.out.println("TWO");

break;

case 3: System.out.println("THREE");

break;

case 4: System.out.println("FOUR");

break;

case 5: System.out.println("FIVE");

break;

case 6: System.out.println("SIX");

break;

Page | 15
Computer project

case 7: System.out.println("SEVEN");

break;

case 8: System.out.println("EIGHT");

break;

case 9: System.out.println("NINE");

break;

default: System.out.println("....NOT A SINGLE DIGIT NUMBER....");

Variable description:-

Variable Data type Usage

digi Integer It stores the value of d


which is transferred to it.

d Integer For storing the single


digit number

Output : -

Page | 16
Computer project

Program 5:-
Write a program in Java to display the amount in rupees in terms
of notes of different denominations:-

Program code:-

import java.util.*;

public class aamount_in_rupees

public static void main()

Scanner ss=new Scanner(System.in);

int a,b,c,d,f,g,h,n,i,j,w;

System.out.println("Enter the amount of money");

n=ss.nextInt();

Page | 17
Computer project

w=n/2000;

System.out.println("Number of 2000 ruppes notes : "+w);

n=n%2000;

a=n/500;

System.out.println("Number of 500 rupees notes : "+a);

n=n%500;

b=n/200;

System.out.println("Number of 200 rupees notes : "+b);

n=n%200;

c=n/100;

System.out.println("Number of 100 rupees notes : "+c);

n=n%100;

d=n/50;

System.out.println("Number of 50 rupees notes : "+d);

n=n%50;

f=n/20;

System.out.println("Number of 20 rupees notes : "+f);

n=n%20;

g=n/10;

System.out.println("Number of 10 rupees notes : "+g);

Page | 18
Computer project

n=n%10;

h=n/5;

System.out.println("Number of 5 rupees notes : "+h);

n=n%5;

i=n/2;

System.out.println("Number of 2 Rupee notes : "+i);

n=n%2;

System.out.println("Number of 1 Rupee notes : "+n);

Variable description:-

Variable Data type Usage

a Integer Stores the number of Rs.500


notes

b Integer Stores the number of Rs.200


notes

c Integer Stores the number of Rs.100


notes

d Integer Stores the number of Rs.50


notes

f Integer Stores the number of Rs.20


notes

Page | 19
Computer project

g Integer Stores the number of Rs.10


notes

h Integer Stores the number of Rs.5 notes

n Integer Stores the amount of money&


the number of Rs.1 notes

i Integer Stores the number of Rs.2 notes

w Integer Stores the number of Rs.2000


notes

Output : -

Page | 20
Computer project

Program 6:-
Write a program in Java that implements a rectangle class. The
rectangle has fields length, breadth, area and perimeter. It has
methods to obtain the values and length and breadth to calculate
area and perimeter. Invoke methods of rectangle class using main
function:-

Program code:-

import java.util.*;

public class rectangle

double length,breadth,area,perimeter;

Page | 21
Computer project

void getValues()

Scanner ss=new Scanner(System.in);

System.out.println("Enter the Length : ");

length=ss.nextDouble();

System.out.println("Enter the Breadth : ");

breadth=ss.nextDouble();

calculate();

void calculate()

area=length*breadth;

perimeter=2*(length+breadth);

void display()

System.out.println("AREA OF THE RECTANGLE : "+area);

System.out.println("PERIMETER OF THE RECTANGLE : "+perimeter);

public static void main()

Page | 22
Computer project

rectangle ob=new rectangle();

ob.getValues();

ob.display();

Variable description:-

Variable Data type Usage

length Double Stores the length of the


rectangle

breadth Double Stores the breadth of the


rectangle

area Double Calculates and stores the


area of the rectangle

Page | 23
Computer project

perimeter Double Calculates and stores the


perimeter of the
rectangle

Output : -

Program 7:-
n is a perfect number if the sum of all factors of the number is the
sum excluding itself is n. Define a class called Number Problems
which has the following functions – int sumOfFactors(int n) which
returns sum of all the number n except itself. booleani Perfect(int
n) which returns true if number n is perfect and false otherwisw.
voidperfectNO(int lim) which first prints out all the perfect number
less than lim.:-

Program code:-

Page | 24
Computer project

import java.util.*;

public class numberProblem

int sumOfFactors(int n)

int i,s=0;

for(i=1;i<n;i++)

if(n%i==0)

s=s+i;

return s;

boolean isPerfect(int n)

if(sumOfFactors(n)==n)

return true;

else

return false;

Page | 25
Computer project

void perfectNo(int lim)

int k,j,c=1;

System.out.println("Perfect numbers below "+lim);

for(k=1;k<lim;k++)

if(isPerfect(k))

System.out.print(+k+" = ");

for(j=1;j<k;j++)

if(k%j==0)

System.out.print(+j+" ");

System.out.println();

System.out.println();

Page | 26
Computer project

public static void main()

numberProblem ob=new numberProblem();

Scanner ss=new Scanner(System.in);

int lim;

System.out.println("Enter the Limit : ");

lim=ss.nextInt();

ob.perfectNo(lim);

Variable description:-

Variable Data type Usage

i Integer For running loop

s Integer Storing return value

k Integer For running loop

j Integer For running loop

lim Integer Stores the limit value

Page | 27
Computer project

Output : -

Program 8:-
Write a menu-driven program to perform – (i) Accept a number and
convert to its binary equivalent & (ii) Accept a binary number and
convert to its decimal form:-

Program code:-

import java.util.*;

public class binaryDecimal

Page | 28
Computer project

public static void main()

Scanner ss=new Scanner(System.in);

int ch;

long d,b,s=0,s1=0,c=0,p=0,e=0;

System.out.println("1. CONVERT DECIMAL NUMBER TO BINARY NUMBER");

System.out.println("2. CONVERT BINARY NUMBER TO DECIMAL NUMBER");

System.out.println("ENTER YOUR CHOICE : ");

ch=ss.nextInt();

switch(ch)

case 1: System.out.println("Enter a Decimal number : ");

d=ss.nextLong();

while(d>0)

c=d%2;

s=s*10+c;

d=d/2;

Page | 29
Computer project

while(s>0)

c=s%10;

s1=s1*10+c;

s=s/10;

System.out.println("BINARY EQUIVALENT : "+s1);

break;

case 2: System.out.println("Enter a Binary number : ");

b=ss.nextLong();

while(b>0)

c=b%10;

e=e+c*(long)Math.pow(2,p);

b=b/10;

p++;

System.out.println("DECIMAL EQUIVALENT : "+e);

break;

default: System.out.println("....WRONG CHOICE....");

Page | 30
Computer project

Variable description:-

Variable Data type Usage

ch Integer Stores user’s choice

d Long Stores the decimal number

b Long Stores the binary number

s Long Used for calculations

s1 Long Used for calculations &


stores binary equivalent

c Long Used for calculations

p Long Used for calculations

e Long Used for calculations &


stores decimal equivalent

Output : -

Page | 31
Computer project

Program 9:-
Write a program in Java to implement Bubble Sorting:-

Program code:-

import java.util.*;

public class bubble_sorting

public static void main()

Scanner ss = new Scanner(System.in);

int m[] = new int[5];

int i,j,t=0;

for(i=0;i<5;i++)

System.out.println("Enter a number : ");

m[i] = ss.nextInt();

for(i=0;i<4;i++)

for(j=i+1;j<5;j++)

Page | 32
Computer project

if(m[i]>m[j])

t = m[i];

m[i] = m[j];

m[j] = t;

for(i=0;i<5;i++)

System.out.println(m[i]);

Variable description:-

Variable Data type Usage

m Integer For making array.

i Integer For running outer


loop.

Page | 33
Computer project

j Integer For running inner


loop.

t Integer Empty variable to


store values.

Output : -

Program 10:-
Write a program in Java to implement Selection Sorting:-

Program code:-

import java.util.*;

public class selection_sorting

public static void main()

Page | 34
Computer project

Scanner ss = new Scanner(System.in);

int m[] = new int[5];

int i,j,t=0,min=0;

for(i=0;i<5;i++)

System.out.println("Enter a number : ");

m[i] = ss.nextInt();

for(i=0;i<4;i++)

min = i;

for(j=i+1;j<5;j++)

if(m[i]>m[j])

min = j;

t = m[i];

m[i] = m[min];

m[min] = t;

Page | 35
Computer project

for(i=0;i<5;i++)

System.out.println(m[i]);

Variable description:-

Variable Data type Usage

m Integer For making array.

i Integer For running outer


loop.

j Integer For running inner


loop.

t Integer Empty variable.

min Integer Stores the min. value

Page | 36
Computer project

Output : -

Program 11:-

Page | 37
Computer project

Write a program in Java to implement calculator with following


functions ---- .Addition, .Substraction, .Multiplication, .Integer
Division, .Integer Minus :-

Program code:-

import java.util.*;

public class calculator

int a,b,c=0,ch;

public static void main()

calculator ob=new calculator();

Scanner ss=new Scanner(System.in);

System.out.println("Enter 1st number : ");

ob.a=ss.nextInt();

System.out.println("Enter 2nd number : ");

ob.b=ss.nextInt();

System.out.println("1. ADDITION");

System.out.println("2. SUBTRACTION");

System.out.println("3. MULTIPLICATION");

Page | 38
Computer project

System.out.println("4. INTEGER DIVISION");

System.out.println("5. INTEGER MINUS");

System.out.println("ENTER YOUR CHOICE : ");

ob.ch=ss.nextInt();

switch(ob.ch)

case 1: ob.addition();

break;

case 2: ob.subtraction();

break;

case 3: ob.multiplication();

break;

case 4: ob.integerDivision();

break;

case 5: ob.integerMinus();

break;

default: System.out.println("....WRONG CHOICE....");

void addition()

Page | 39
Computer project

c=a+b;

System.out.println("ADDITION IS : "+c);

void subtraction()

if(a>b)

c=a-b;

else

c=b-a;

System.out.println("SUBTRACTION IS : "+c);

void multiplication()

c=a*b;

System.out.println("MULTIPLICATION IS : "+c);

void integerDivision()

if(a>b)

Page | 40
Computer project

c=a/b;

else

c=b/a;

System.out.println("INTEGER DIVISION IS : "+c);

void integerMinus()

c=a-b;

System.out.println("INTEGER MINUS IS : "+c);

Variable description:-

Variable Data type Usage

a Integer Stores the first


number.

b Integer Stores the second


number.

c Integer Stores the resultant


value.

ch Integer Stores the user’s


Page | 41
Computer project

choice.

Output : -

Program 12:-
Twin prime numbers are prime numbers where difference is two.
Eq- 3,5 5,7 11,13 17,14, etc. :

Define a class which takes an integer argument and print all the
twin numbers :-

Program code:-

import java.util.*;

Page | 42
Computer project

public class twinPrime

public static void main()

Scanner ss=new Scanner(System.in);

int lim,n,i,j,num,c=0,k=0,m,p=0;

System.out.println("Enter the Limit : ");

lim=ss.nextInt();

for(i=1;i<lim-2;i++)

num=i+2;

for(j=1;j<=i;j++)

c=i%j;

if(c==0)

k++;

for(m=1;m<=num;m++)

c=num%m;

Page | 43
Computer project

if(c==0)

p++;

if(k==2 && p==2)

System.out.println(i+" "+num+" are twin prime numbers");

k=0;

p=0;

Variable description:-

Variable Data type Usage

lim Integer Stores the limit of numbers to


check.

i Integer For running loop.

j Integer For running loop.

num Integer Stores the twin prime numbers.

c Integer Used for calculations.

k Integer Counter variable.

m Integer For running loop.

Page | 44
Computer project

p Integer Counter variable.

Output : -

Program 13:-
Write a program in Java to input any Roman Number and the same
in decimal format using User Defined Function:-

Page | 45
Computer project

Program code:-

import java.io.*;

public class romantodecimal

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int x;

int d=0;//Declaring variables

String ro;//Declaring variables

System.out.println("Enter the Roman number:");

ro=br.readLine();

String ron=ro.toUpperCase();

for(x=0;x<ron.length();x++)//Checking each Roman Numerical

char v=ron.charAt(x);

switch(v) //Checking each single Roman Numerical

case 'M':

Page | 46
Computer project

d+=1000;

break;

case 'D':

d+=500;

break;

case 'C':

d+=100;

break;

case 'L':

d+=50;

break;

case 'X':

d+=10;

break;

case 'V':

Page | 47
Computer project

d+=5;

break;

case 'I':

d+=1;

break;

//Checking special cases in Roman

if(ron.contains("IV"))

d-=2;

if(ron.contains("IX"))

d-=2;

if(ron.contains("XL"))

d-=20;

Page | 48
Computer project

if(ron.contains("XC"))

d-=20;

if(ron.contains("CD"))

d-=200;

if(ron.contains("CM"))

d-=200;

System.out.println("The Decimal Number:"+d);

Variable description:-

Variable Data type Variable description

d Integer Stores decimal value.

ro String Stores roman value entered


Page | 49
Computer project

by the users.

ron String Stores the value of ro in


upper case.

x Integer Used to run a loop.

v Character Stores a single letter.

Output : -

Program 14:-
Write a program to accept a string. Convert the string to
uppercase. Count the number of double letter sequences that exist
in the string:-

Program code:-

import java.io.*;
public class DoubleLetter
{
public static void main(String args[])throws IOException

Page | 50
Computer project

{
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
String s,u;
System.out.println("Enter a String:");
s=br.readLine();
u = s.toUpperCase();
int i,p=0;
for (i=0;i<(u.length()-1);i++)
{
if ((u.charAt(i))==u.charAt(i+1))
{
p++;
}
}
System.out.println("The string in upper case : " + u);
System.out.println("Number of double letter sequences in the string : " + p);
}
}

Variable description:-

Variable Data type Usage

s String Stores the string.

u String Stores the string converted


into upper case.

i Integer For running loop.

p Integer For counting the number of


double letter sequences.

Page | 51
Computer project

Output : -

Program 15:-
Write a program in Java to store the names, address and telephone
numbers of “N” number of students. Process the data in such a way
that the output appears in alphabetical order under appropriate
headings as given:-
Program code:-

import java.io.*;
public class students
{
public static void main(String args[]) throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int lim;
System.out.println("Enter the number of students : ");
lim=Integer.parseInt(br.readLine());
String name[]=new String[lim];
Page | 52
Computer project

String add[]=new String[lim];


String tel[]=new String[lim];
String n,a,t;
int i,j;
for(i=0;i<lim;i++)
{
System.out.println("Enter Student Name : ");
name[i]=br.readLine();
System.out.println("Enter Student Address : ");
add[i]=br.readLine();
System.out.println("Enter Student Telephone No. : ");
tel[i]=br.readLine();
}
for(i=0;i<lim-1;i++)
{
for(j=i+1;j<lim;j++)
{
if(name[i].compareTo(name[j])>0)
{
n=name[i];
name[i]=name[j];
name[j]=n;
a=add[i];
add[i]=add[j];
add[j]=a;
t=tel[i];
tel[i]=tel[j];
tel[j]=t;

Page | 53
Computer project

}
}
}
System.out.println("Name\t\t\tAddress\t\t\tTelephone No.");
System.out.println("====\t\t\t=======\t\t\t=============");
System.out.println();
for(i=0;i<lim;i++)
{
System.out.print(name[i]+"\t\t"+add[i]+"\t\t"+tel[i]);
System.out.println();
}
}
}

Variable description:-

Variable Data type Usage

lim Integer Stores the number of students.

name String Stores the name of the student.

add String Stores the address of the student.

tell String Stores the telephone number of


the student.

n String Used for sorting.

a String Used for sorting.

t String Used for sorting.

i Integer For running loop.

Page | 54
Computer project

j Integer For running loop.

Output : -

CONCLUSION
As with any programming language, Java is a script with
advantages and disadvantages.Java is a general use
programming language.It can,with time and energy,do
just about anything with a decent degree of
efficiency.But if the user is in hurry ,then other
languages are better.The syntax of Java is arguably
simpler than other C derivatives.The Java library is well-
written.Java implements finalization and cleanup of
objects without the need for destructors.It is also
portable to other systems.

Page | 55
Computer project

Java is worth learning and using.It’s a


handy language to have for working environments
where a lot of different computer systems are in use, or
when you need to pass a project between different
programmers.It’s far from the ultimate
language,however,and should be used in conjunction
with others to create robust,working programs.

Page | 56

S-ar putea să vă placă și