FILE
FILE
class First{
public static void main(String args[])
{
int a=10,b=5;
if(a==b)
{
System.out.println("a and b are equal numbers ");
}
else{
if(a>b)
System.out.println("a is greater than b");
else
System.out.println("b is gerater than a");
}
}
}
Output:
2.Write a program to find sum, average and percentage of Marks of three subjects of a
Student.
class First{
sum=sub1+sub2+sub3;
avg=sum/3;
per=avg;
System.out.println("Sum of 3 Subjects = "+sum);
System.out.println("Average of 3 Subjects = "+avg);
System.out.println("Percentage = "+per);
}
}
Output:
3. Write a program to check whether the given 2 numbers in the programs are equal .If
not then find the greater of 2 given numbers.
Output:
class fact
{
public static void main(String [] args)
{
int a=10,i,fact;
fact=1;
for (i=1;i<=10;i++)
{
fact=fact*i;
}
System.out.println("factorial of 10="+fact);
}
}
Output:
5. Write a program to find the cube of number between 1 to 10.
class Cube
{
public static void main(String [] args)
{
int i;
for(i=1;i<=10;i++)
{
cube=i*i*i;
System.out.println("cube of "+i+"="+i*i*i);
}
}
}
Output:
class table
{
public static void main(String [] args)
{
int i;
for(i=0;i<10;i++)
{
System.out.println("\n");
for(int j=0;j<=i;j++)
{
System.out.print(i*j+"\t");
}
}
}
}
7. Write a Java program that has a method for the calculation of the fourth power of 2.
class Fourth{
8. Write a Java program that has a method for initialization of variables to 10 and 20
and another method which will display the same.
class MethodA{
int x,y;
public void setVar(int a,int b){
x=a;
y=b;
}
public void display(){
System.out.println("b="+x+" a="+y);
}
}
class VarSet{
public static void main(String args[])
{
MethodA a1=new MethodA();
a1.setVar(10,20);
a1.display();
}
}
Output:
9. Write a Java program using the concept of Method Overloading to add two integers,
add three integers, add two double type values and to concatenate two strings.
class over{
int res;
void add(int a,int b){
res=a+b;
System.out.println("Sum of (4,5) integres="+res);
}
Output:
10. Write a class Rectangle with overloaded constructors. In the first constructor accept
only one parameter and the code for the constructor should display that “It is a Square”
and calculate the area. The second constructor should take two parameters and display
“It is a Rectangle” and calculate the area.
class Rectangle{
Rectangle(double side){
System.out.println("It is a Square with side="+side+" and Area ="+side*side);
}
Output:
11. Write a program to accept various features of java as command line arguments and
display the total number of arguments with their features in the following format :-
No. of arguments:
1 : Java is Simple
2 : Java is Object-oriented
3 : Java is Distributed
4 : Java is secure
5 : Java is Multithreaded
class javaFeatures
{
public static void main(String args[])
{
String s1=args[0],s2=args[1],s3=args[2],s4=args[3],s5=args[4];
int count=0;
System.out.println("Number of arguments :");
for(int i=0;i<args.length;i++){
count=i+1;
System.out.println(count+" : "+" Java is "+args[i]);
}
}
}
12. Write a java program to accept parameters on the command line .If there are no
command line argument entered, the program should print an error message and exit
else the arguments should be printed.
class printArg
{
public static void main(String args[])
{
int count=0;
if(args.length>0)
{
for(int i=0;i<args.length;i++)
{
count=i+1;
System.out.println("Argument "+count+" : "+args[i]);
}
}
else{
System.out.println("No Arguments Entered");
}
}
}
Output:
13. Write a java program to accept 3 command line arguments from the user and
display it in sorted order .
class sortArg
{
public static void main(String args[])
{
int num1,num2,num3,temp=0;
num1=Integer.parseInt(args[0]);
num2=Integer.parseInt(args[1]);
num3=Integer.parseInt(args[2]);
if ((num1 > num2 && num1 > num3))
{
if(num2 > num3)
{
System.out.print(num3 + " " + num2 + " " + num1);
}
else
System.out.print(num2 + " " + num3 + " " + num1);
}
else if ((num2 > num1 && num2 > num3))
{
if(num1 > num3)
{
System.out.print(num3 + " " + num1 + " " + num1);
}
else
{
System.out.print(num1 + " " + num3 + " " + num2);
}
}
else if ((num3 > num1 && num3 > num2))
{
if(num1 > num2)
{
System.out.print(num2 + " " + num1 + " " + num3);
}
else
System.out.print(num1 + " " + num2 + " " + num3);
}
else
{
System.out.println("ERROR!");
}
}
}
Output:
class W{
float var;
}
class X extends W{
String Xvar;
}
class Y extends X{
String Yvar;
}
class Z extends Y{
int Zvar;
}
class XYZ{
public static void main(String args[]){
Z w=new Z();
w.Zvar=21;
System.out.println("Z class , Integer var="+w.Zvar);
w.Xvar=new String("Hi");
System.out.println("X class , String var="+w.Xvar);
w.var=4.5f;
System.out.println("W class , Double var="+w.var);
w.Yvar=new String("Hello");
System.out.println("Y class , String var="+w.Yvar);
}
}
Output:
15. Create a base class called Shape. It should contain two methods getCoord() and
showCord() to accept x and y coordinates and to display the same respectively.
class Shape{
int x_coord,y_coord;
public void getCoord(int x,int y){
x_coord=x;
y_coord=y;
}
public void showCoord(){
System.out.println("x coordinates:"+x_coord);
System.out.println("y coordinates:"+y_coord);
}
Output:
16. Write an application that concatenates the following three Strings: “Event Handlers
is dedicated”, “to make your event” and “a most memorable one Madam.” Print each
string separately and the concatenated String. Also perform the following on the
concatenated String –
a) Identify the length of a String
b) Identify the index of the character ‘e’ after leaving 3 characters
c) Replace all small ‘a’ character with capital ‘A’
d) Verify that the String is ending with “Madam”
e) Extract the String “Madam” and Verify that it is a palindrome.
f) Count the total number of Vowels with the frequency in the given String.
class exercise{
public static void main(String args[])
{
String s1,s2,s3,s4,s5,s6;
//Set TextField
TextField t1=new TextField();
TextField t2=new TextField();
TextField t3=new TextField();
t1.setBounds(300,50,200,30);
t2.setBounds(300,100,200,30);
t3.setBounds(300,150,200,30);
f1.add(t1);f1.add(t2);f1.add(t3);
//add buttons
Button b1=new Button("ADD");
Button b2=new Button("SUB");
Button b3=new Button("MUL");
Button b4=new Button("DIV");
Button b5=new Button("CLEAR");
Button b6=new Button("EXIT");
b1.setBounds(50,300,50,30);
b2.setBounds(170,300,50,30);
b3.setBounds(300,300,50,30);
b4.setBounds(450,300,50,30);
b5.setBounds(170,400,50,30);
b6.setBounds(300,400,50,30);
f1.add(b1);f1.add(b2);f1.add(b3);f1.add(b4);f1.add(b5);f1.add(b6);
f1.show();
}
}
18.
package awt;
import java.awt.*;
//Set TextField
TextField t1=new TextField();
t1.setBounds(150,50,300,30);
f1.add(t1);
//button 16-20
Button b16=new Button("*");
Button b17=new Button("=");
Button b18=new Button("ON");
Button b19=new Button("OFF");
Button b20=new Button("EXIT");