1.write A Java Program For Sorting A Given List of Names in Ascending Order Using Command Line Arguments

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 32

1.

Write a java program for sorting a given list of names in ascending order using
command line arguments
import java.lang.*;
import java.io.*;
import java.util.*;
class Sort
{
public static void main(String arg[ ]) throws IOException
{
DataInputStream dis=new DataInputStream(System.in);
System.out.println("enter the size");
int n=Integer.parseInt(dis.readLine());
String array[ ]=new String[n];
System.out.println("enter names");
for(int i=0;i{
array[i]=dis.readLine();
}
for(int i=0;i{
for(int j=0;j{
if(array[j].compareTo(array[j+i])>0)
{
String temp=array[j];
array [j]=array[j+1];
array[j+1]=temp;
}
}
}

System.out.println("the sorted strings are");


for(int i=0;iSystem.out.println(array[i]);
}
}

Output
Java sorting Harish Ramesh Mahesh Rakesh
Sorted order is
Ramesh
Rakesh
Mahesh
Harish
Java sorting sai hari teja ravi sandeep
Sorted order is
teja
sandeep
sai
ravi
hari

2. Write a java program to multiply two given matrices


import java.util.Scanner;
class matmul
{
public static void main(String args[])
{
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
System.out.println("Enter the first matrix:");
Scanner input=new Scanner(System.in);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j]=input.nextInt();
System.out.println("Enter the second matrix:");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
b[i][j]=input.nextInt();
System.out.println("Matrix multiplication is as follows:");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=0;i<3;i++)
{

for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println("\n");
}
System.out.println("\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.println("\n");
}
System.out.println("\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.println("\n");
}
}
}

Output:
Enter the first matrix:
123456789

Enter the second matrix:


987654321

Matrix multiplication is as follows:


1

30

24

18

84

69

54

138

114

90

3. Program Illustrating Overloading & Overriding methods in Java


Method overloading
//dynamic polymorphism
class Sum
{
void add(int a, int b)
{
System.out.println("Sum of two="+(a+b));
}

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


{
System.out.println("Sum of three="+(a+b+c));
}
}
class Polymorphism
{
public static void main(String args[])
{
Sum s=new Sum();

s.add(10,15);
s.add(10,20,30);
}
}
Output:
Sum of two=25
Sum of three=60

Method overriding
//dynamic polymorphism
class A
{
void cal(double x)
{
System.out.println("square value="+(x*x));
}
}
class B extends A
{
void cal(double x)
{
System.out.println("square root="+Math.sqrt(x));
}
}
class Polymorphism
{
public static void main(String args[])
{
A a=new A();

a.cal(15);
}
}

Output:Square value=225.0

4. Programs Illustrating the Implementation of Various Forms of Inheritance

5. Program which illustrates the implementation of multiple Inheritance using


interfaces in java
import java.io.*;
interface bookinfo
{

public void getbookdata()throws IOException;


public void putbookdata();

}
interface basicinfo
{

public void getbasicdata()throws IOException;


public void putbasicdata();

}
interface otherinfo
{

public void getotherdata()throws IOException;


public void putotherdata();

class book implements bookinfo,basicinfo,otherinfo


{

String Bname,Bauthor, Blanguage,Bpublisher;


int Bprice, Bcopies,Byear;
BufferedReader br=new BufferedReader(new

InputStreamReader(System.in));

public void getbookdata()throws IOException


{

System.out.println("Enter the Book name:");


Bname=br.readLine();
System.out.println("Enter the author's name:");
Bauthor=br.readLine();

public void putbookdata()


{ System.out.print(Bname+"\t"+Bauthor+"\t");
}

public void getbasicdata()throws IOException


{

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


Blanguage=br.readLine();

System.out.println("Enter the published year:");


Byear=Integer.parseInt(br.readLine());
System.out.println("Enter the price");
Bprice=Integer.parseInt(br.readLine());
}

public void putbasicdata()


{

System.out.print(Blanguage+"\t\t"+Byear+"\t"+Bprice+"\t");

public void getotherdata()throws IOException


{

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


Bcopies=Integer.parseInt(br.readLine());
System.out.println("Enter the publisher:");
Bpublisher=br.readLine();

public void putotherdata()


{

System.out.print(Bcopies+"\t"+Bpublisher+"\t\t");

}
}

class interfaceEg
{
public static void main(String s[])throws IOException
{
book it[];
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("How much entries..?");
int k=Integer.parseInt(br.readLine());
it=new book[k];
for(int i=0;i<k;i++)
{ it[i]=new book();
}

for(int i=0;i<k;i++)
{
int j=i+1;
System.out.println("ENTER DETAILS OF THE BOOK : "+j);
it[i].getbookdata();
it[i].getbasicdata();
it[i].getotherdata();

System.out.println("\n\n***********-----------------Book
details-----------------************\n");
System.out.println("Book\tAuthor\tLanguage\tYear\tPrice\tCopies
\tPublisher");
System.out.println("............................................................................");
for(int i=0;i<k;i++)
{

it[i].putbookdata();
it[i].putbasicdata();
it[i].putotherdata();
}
}
}

Output:

6. Program illustrates the implementation of abstract class

7. Programs to Create Package in Java

package folder;
import java.io.*;
public class big
{
public int empno;
public String empname=new String( );
public String designation=new String( );
public int basicpay;
public double hra,da,pf,lic,grosspay,netpay;
public void getData( )
{
try
{
DataInputStream ds=new DataInputStream(System.in);
System.out.println("Enter the Employee number: ");
empno=Integer.parseInt(ds.readLine( ));
System.out.println("Enter the Employee name: ");
empname=ds.readLine( );
System.out.println("Designation: ");
designation=ds.readLine( );
System.out.println("Enter Basicpay: ");
basicpay=Integer.parseInt(ds.readLine( ));
}
catch(Exception e)
{}
}
public void Calculate( )
{
if(designation.equals("Engineer"))
{
hra=basicpay*0.20;
da=basicpay*0.15;
pf=basicpay*0.12;
lic=basicpay*0.25;
}
else if(designation.equals("Analyst"))
{
hra=basicpay*0.20;
da=basicpay*0.15;
pf=basicpay*0.12;
lic=basicpay*0.25;
}
else
{
hra=basicpay*0.10;
da=basicpay*0.14;

pf=basicpay*0.12;
lic=basicpay*0.10;
}
grosspay=basicpay+hra+da;
netpay=grosspay-(pf+lic);
}
public void Display( )
{
System.out.println("Employee number: "+empno);
System.out.println("Employee name: "+empname);
System.out.println("Designation: "+designation);
System.out.println("Basicpay: "+basicpay);
System.out.println("HRA: "+hra);
System.out.println("DA: "+da);
System.out.println("PF: "+pf);
System.out.println("LIC: "+lic);
System.out.println("Grosspay: "+grosspay);
System.out.println("Netpay: "+netpay);
}
}
MAIN FUNCTION
----------------------import floder.big;
public class emp
{
public static void main(String args[ ])
{
System.out.println(EMPLOYEE SALARY DETAILS);
System.out.println(****************************);
big ob=new big( );
ob.getData( );
ob.Calculate( );
ob.Display( );
}
}

OUTPUT:
RUN 1:
C:\jdk1.3\bin\ct>javac emp1.java
C:\jdk1.3\bin\ct>java emp1
EMPLOYEE SALARY DETAILS
****************************
Enter the Employee number:
25
Enter the Employee name:
Rajesh
Designation:
Engineers
Enter Basicpay:
1250
Employee number: 25
Employee name: Rajesh
Designation: Engineer
Basicpay: 1250
HRA: 250.0
DA: 187.5
PF: 150.0
LIC: 312.5
Grosspay: 1687.5
Netpay: 1225.0

RUN 2:
C:\jdk1.3\bin\ct>javac emp1.java
C:\jdk1.3\bin\ct>java emp1
EMPLOYEE SALARY DETAILS
****************************
Enter the Employee number:
26
Enter the Employee name:
Ravi
Designation:
Analyst
Enter Basicpay:
875
Employee number: 26
Employee name: Ravi
Designation: Analyst
Basicpay: 875
HRA: 175.0
DA: 131.25
PF: 105.0
LIC: 218.75
Grosspay: 1181.25
Netpay: 857.5

RUN 3:
C:\jdk1.3\bin\ct>javac emp1.java
C:\jdk1.3\bin\ct>java emp1
EMPLOYEE SALARY DETAILS
****************************
Enter the Employee number:
35
Enter the Employee name:
Raja
Designation: Superviser
Enter Basicpay:
1000
Employee number: 35
Employee name: Raja
Designation: Supervisor
Basicpay: 1000
HRA: 100.0
DA: 140.0
PF: 120.0
LIC: 100.0
Grosspay: 1240.0
Netpay: 1020.0

8. Program to create multiple threads in Java


class NewThread implements Runnable
{
String name;
Thread t;
NewThread(String threadname)
{
name=threadname;
t=new Thread(this,name);
System.out.println("New Thread:"+t);
t.start();
}
public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.println(name+ ":"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(name+" Interrupted");
}
System.out.println(name+" exiting");

}
}
class MultiThreadDemo
{

public static void main(String[] args)


{
new NewThread("One");
new NewThread("Two");
new NewThread("Three");
try
{
Thread.sleep(10000);
}
catch(InterruptedException e)
{
System.out.println("Main Thread Interrupted");
}
System.out.println("Main Thread Exiting");
}
}

Output:
New Thread :Thread[One,5,main]
New Thread : Thread[Two,5,main]
One:5
Two:5
New Thread : Thread[Three,5,main]
Three:5
One:4
Three:4
Two:4
One:3
Three:3
Two:3
One:2
Three:2
Two:2
One:1
Three:1
Two:1
One exiting
Three exiting
Two exiting
Main Thread Exiting

9. Program to Implement Producer / Consumer Problem using


synchronization.
public class ProducerConsumerTest {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
p1.start();
c1.start();
}
}
class CubbyHole {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
}
catch (InterruptedException e) {
}
}
available = false;

notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
}
catch (InterruptedException e) {
}
}
contents = value;
available = true;
notifyAll();
}
}

class Consumer extends Thread {


private CubbyHole cubbyhole;
private int number;
public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;

}
public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = cubbyhole.get();
System.out.println("Consumer #"
+ this.number
+ " got: " + value);
}
}
}

class Producer extends Thread {


private CubbyHole cubbyhole;
private int number;

public Producer(CubbyHole c, int number) {


cubbyhole = c;
this.number = number;
}

public void run() {


for (int i = 0; i < 10; i++) {

cubbyhole.put(i);
System.out.println("Producer #" + this.number
+ " put: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}

Output:
Producer #1 put: 0
Consumer #1 got: 0
Producer #1 put: 1
Consumer #1 got: 1
Producer #1 put: 2
Consumer #1 got: 2
Producer #1 put: 3
Consumer #1 got: 3
Producer #1 put: 4
Consumer #1 got: 4
Producer #1 put: 5
Consumer #1 got: 5
Producer #1 put: 6
Consumer #1 got: 6
Producer #1 put: 7
Consumer #1 got: 7
Producer #1 put: 8
Consumer #1 got: 8
Producer #1 put: 9
Consumer #1 got: 9

10. Program to write Applets to draw the various polygons


import java.awt.*;
import java.applet.*;
public class Face extends Applet
{
public void paint(Graphics g)
{
g.drawOval(40,40,120,150);
g.drawOval(57,75,30,20);
g.drawOval(110,75,30,20);
g.fillOval(68,81,10,10);
g.fillOval(121,81,10,10);
g.drawOval(85,100,30,30);
g.fillArc(60,125,80,40,180,180);
g.drawOval(25,92,15,30);
g.drawOval(160,92,15,30);
}
}

/*<applet code="Face.class" height=500 width=400></applet>*/

Output:

C:\jdk1.3\bin\ct>javac Face.java
C:\java1.3\bin\ct>appletviewer Face.java

11. Create and Manipulate Labels, Lists, Text Fields, Text Areas & Panels

12. Handling Mouse Event & Keyboard Events.

You might also like