Matrix Operations

1 comments - 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

2 comments - 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

3 comments - 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

6 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

3 comments - 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();
}


}
}
*/