Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
93 views

Part-A 1) Write A Program To Find Factorial of List of Number Reading Input As Command Line Argument.

The document contains 10 programming problems covering topics like finding factorial of numbers, displaying prime numbers between limits, sorting elements, string operations, calculating area of shapes using methods, constructor overloading, creating a student report using textboxes and buttons, calculating bonus for different departments using method overriding, implementing animation of a moving ball using threads and graphics, and handling mouse and keyboard events. Sample code and outputs are provided for each problem.

Uploaded by

Sanjay San
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Part-A 1) Write A Program To Find Factorial of List of Number Reading Input As Command Line Argument.

The document contains 10 programming problems covering topics like finding factorial of numbers, displaying prime numbers between limits, sorting elements, string operations, calculating area of shapes using methods, constructor overloading, creating a student report using textboxes and buttons, calculating bonus for different departments using method overriding, implementing animation of a moving ball using threads and graphics, and handling mouse and keyboard events. Sample code and outputs are provided for each problem.

Uploaded by

Sanjay San
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Part-A

1) Write a program to find factorial of list of number reading input as


command line argument..

import java.io.*;
public class Factorial
{
public static void main(String args[])
{
int[] arr = new int[10];
int fact;
if (args.length==0){
System.out.println("No command Line arguments ");
return;
}
for(int i=0;i<args.length;i++)
{
arr[i]=Integer.parseInt(args[i]);
}
for (int i=0;i<args.length;i++)
{
fact=1;
while(arr[i]>0)
{
fact=fact*arr[i];
arr[i]--;
}
System.out.println(" Factorial of " +args[i]+ " is : " +fact);
}
}
}

OUTPUT:
java Factorial 2 5 7
Factorial of 2 is : 2
Factorial of 5 is : 120
Factorial of 7 is : 5040
2)Write a program to display all prime numbers between two limits.

import java.io.*;
public class PrimeNumber
{
public static void main(String args[])
{
int i,j;
if(args.length<2)
{
System.out.println("No Command Line Arguments");
return;
}
int num1=Integer.parseInt(args[0]);
int num2=Integer.parseInt(args[1]);
System.out.println("Prime numbers between "+num1+" and "+num2+" are:");
for(i=num1;i<=num2;i++)
{
for(j=2;j<i;j++)
{
int n=i%j;
if(n==0)
{
break;
}
}
if(i==j)
{
System.out.println(" " +i);
}}}}

OUTPUT:
java PrimeNumber
No Command Line Arguments
java PrimeNumber 1 10
Prime numbers between 1 and 10 are :
2
3
5
7
3) Write a program to sort list of elements in ascending and descending order
and show the exception handling.

import java.util.Scanner;
class Sorting
{
public static void main(String args[])
{
int a[] = new int[5];
try
{
for(int i=0;i<5;i++)
a[i]=Integer.parseInt(args[i]);
System.out.println("\n Before Sorting \n");
for(int i=0;i<5;i++)
System.out.print(" " +a[i]);
bubbleSort(a,5);
System.out.println("\n\n After Sorting \n");
System.out.println(" Ascending Order \n");
for(int i=0;i<5;i++)
System.out.print(" " +a[i]);
System.out.println("\n\n Descending Order\n");
for(int i=4;i>=0;i--)
System.out.print(" " +a[i]);
}
catch(NumberFormatException e)
{
System.out.println("Enter only Integers");
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Enter only 5 Integers");
}
}
private static void bubbleSort(int[] arr,int length)
{
int temp,i,j;
for(i=0;i<length-1;i++)
{
for(j=0;j<length-1-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
}

OUTPUT:
java Sorting 10 30 40 20 50

Before Sorting

10 30 40 20 50

After Sorting

Ascending Order

10 20 30 40 50

Descending Order

50 40 30 20 10

4)Write a program to implement all String operations.

import java.io.*;
class StringOperation
{
public static void main(String args[])
{
String s1,s2;
s1="Java";
s2="Programming";
System.out.println();
System.out.println("Given Strings are " + s1 + " and " +s2);
System.out.println();
int len=s1.length();
int len1=s2.length();
System.out.println("Length of " + s1 + " is = " + len);
System.out.println();
System.out.println("Length of " + s2 + " is = " + len1);
System.out.println();
System.out.println("Concatination of two strings = " +s1.concat(s2));
System.out.println();
System.out.println("Comparsion of " + s1 + " and " + s2 + " = " + s1.equals(s2));
System.out.println();
System.out.println("Character Extraction=First char of " + s1 + " is = "
+s1.charAt(0));
System.out.println();
System.out.println("Searching = v occurs at position " +s1.indexOf("v") + " in "
+s1);
System.out.println();
System.out.println("Substring of "+s2+ " Starting from index 3 and ending at 6 is =
"+s2.substring(3,7));
System.out.println();
System.out.println("Replace=Replacing 'r' by 'e' in " + s1 +"is = "
+s2.replace('r','e'));
System.out.println();
System.out.println("The Uppercase of " +s1 + " is = " +s1.toUpperCase());
System.out.println();
System.out.println("The LOwercase of " +s1 + " is = " +s1.toLowerCase());
}
}

OUTPUT:
java StringOperation

Given Strings are Java and Programming

Length of Java is = 4

Length of Programming is = 11
Concatination of two strings = JavaProgramming

Comparsion of Java and Programming = false

Character Extraction=First char of Java is = J

Searching = v occurs at position 2 in Java

Substring of Programming Starting from index 3 and ending at 6 is = gram

Replace=Replacing 'r' by 'e' in Javais = Peogeamming

The Uppercase of Java is = JAVA

The Lowercase of Java is = java

5)Write a java program to find area of geometrical figures using method .

import java.io.*;
class areafigure
{
void circle(float pi, int r)
{
float area=pi*r*r;
System.out.println("Area of Circle = " +area+ " Sq.units");
}
void square(float side)
{
float area=side*side;
System.out.println("Area of Square = " +area+ " Sq.units");
}
void triangle(float base, float height)
{
double area=0.5*(base*height);
System.out.println("Area of Triangle = " +area+ " Sq.units");
}
}
class Area
{
public static void main(String args[])
{
areafigure ob=new areafigure();
System.out.println("Area of Geometrical Figures");
System.out.println();
ob.circle(3.14f,3);
ob.square(1.5f);
ob.triangle(4.2f,1.2f);
System.out.println();
}
}

OUTPUT:
java Area
Area of Geometrical Figures

Area of Circle = 28.26 Sq.units


Area of Square = 2.25 Sq.units
Area of Triangle = 2.5199999809265137 Sq.units

6.Write a java program to implement constructor overloading by passing


different number of parameters of different types..

class tri
{
int x,y;
float z;
tri(int i)
{
x=y=i;
z=i;
}
tri(int i, int j)
{
x=y=i;
z=j;
}
tri(int i,int j,float k)
{
x=i;
y=j;
z=k;
}
float peri()
{
return(x+y+z);
}
}
class OverLoading
{
public static void main(String args[])
{
float val,val1,val2;
tri m=new tri(5);
tri m1=new tri(5,3);
tri m2=new tri(2,3,22.5f);
val=m.peri();
System.out.println("Perimeter of Equilateral Triangle = " +val+ " sq.units\n");
val1=m1.peri();
System.out.println("Perimeter of Isosceles Triangle = " +val1+ " sq.units\n");
val2=m2.peri();
System.out.println("Perimeter of Scalene Triangle = " +val2+ " sq.units\n");
}
}

OUTPUT:
java OverLoading

Perimeter of Equilateral Triangle = 15.0 sq.units

Perimeter of Isosceles Triangle = 13.0 sq.units

Perimeter of Scalene Triangle = 27.5 sq.units

7. write a java program to create a student report by reading the i/p using
textbox and display o/p using buttons

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class StudReport extends Applet implements ActionListener
{
TextField tRegNo,tName,tCourse,tResult;
Label lTitle,lRegNo,lName,lCourse,lResult;
Button CReport;
String eno=" ",course=" ",nam=" ",res=" ";
public void init()
{
lTitle=new Label("Enter Students Details");
lRegNo=new Label("Register Number",Label.RIGHT);
lName=new Label("Student Name",Label.RIGHT);
lCourse=new Label("Course",Label.RIGHT);
lResult=new Label("Result",Label.RIGHT);
tRegNo=new TextField(20);
tName=new TextField(20);
tCourse=new TextField(20);
tResult=new TextField(20);
CReport=new Button("Report");
add(lTitle);
add(lRegNo);
add(tRegNo);
add(lName);
add(tName);
add(lCourse);
add(tCourse);
add(lResult);
add(tResult);
add(CReport);
tRegNo.addActionListener(this);
tName.addActionListener(this);
tCourse.addActionListener(this);
tResult.addActionListener(this);
CReport.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==CReport)
{
eno=tRegNo.getText();
course=tCourse.getText();
nam=tName.getText();
res=tResult.getText();
eno="Register Number:"+eno;
nam="Name:" +nam;
course="Course:" +course;
res="result:" +res;
repaint();
}
}
public void paint(Graphics g)
{
g.drawString(eno,30,180);
g.drawString(nam,30,200);
g.drawString(course,30,220);
g.drawString(res,30,240);
}
}
/* <applet code=StudReport.class width=500 height=700>
</applet> */

8.write a program to calculate bonus for different department using method


overriding.

import java.io.*;
class SuperClass
{
float basic,da,it;
SuperClass(float b,float d,float i)
{
basic=b;
da=d;
it=i;
}
double bonus()
{
System.out.println("Research department:");
return (basic*0.2);
}
}
class SubClass extends SuperClass
{
float hra,pt;
SubClass(float b,float d,float i,float p,float h)
{
super(b,d,i);
pt=p;
hra=h;
}
double bonus()
{
System.out.println("Administration department");
return(basic*0.4);
}
}
class FindSal
{
public static void main(String args[])
{
SubClass sc=new SubClass(20000,12000,6000,1000,500);
System.out.println("Bonus is Rs. = " +sc.bonus()+ "\n");
}
}

9.write a program to implement thread,applet, and graphics by implementing


animation of ball moving.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class MovingBall extends Applet implements Runnable
{
int x=100;
int y=100;
int dx=10;
int dy=10;
int w=100;
int h=150;
Thread t=null;
boolean flag;
public void init()
{
setBackground(Color.yellow);
}
public void start()
{
flag=true;
Thread t=new Thread(this);
t.start();
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(x,y,50,50);
}
public void stop()
{
t=null;
flag=false;
}
public void run()
{
while(flag)
{
if((x+dx<=0)||(x+dx>=w))
dx=-dx;
if((y+dy<=0)||(y+dy>=h))
dy=-dy;
x+=dx;
y+=dy;
repaint();
try
{
Thread.sleep(300);
}
catch(InterruptedException ex)
{}
}
}
}
/* <applet code=MovingBall.class width=300 height=300>
</applet> */

10.write a program to implement mouse event and keyboard events

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MouseEvent extends Applet implements MouseListener,
MouseMotionListener
{
String str=" ";
public void init()
{
add MouseListener(this);
add MouseMotionListener(this);
requestFocus();
}
public void mousePressed(MouseEvent e)
{
show Status("mouse pressed");
}
public void mouseClicked(MouseEvent e)
{
show Status("mouse clicked");
}
public void mouseDragged(MouseEvent e)
{
show Status("mouse dragged");
}
public void mouseMoved(MouseEvent e)
{
show Status("mouse moved");
}
public void mouseExited(MouseEvent e)
{
show Status("mouse exited");
}
public void mouseEntered(MouseEvent e)
{
show Status("mouse entered");
}
public void mouseReleased(MouseEvent e)
{
show Status("mouse released");
}
public void paint(Graphics g)
{
g.drawString(str,20,20);
}
}
<applet code=MouseEvent.class width=300,Height=400>
</applet>

Part-B

1.write a java program to find given number is Even or Odd

import java.io.*;
import java.util.Scanner;
class EvenOdd
{
public static void main(String args[])
{
int x;
System.out.println("Enter a number");
Scanner sc=new Scanner(System.in);
x=sc.nextInt();
if(x%2==0)
System.out.println("The given number is Even");
else
System.out.println("The given number is Odd");
}
}
OUTPUT:

java EvenOdd

Enter a number
4
The given number is Even

Enter a number
3
The given number is Odd

2.Write a program to implement math class functions

class MathFunction
{
public static void main(String args[])
{
System.out.println("Absolute value is = " + (Math.abs(-26.4)));
System.out.println("The Ceil Value is = " +(Math.ceil(6.5)));
System.out.println("The Floor value is = " +(Math.floor(5.8)));
System.out.println("Squart of a number is =" +(Math.sqrt(16)));
System.out.println("Power of given number is =" +(Math.pow(2,3)));
System.out.println("Minimum Value is =" +(Math.max(26.4,50)));
System.out.println("Maximum Value is =" +(Math.max(26.4,50)));
}
}

java MathFunction
Absolute value is = 26.4
The Ceil Value is = 7.0
The Floor value is = 5.0
Squart of a number is =4.0
Power of given number is =8.0
Minimum Value is =50.0
Maximum Value is =50.0
3.write a program to copy elements from one array to another

import java.io.*;
public class Copyarray
{
public static void main(String args[])
{
int array1[]={10,20,30,40,50};
int array2[] = new int[5];
System.out.println("The elements of Array1: ");
for(int Var:array1)
{
System.out.println(" " +Var);
}
System.out.println(" The elements of Array2: ");
for(int j=0;j<array1.length;j++)
{
array2[j]=array1[j];
System.out.println(" " +array2[j]);
}
}
}

OUTPUT:
java Copyarray
The elements of Array1 :
10
20
30
40
50
The elements of Array2 :
10
20
30
40
50
4.write a java program to add 2 numbers and input values are sent through
command line argument

import java.io.*;
public class Sum
{
public static void main(String args[])
{
if(args.length>=2)
{
int num1= Integer.parseInt(args[0]);
int num2=Integer.parseInt(args[1]);
int sum=num1+num2;
System.out.println("Sum of two numbers = " +sum);
}
else
{
System.out.println("Insufficient Number of command line arguments");
}
}
}

OUTPUT:
java Sum
Insufficient Number of command line arguments

java Sum 10 20
Sum of two numbers = 30

5.Write a program to implement Rhombus pattern reading the limit from


user

import java.io.*;

public class RhombusPattern


{
public static void main(String ars[]) throws IOException
{
int i,j,limit;
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.println(“Enter the limit”);
limit=Integer.parseInt(br.readLine());
for(i=1;i<=limit;i++)
{
for(j=limit-i;j>0;j--)
System.out.print(“ ”);
for(j=1;j<=2*i-1;j++)
System.out.print(“*”);
System.out.println();
}
for(i=limit-1;i>=1;i--){
for(j=1;j<=limit-i;j++)
System.out.print(“”);
for(j=1;j<=2*i-1;j++)
System.out.print(“*”);
System.out.println();
}
}
}

6.Write a java program to check whether the accepted number is palindrome


or not

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Palindrome
{
public static void main(String args[])throws IOException
{
int rev=0,num,digit,temp;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the number”);
num=Integer.parseInt(br.readLine());
temp=num;
while(num>0)
{
digit=num%10;
num=num/10;
rev=rev*10+digit;
}
if(temp==rev)
System.out.println(“The number is palindrome”);
else
System.out.println(“The number is not palindrome”);
}
}

OUTPUT:

7.write a java program to find Reverse of a number


import java.io.*;
public class Reverse
{
public static void main(String args[])throws IOException
{
int rev=0,num,digit;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the number”);
num=Integer.parseInt(br.readLine());
while(num>0)
{
digit=num%10;
num=num/10;
rev=rev*10+digit;
}
System.out.println(“Reverse of a number is:” +rev);
}
}
OUTPUT:

java Palindrome

Enter the number


121
The number is palindrome

java Palindrome
Enter the number
345
The number is not palindrome

8.write a java program for Multiplication table

import java.io.*;
import java.util.Scanner;
public class Multiplication
{
public static void main(String args[])
{
int n,c;
System.out.println("Enter a number");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
System.out.println("Multiplication table of " +n+ " is :");
for(c=1;c<=10;c++)
System.out.println(n+ "*" +c+ "=" +(n*c));
}
}

OUTPUT:

java Multiplication
Enter a number
2
Multiplication table of 2 is :
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20

9.write a java program to find Sum of 1st n natural numbers

import java.io.*;
import java.util.Scanner;
public class SumNatural
{
public static void main(String args[])
{
int n,i=1,sum=0;
Scanner input=new Scanner(System.in);
System.out.println("Enter a number");
n=input.nextInt();
do
{
sum=sum+i;
i+=1;
}
while(i<=n);
System.out.println("Sum of First " +n+ " Natural Numbers = " +sum);
}
}
OUTPUT
Java SumNatural 6
Sum of First 6 Natural Numbers = 21

10.write a java program to find largest of two numbers

import java.io.*;
import java.util.Scanner;
public class Largest
{
public static void main(String args[])
{
Scanner data=new Scanner(System.in);
int num1,num2;
System.out.println("Enter first number");
num1=data.nextInt();
System.out.println("Enter second number");
num2=data.nextInt();
if(num1>num2)
System.out.println("The largest number is : " +num1);
else
System.out.println("The largest number is : " +num2);
}
}

OUTPUT

java Largest
Enter first number
20
Enter second number
30
The largest number is : 30

You might also like