Chinaunix首页 | 论坛 | 博客
  • 博客访问: 77210
  • 博文数量: 22
  • 博客积分: 1250
  • 博客等级: 中尉
  • 技术积分: 305
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-19 09:44
文章分类

全部博文(22)

文章存档

2009年(2)

2008年(20)

我的朋友

分类: Java

2008-11-10 10:58:28

Vectors

No Comment - Post a comment

/* Implementation Of Vectors */
import java.util.Vector;
import java.io.*;
public class MainClass extends Thread{
public static void main(String args[]) throws InterruptedException{
Vector v = new Vector();
int num,choice,ch;
boolean tr = true;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
do {
System.out.println("1. Add Elements\n2. Delete Elements\n3. Display\n4. Exit");
choice = Integer.parseInt(br.readLine());
switch(choice) {
case 1:
System.out.println("Enter Element : ");
v.add(Integer.parseInt(br.readLine()));
break;
case 2:
int no;
System.out.println("Enter Element : ");
no = Integer.parseInt(br.readLine());
if(v.contains(no))
v.removeElement(no);
break;
case 3:
System.out.println(v);
break;
case 4:
System.out.println("Exiting Application....");
Thread.sleep(2000);
System.exit(0);
break;
default:
System.out.println("ERROR!!! INVALID OPTION!!!");
Thread.sleep(2000);
}
System.out.println("Do YOu want to continue (1 = yes: )");
ch = Integer.parseInt(br.readLine()); }
while(ch==1);
} catch(Exception e){
System.out.println("Error Message: "+e);
}
}}
/**** OutPut ******
1. Add Elements
2. Delete Elements
3. Display
4. Exit
1
Enter Element :
4
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
1
Enter Element :
6
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
1
Enter Element :
7
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
1
Enter Element :
5
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
3
[4, 6, 7, 5]
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
2
Enter Element :
7
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
3
[4, 6, 5]
Do YOu want to continue (1 = yes: )
1
1. Add Elements
2. Delete Elements
3. Display
4. Exit
4
Exiting Application.... */

 

Threads

No Comment - Post a comment

/* Implementation Of Threads */

import java.io.*;
import java.util.*;

class AA extends Thread
{
int i;
public void run()
{
System.out.println("Thread A");
for(i=0;i<5;i++)
{
System.out.println("Thread A : "+i);
yield();
}
}
}
class B extends Thread
{
int i;
public void run()
{
System.out.println("Thread B");
for(i=0;i<5;i++)
{
System.out.println("Thread B : "+i);
yield();
}
}

}
class C extends Thread
{
int i;
public void run()
{
System.out.println("Thread C");
for(i=0;i<5;i++)
{
System.out.println("Thread C : "+i);
yield();
}
}
}
class ThreadLionel
{
public static void main(String []args)
{
AA a = new AA();
B b = new B();
C c = new C();
a.start();
b.start();
c.start();
}
}
/*
--------------------OUTPUT--------------------
Thread A
Thread A : 0
Thread A : 1
Thread B
Thread B : 0
Thread C
Thread C : 0
Thread A : 2
Thread B : 1
Thread C : 1
Thread A : 3
Thread B : 2
Thread C : 2
Thread A : 4
Thread B : 3
Thread C : 3
Thread B : 4
Thread C : 4
*/

 

Matrix Operations

No Comment - Post a comment

/********** Implementation of Matrix Operation Using Arrays *********/
// Matrix Addtion, Subtraction, Transpose, Multiplication

import matrix;

class Matrix
{
public static void main(String args[])
{
int i,j,k;
int mat1 [][]={ {1,2,3}, {4,5,6}, {7,8,9} };
int mat2 [][]={ {10,11,12}, {13,14,15}, {16,17,18} };

//Matrix A
System.out.println("\nMatrix A:");
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
System.out.print("\t"+mat1[i][j]);
System.out.println("");
}

//Matrix B
System.out.println("\nMatrix B:");
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
System.out.print("\t"+mat2[i][j]);
System.out.println("");
}
System.out.println("\nOperation ON Matrices \n1.Addition \n");
int sum [][] = new int [3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
System.out.print("\t" + sum[i][j]);
}

System.out.println("");
}

System.out.println("2.Subtraction\n");
int diff[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
diff [i][j] = mat1[i][j] - mat2[i][j];
System.out.print("\t"+ diff[i][j]);
}
System.out.println("");
}

System.out.println("3. Transpose Of A\n");
int trans[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
trans [i][j] = mat1[j][i];
System.out.print("\t"+ trans[i][j]);
}
System.out.println("");
}
System.out.println("4.Multiplication\n");
int prod[][] = new int[3][3];
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
prod[i][j] = 0;
{
for(k=0;k< 3;k++)
prod[i][j] = prod[i][j]+mat1[i][k]*mat2[k][j];
}
System.out.print("\t"+ prod[i][j]);
}
System.out.println("");

}
}
}

/************* OUTPUT ***************



Matrix A:
1 2 3
4 5 6
7 8 9

Matrix B:
10 11 12
13 14 15
16 17 18

Operation ON Matrices
1.Addition

11 13 15
17 19 21
23 25 27
2.Subtraction

-9 -9 -9
-9 -9 -9
-9 -9 -9
3. Transpose Of A

1 4 7
2 5 8
3 6 9
4.Multiplication

84 90 96
201 216 231
318 342 366
*/

 

Packages : Calculation of Simple and compound Interest

No Comment - Post a comment

/* java program : implementation of packges...
Import a package in a class and use it. */

/*
First.java
*/

package pack;

public class First
{
static double amt,sim,comp,princ = 1000,rate = 0.09;
static int n=2;

public First()
{
System.out.println("\n...Welcome to First Class\n");
}

public static void DisplayData()
{
System.out.println("Principle Amount\t:"+princ+"\nRate Of Interest\t:"+rate*100+"%");
System.out.println("Term\t\t\t:"+n);
}
public static void SimpleInterest()
{
System.out.println("\nDisplaying Simple Interest...");
try
{
Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)
}
catch(InterruptedException e)
{
e.printStackTrace();
}
sim = (princ*n*rate);
System.out.println("\tInterest\t: "+sim);
}

public static void CompInterest()
{
System.out.println("\nDisplaying Compound Interest...");
try
{
Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)
}
catch(InterruptedException e)
{
e.printStackTrace();
}
double ans = princ*(1+rate/n);
comp = Math.pow(ans,(double)n*4.0);
System.out.println("\tInterest\t: "+comp);
}
/* public static void main(String []args)
{
First f= new First();
f.DisplayData();
f.SimpleInterest();
f.CompInterest();
}*/
}

/* The socond file imports the first file and calculates the interst*/
/*Second.java*/

import pack.First;

class Second
{
public static void main(String[] args)
{
System.out.println("Running Class Second..");
First first = new First();
first.DisplayData();
first.SimpleInterest();
first.CompInterest();
}
}

/* Output
* Running Class Second..
*
* ...Welcome to First Class
*
* Principle Amount :1000.0
* Rate Of Interest :9.0%
* Term :2
*
* Displaying Simple Interest...
* Interest : 180.0
* Displaying Compound Interest...
* Interest : 1.4221006128366082E24
*/

 

Length Convertor

No Comment - Post a comment

/* (Length Convertor): Simple Java Program to convert miles, kilometers, meter, feet, centimeter and inches
*/
Enter value for Miles: 45

Kilometer: 72.4185
Meter: 72418.5
Feet: 237593.50429815002
Inches: 2851122.0515778
Centimeter: 7241850.011007612

import java.io.*;

public class ConverterIO
{
public static void main(String[] args) throws IOException
{
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
String strChoose;
int choose;
String strMiles, strKilometer, strMeter, strFeet, strInches, strCentimeter;
double miles, kilometer, meter,feet, inches, centimeter;
boolean done = false;
while (!done)
{
//Getting input from the user
println("\n\n\n\t\tLENGTH CONVERTER");
println("\n\tWhat you would like to convert?");
println("");
println("\t1) Miles 4) Feet");
println("\t2) Kilometer 5) Inches");
println("\t3) Meter 6) Centimeter");
println("\t\t 7) Exit");
print("\n\tEnter your choice: ");
strChoose = dataIn.readLine();
choose = Integer.parseInt(strChoose);

if ((choose < = 0) || (choose >= 8))
{
println("\tInvalid entry, please try again!");
}

switch (choose)
{
case 1:
print("\n\tEnter value for Miles: ");
strMiles = dataIn.readLine();
miles = Double.parseDouble(strMiles);
println("\n\t\tKilometer: " + (miles*1.6093));
println("\t\tMeter: " + (miles*1609.3));
println("\t\tFeet: " + (miles*5279.85565107));
println("\t\tInches: " + (miles*63358.26781284));
println("\t\tCentimeter: " + (miles*160930.0002446136));
break;

case 2:
print("\n\tEnter value for Kilometer: ");
strKilometer = dataIn.readLine();
kilometer = Double.parseDouble(strKilometer);
println("\n\t\tMiles: " + (kilometer*0.6213882));
println("\t\tMeter: " + (kilometer*1000));
println("\t\tFeet: " + (kilometer*3280.8399));
println("\t\tInches: " + (kilometer*39370.0788));
println("\t\tCentimeter: " + (kilometer*100000)); break;

case 3:
print("\n\tEnter value for Meter: ");
strMeter = dataIn.readLine();
meter = Double.parseDouble(strMeter);
println("\n\t\tMiles: " + (meter*621.3882));
println("\t\tKilometer: " + (meter*1000));
println("\t\tFeet: " + (meter*3.2808399));
println("\t\tInches: " + (meter*39.3700788));
println("\t\tCentimeter: " + (meter*100));
break;

case 4:
print("\n\tEnter value for Feet: ");
strFeet = dataIn.readLine();
feet = Double.parseDouble(strFeet);
println("\n\t\tMiles: " + (feet*0.0001893991176));
println("\t\tKilometer: " + (feet*0.0003048));
println("\t\tMeter: " + (feet*0.3048));
println("\t\tInches: " + (feet*12));
println("\t\tCentimeter: " + (feet*30.48));
break;

case 5:
print("\n\tEnter value for Inches: ");
strInches = dataIn.readLine();
inches = Double.parseDouble(strInches);
println("\n\t\tMiles: " + (inches*0.000015783254));
println("\t\tKilometer: " + (inches*0.000025349));
println("\t\tMeter: " + (inches*0.02534));
println("\t\tFeet: " + (inches*0.08333));
println("\t\tCentimeter: " + (inches*2.51));
break;

case 6:
print("\n\tEnter value for Centimeter: ");
strCentimeter = dataIn.readLine();
centimeter = Double.parseDouble(strCentimeter);
println("\n\t\tMiles: " + (centimeter*0.000006218797));
println("\t\tKilometer: " + (centimeter*0.000004));
println("\t\tMeter: " + (centimeter*0.004));
println("\t\tFeet: " + (centimeter*0.032808386877));
println("\t\tInches: " + (centimeter*0.3937008));
break;

case 7:
done = true;
println("\n\tThank you for using this program!");
println("\n\n");
break;

default: throw new NumberFormatException();

} // end switch
} // end while
} // end main

//short cut for print
private static void print(String s)
{
System.out.print(s);
}

//short cut for println
private static void println(String s)
{
System.out.println(s);
}
}

/* Output
LENGTH CONVERTER

What you would like to convert?

1) Miles 4) Feet
2) Kilometer 5) Inches
3) Meter 6) Centimeter
7) Exit

Enter your choice: 1

Enter value for Miles: 45

Kilometer: 72.4185
Meter: 72418.5
Feet: 237593.50429815002
Inches: 2851122.0515778
Centimeter: 7241850.011007612


LENGTH CONVERTER

What you would like to convert?

1) Miles 4) Feet
2) Kilometer 5) Inches
3) Meter 6) Centimeter
7) Exit

Enter your choice: 5

Enter value for Inches: 48.5

Miles: 7.65487819E-4
Kilometer: 0.0012294265
Meter: 1.22899
Feet: 4.041505
Centimeter: 121.73499999999999


LENGTH CONVERTER

What you would like to convert?

1) Miles 4) Feet
2) Kilometer 5) Inches
3) Meter 6) Centimeter
7) Exit

Enter your choice: 7

Thank you for using this program!
*/

 

Bubble Sort

4 comments - Post a comment

/* Java program on Arrays - Bubble sort on numbers
*/

import java.io.*;
class Bubble
{
public static void main(String args[]) throws IOException
{
int num;
System.out.println("Program to demonstrate bubble sort");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of elements to be entered <10");
num=Integer.parseInt(br.readLine());
System.out.println("Enter the numbers to be sorted");
int arr[]=new int[10];

for(int i=0;i< num;i++)
{
arr[i]=Integer.parseInt(br.readLine());
}
System.out.println("The number you have entered:");
for(int i=0;i< num;i++)
{
System.out.println(arr[i]);
}
for(int i=0;i< num;i++)
{
for(int j=i+1;j< num;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("The sorted numbers are");
for(int i=0;i< num;i++)
{
System.out.println(arr[i]);
}

}
}


/********* Output **********

Program to demonstrate bubble sort
Enter the number of elements to be entered <10
5
Enter the numbers to be sorted
8
65
14
23
2
The number you have entered:
8
65
14
23
2
The sorted numbers are
2
8
14
23
65

*/

 

Read A File

No Comment - Post a comment

/***** Java Program to read a file and display it on the screen */

import java.io.*;
public class LioFile
{
public static void main(String args[])
{
try
{
FileReader fr=new FileReader("LioFile.java");
BufferedReader br=new BufferedReader(fr);
String str=" ";
String s1 =" ";
System.out.println(" s1 is "+s1.equals("ass"));

while((str=br.readLine())!=null)
{
System.out.println(str);
}
}
catch(FileNotFoundException fe)
{
fe.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}

/**** Output ***
s1 is false
import java.io.*;
public class J7
{
public static void main(String args[])
{
try
{
FileReader fr=new FileReader("J7.java");
BufferedReader br=new BufferedReader(fr);
String str=" ";
String s1 =" ";
System.out.println(" s1 is "+s1.equals("ass"));

while((str=br.readLine())!=null)
{
System.out.println(str);
}
}
catch(FileNotFoundException fe)
{
fe.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}


}
}
*/

 

Function Overloading

No Comment - Post a comment

/* Function Overloading program in java. The program overloads the area() function and calculates the area for square, rectangle and triangle */

class overloading
{
double area,root;
//Area Of Square
public void Area(double a)
{
area = a*a;
System.out.println("The Area Of Square : " +area);
}

// Area of rectangle
public void Area(double a,double b)
{
area= a*b;
System.out.println("The Area Of Rectangle : " +area);
}

// Aera Of Triangle
public void Area(double a,double b,double c)
{
double s= (a+b+c);
root = s*(s-a)*(s-b)*(s-c);
area = Math.sqrt(root);
System.out.println("The Area Of Triangle : " +area);
}

public static void main(String args[])
{
overloading ar = new overloading();
ar.Area(5.65);
ar.Area(5.32,43.2);
ar.Area(4,7,7);
}
}
/* Output
The Area Of Square : 31.922500000000003
The Area Of Rectangle : 229.82400000000004
The Area Of Triangle : 174.61958653026298 */

 

Type Casting

No Comment - Post a comment

/* Simple Type casting program */
class cast
{
public static void main(String args[])
{
double a=5.35, b=55.6;
int rem;
rem = (int)a% (int)b;
System.out.println("Remainder : "+rem);
}
}

/* Output
Remainder : 5 */

阅读(803) | 评论(0) | 转发(0) |
0

上一篇:Enum Types

下一篇:java bmp图片转成png

给主人留下些什么吧!~~