Public Class Public Static Void Int New Int If Return For Int
Public Class Public Static Void Int New Int If Return For Int
Public Class Public Static Void Int New Int If Return For Int
num[i]=Integer.parseInt(args[i]);
for(int i=0;i<args.length;i++)
{
int fact=1;
for(int j=1;j<=num[i];j++)
fact *=j;
System.out.println("the factorial of"+args[i]+" is : " +fact);
}
}
}
Output:
The factorial of 6 is:120
Source Code
class Prime
{
public static void main(String args[])
{
int i,j;
if(args.length<2)
{
System.out.println("No command line Argruments ");
return;
}
int num1=Integer.parseInt(args[0]);
int num2=Integer.parseInt(args[1]);
int n=i%j;
if(n==0)
{
break;
}
}
if(i==j)
{
System.out.println(" "+i);
}
}
}
Output:
Prime number between1and10 are:
2
3
5
7
3.
class Sorting
{
try
{
for(int i=0;i<5;i++)
a[i]=Integer.parseInt(args[i]);
System.out.println("Before Sorting\n");
for(int i=0;i<5;i++)
bubbleSort(a,5);
for(int i=0;i<5;i++)
System.out.print(" "+a[i]);
for(int i=4;i>=0;i--)
System.out.print(" "+a[i]);
catch(NumberFormatException e)
catch(ArrayIndexOutOfBoundsException e)
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:
Solution:
public class string1 {
public static void main(String args[])
{
String s1="java";
String s2="programmig";
System.out.println("thr string sre "+s1+"and"+s2);
int len1=s1.length();
int len2=s2.length();
System.out.println("length of "+s1+"is = "+len1);
System.out.println("length of "+s2+"is = "+len2);
System.out.println("the concation of 2 strings "+s1.concat(s2));
System.out.println("first char of "+s1+" is ="+s1.charAt(0));
System.out.println("second char of "+s2+" is ="+s2.charAt(0));
System.out.println("string first "+s1+" is ="+s1.toUpperCase());
/// System.out.println("first char of "+s2+" is ="+s2.toUpperCase());
// System.out.println("first char of "+s1+" is ="+s1.toLowerCase());
System.out.println("second string of "+s2+" is ="+s2.toLowerCase());
System.out.println(" v occurs at position "+s1.indexOf("v")+" in "+s1);
System.out.println("substrig of"+s2+"starting from index 3 and ending at 7 is =
"+s2.substring(3,7));
System.out.println("replacing 'v' with 'z' in "+s1+" is = "+s1.replace('v','z'));
boolean check=s1.equals(s2);
if(check==false)
{
System.out.println(" "+s1+" and " +s2+" are not same");
}
else
{
System.out.println(" "+s1+" and " +s2+" are same");
}
}
}
Output:
thr string sre javaandprogrammig
length of javais = 4
length of programmigis = 10
the concation of 2 strings javaprogrammig
first char of java is =j
second char of programmig is =p
string first java is =JAVA
second string of programmig is =programmig
v occurs at position 2 in java
substrig ofprogrammigstarting from index 3 and ending at 7 is = gram
replacing 'v' with 'z' in java is = jaza
java and programmig are not same
import java.io.*;
class Area
{
public static double circleArea(double r)
{
return Math.PI*r*r;
}
return input;
}
}
Output:
Output:
Initialized with different constructor
Volume of cube1:8
Volume of cube2:400
Volume of cube3:6000
7.Write a program to create student report using applet, read the input
using text boxes and display the o/p using buttons.
Source code
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
TextField txtRegno,txtCourse,txtSemester,txtSub1,txtSub2;
Button cmdReport;
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblRegno,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtRegno,gbc);
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblCourse,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtCourse,gbc);
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblSemester,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtSemester,gbc);
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblSub1,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtSub1,gbc);
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbag.setConstraints(lblSub2,gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbag.setConstraints(txtSub2,gbc);
gbc.anchor=GridBagConstraints.CENTER;
gbag.setConstraints(cmdReport,gbc);
add(lblTitle);
add(lblRegno);
add(txtRegno);
add(lblCourse);
add(txtCourse);
add(lblSemester);
add(txtSemester);
add(lblSub1);
add(txtSub1);
add(lblSub2);
add(txtSub2);
add(cmdReport);
cmdReport.addActionListener(this);
}
rno=txtRegno.getText().trim();
course=txtCourse.getText().trim();
sem=txtSemester.getText().trim();
sub1=txtSub1.getText().trim();
sub2=txtSub2.getText().trim();
avg="Avg Marks:" + ((Integer.parseInt(sub1) +
Integer.parseInt(sub2))/2);
heading="Student Report";
removeAll();
showStatus("");
repaint();
}
}catch(NumberFormatException e)
{
showStatus("Invalid Data");
}
}
public void paint(Graphics g)
{
g.drawString(heading,30,30);
g.drawString(rno,30,80);
g.drawString(course,30,100);
g.drawString(sem,30,120);
g.drawString(sub1,30,140);
g.drawString(sub2,30,160);
g.drawString(avg,30,180);
}
}
import java.io.*;
class Area
{
public static double circleArea(double r)
{
return Math.PI*r*r;
}
return input;
}
import java.awt.*;
import java.applet.*;
/* <applet code="MovingBall.class" height=300 width=300></applet> */
public class MovingBall extends Applet implements Runnable
{
int x,y,dx,dy,w,h;
Thread t;
boolean flag;
public void init()
{
w=getWidth();
h=getHeight();
setBackground(Color.yellow);
x=100;
y=10;
dx=10;
dy=10;
}
public void start()
{
flag=true;
t=new Thread(this);
t.start();
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(x,y,50,50);
}
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 e)
{}
}
}
public void stop()
{
t=null;
flag=false;
}
}
Source code
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
addMouseListener(this);
addMouseMotionListener(this);
repaint();
repaint();
repaint();
repaint();
repaint();
}
public void mouseMoved(MouseEvent me){
repaint();
repaint();
repaint();
Output:
import java.applet.*;
public class KeyBoardEvents extends Applet implements KeyListener
addKeyListener(this);
requestFocus();
str +=e.getKeyChar();
repaint(0);
showStatus("KEY PRESSED");
showStatus("KEY RELEASED");
g.drawString(str,15,15);