0% found this document useful (0 votes)
57 views13 pages

FILE

The document contains 14 code snippets showing examples of Java programs that demonstrate various programming concepts like conditional statements, loops, methods, inheritance, command line arguments, and more.

Uploaded by

Divya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views13 pages

FILE

The document contains 14 code snippets showing examples of Java programs that demonstrate various programming concepts like conditional statements, loops, methods, inheritance, command line arguments, and more.

Uploaded by

Divya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

1.Write a program to check whether the given two numbers in the program are equal.

If not, then find the greater of the given two numbers

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{

public static void main(String args[])


{
float sub1=77,sub2=90,sub3=80,sum,avg,per;

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.

public class large


{
public static void main(String args[])
{
int a=15,b=12,c=21;
if(a>b && a>c)
System.out.println(+a+" is larger than "+c+" and "+b);
else if(b>a && b>c)
System.out.println(+b+" is larger than "+c+" and "+a);
else
System.out.println(+c+" is larger than "+a+" and "+b);
}
}

Output:

4.Write a program to find the factorial of a number 10.

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:

6 Write a program to print the following the pattern

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{

public static void main(String args[])


{
int p=1;
for(int i=1;i<=4;i++){
p=2*p;
}
System.out.println("Fourth power of 2="+p);
}
}
Output:

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);
}

void add(int a,int b ,int c){


res=a+b+c;
System.out.println("Sum of (4,5,3) intergers="+res);
}

void add(double a,double b){


double res=a+b;
System.out.println("Sum of (3.5,4.0) doubles="+res);
}

void add(String a,String b){


String res=a+b;
System.out.println("String Concatenation="+res);
}

public static void main(String args[]){


over o1=new over();
o1.add(4,5);
o1.add(4,5,3);
o1.add(3.5,4.0);
o1.add("Today is "," Monday");
}
}

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);
}

Rectangle(double length ,double breadth){


System.out.println("It is a Rectangle with Length ="+length+"and
breadth="+breadth);
System.out.println("It's Area="+length*breadth);
}

public static void main(String args[]){


Rectangle r1=new Rectangle(4);
Rectangle r2=new Rectangle(4,5);
}
}

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:

14. Write a program to demonstrate the inheritance.

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);
}

public static void main(String args[]){


Shape s1=new Shape();
s1.getCoord(100,200);
s1.showCoord();
}
}

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;

s1="Event Handlers is dedicated";


s2="to make your event";
s3="a most memorable one Madam.";
s4=s1+s2+s3;
System.out.println("Concatenated String = "+s4);
System.out.println("Concatenated String Length= "+s4.length());
System.out.println(" e character index after leaving 3 characters =
"+s4.indexOf("e",3));
System.out.println("Uppercase letters A = "+s4.replace("a","A"));
System.out.println("String ends with Madam= "+s4.endsWith("Madam"));
s5=s4.substring(66,71);
s5.equals()
System.out.println();
}

17.Using awt design


package awt;
import java.awt.*;

public class exercise1 {


public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f1=new Frame();
f1.setSize(600,500);
f1.setTitle("Result");
f1.setLayout(null);
//Set Label
Label l1=new Label("Enter first Number ");
Label l2=new Label("Enter second Number ");
Label l3=new Label("RESULT");
l1.setBounds(50,50,200,30);
l2.setBounds(50,100,200,30);
l3.setBounds(50,150,200,30);
f1.add(l1); f1.add(l2);f1.add(l3);

//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.*;

public class exercise3 {


public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f1=new Frame();
f1.setSize(600,500);
f1.setTitle("CALCULATOR");
f1.setLayout(null);

//Set TextField
TextField t1=new TextField();
t1.setBounds(150,50,300,30);
f1.add(t1);

//add buttons 0-4


Button b1=new Button("0");
Button b2=new Button("1");
Button b3=new Button("2");
Button b4=new Button("3");
Button b5=new Button("4");
//SetBounds
b1.setBounds(150,80,60,30);
b2.setBounds(210,80,60,30);
b3.setBounds(270,80,60,30);
b4.setBounds(330,80,60,30);
b5.setBounds(390,80,60,30);

//add buttons 5-9


Button b6=new Button("5");
Button b7=new Button("6");
Button b8=new Button("7");
Button b9=new Button("8");
Button b10=new Button("9");
//SetBounds
b6.setBounds(150,110,60,30);
b7.setBounds(210,110,60,30);
b8.setBounds(270,110,60,30);
b9.setBounds(330,110,60,30);
b10.setBounds(390,110,60,30);

//add buttons Operators


Button b11=new Button(".");
Button b12=new Button("00");
Button b13=new Button("+");
Button b14=new Button("-");
Button b15=new Button("/");
//SetBounds
b11.setBounds(150,140,60,30);
b12.setBounds(210,140,60,30);
b13.setBounds(270,140,60,30);
b14.setBounds(330,140,60,30);
b15.setBounds(390,140,60,30);

//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");

You might also like