Java Program prints ip address

Java Program prints ip address | IP address Printing using Java



import java.net.InetAddress;

class IPAddress
{
public static void main(String args[]) throws Exception
{
System.out.println(InetAddress.getLocalHost());
}
}

Reverse number in Java || Java Reverse Given Number

Reverse number in Java || Java Reverse Given Number

import java.util.Scanner;   class ReverseNumber {    public static void main(String args[])    {       int n, reverse = 0;         System.out.println("Enter the number to reverse");       Scanner in = new Scanner(System.in);       n = in.nextInt();         while( n != 0 )       {           reverse = reverse * 10;           reverse = reverse + n%10;           n = n/10;       }         System.out.println("Reverse of entered number is "+reverse);    } }

add two matrices using Java | Addition of two matrices in Java

add two matrices using Java | Addition of two matrices in Java

import java.util.Scanner;   class AddTwoMatrix {    public static void main(String args[])    {       int m, n, c, d;       Scanner in = new Scanner(System.in);         System.out.println("Enter the number of rows and columns of matrix");       m = in.nextInt();       n = in.nextInt();         int first[][] = new int[m][n];       int second[][] = new int[m][n];       int sum[][] = new int[m][n];         System.out.println("Enter the elements of first matrix");         for (  c = 0 ; c < m ; c++ )          for ( d = 0 ; d < n ; d++ )             first[c][d] = in.nextInt();         System.out.println("Enter the elements of second matrix");         for ( c = 0 ; c < m ; c++ )          for ( d = 0 ; d < n ; d++ )             second[c][d] = in.nextInt();         for ( c = 0 ; c < m ; c++ )          for ( d = 0 ; d < n ; d++ )              sum[c][d] = first[c][d]+ second[c][d];         System.out.println("Sum of entered matrices:-");         for ( c = 0 ; c < m ; c++ )       {          for ( d = 0 ; d < n ; d++ )             System.out.print(sum[c][d]+"\t");            System.out.println();       }    } }

transpose matrix Program in Java | transpose matrix JAVA, Core Java transpose matrix Assignment

transpose matrix Program in Java | transpose matrix JAVA, Core Java transpose matrix Assignment

import java.util.Scanner;

class TransposeAMatrix
{
public static void main(String args[])
{
int m, n, c, d;

Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();

int matrix[][] = new int[m][n];

System.out.println("Enter the elements of matrix");

for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) matrix[c][d] = in.nextInt(); int transpose[][] = new int[n][m]; for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) transpose[d][c] = matrix[c][d]; } System.out.println("Transpose of entered matrix:-"); for ( c = 0 ; c < n ; c++ ) { for ( d = 0 ; d < m ; d++ ) System.out.print(transpose[c][d]+"\t"); System.out.print("\n"); } } }

java program multiply two matrices

java program multiply two matrices



import java.util.Scanner;

class MatrixMultiplication
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, c, d, k;

Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of first matrix");
m = in.nextInt();
n = in.nextInt();

int first[][] = new int[m][n];

System.out.println("Enter the elements of first matrix");

for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
first[c][d] = in.nextInt();

System.out.println("Enter the number of rows and columns of second matrix");
p = in.nextInt();
q = in.nextInt();

if ( n != p )
System.out.println("Matrices with entered orders can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];

System.out.println("Enter the elements of second matrix");

for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
second[c][d] = in.nextInt();

for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

System.out.println("Product of entered matrices:-");

for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
System.out.print(multiply[c][d]+"\t");

System.out.print("\n");
}
}
}
}


FileDumper Class in java

FileDumper Class in java

import java.io.*; import com.elharo.io.*;  public class FileDumper {    public static final int ASC = 0;   public static final int DEC = 1;   public static final int HEX = 2;    public static void main(String[] args) {      if (args.length < 1) {       System.err.println("Usage: java FileDumper [-ahd] file1 file2...");       return;     }      int firstArg = 0;     int mode = ASC;      if (args[0].startsWith("-")) {       firstArg = 1;       if (args[0].equals("-h")) mode = HEX;       else if (args[0].equals("-d")) mode = DEC;     }      for (int i = firstArg; i < args.length; i++) {       try {         if (mode == ASC) dumpAscii(args[i]);         else if (mode == HEX) dumpHex(args[i]);         else if (mode == DEC) dumpDecimal(args[i]);          }         catch (IOException ex) {         System.err.println("Error reading from " + args[i] + ": "           + ex.getMessage());       }        if (i < args.length-1) {  // more files to dump         System.out.println("\r\n--------------------------------------\r\n");       }     }   }    public static void dumpAscii(String filename) throws IOException {      FileInputStream fin = null;     try {       fin = new FileInputStream(filename);       StreamCopier.copy(fin, System.out);     }     finally {       if (fin != null) fin.close();     }   }    public static void dumpDecimal(String filename) throws IOException {      FileInputStream fin = null;     byte[] buffer = new byte[16];     boolean end = false;      try {       fin = new FileInputStream(filename);       while (!end) {         int bytesRead = 0;         while (bytesRead < buffer.length) {           int r = fin.read(buffer, bytesRead, buffer.length - bytesRead);           if (r == -1) {             end = true;             break;           }           bytesRead += r;         }         for (int i = 0; i < bytesRead; i++) {           int dec = buffer[i];           if (dec < 0) dec = 256 + dec;           if (dec < 10) System.out.print("00" + dec + " ");           else if (dec < 100) System.out.print("0" + dec + " ");           else System.out.print(dec + " ");         }         System.out.println();       }     }     finally {       if (fin != null) fin.close();     }   }    public static void dumpHex(String filename) throws IOException {      FileInputStream fin = null;     byte[] buffer = new byte[24];     boolean end = false;      try {       fin = new FileInputStream(filename);       while (!end) {         int bytesRead = 0;         while (bytesRead < buffer.length) {           int r = fin.read(buffer, bytesRead, buffer.length - bytesRead);           if (r == -1) {             end = true;             break;           }           bytesRead += r;         }         for (int i = 0; i < bytesRead; i++) {           int hex = buffer[i];           if (hex < 0) hex = 256 + hex;           if (hex >= 16) System.out.print(Integer.toHexString(hex) + " ");           else System.out.print("0" + Integer.toHexString(hex) + " ");         }         System.out.println();       }     }     finally {       if (fin != null) fin.close();     }   } }

FileCopier Class in java

FileCopier Class in java

import java.io.*; import com.elharo.io.*;  public class FileCopier {    public static void main(String[] args) {      if (args.length != 2) {       System.err.println("Usage: java FileCopier infile outfile");     }     try {       copy(args[0], args[1]);     }     catch (IOException ex) {       System.err.println(ex);     }   }    public static void copy(String inFile, String outFile)     throws IOException {      FileInputStream  fin = null;     FileOutputStream fout = null;          try {       fin  = new FileInputStream(inFile);       fout = new FileOutputStream(outFile);       StreamCopier.copy(fin, fout);     }     finally {       try {         if (fin != null) fin.close();       }       catch (IOException ex) {       }       try {         if (fout != null) fout.close();        }       catch (IOException ex) { }     }   } }

FileTyper class in java

FileTyper class in java

import java.io.*; import com.elharo.io.*;  public class FileTyper {    public static void main(String[] args) throws IOException {     if (args.length != 1) {       System.err.println("Usage: java FileTyper filename");       return;     }     typeFile(args[0]);   }    public static void typeFile(String filename) throws IOException {     FileInputStream fin = new FileInputStream(filename);     try {         StreamCopier.copy(fin, System.out);     }     finally {       fin.close();     }   } }

Program using NullOutputStream Class in java

Program using NullOutputStream Class in java

package com.elharo.io;

import java.io.*;

public class NullOutputStream extends OutputStream {

private boolean closed = false;

public void write(int b) throws IOException {
if (closed) throw new IOException("Write to closed stream");
}

public void write(byte[] data, int offset, int length)
throws IOException {
if (data == null) throw new NullPointerException("data is null");
if (closed) throw new IOException("Write to closed stream");
}

public void close() {
closed = true;
}
}

Java Program to show AsciiArray class

Java Program to show AsciiArray class

import java.io.*;

public class AsciiArray {

public static void main(String[] args) {

byte[] b = new byte[(127-31)*2];
int index = 0;
for (int i = 32; i < 127; i++) {
b[index++] = (byte) i;
// Break line after every eight characters.
if (i % 8 == 7) b[index++] = (byte) '\n';
else b[index++] = (byte) '\t';
}
b[index++] = (byte) '\n';
try {
System.out.write(b);
}
catch (IOException ex) {
System.err.println(ex);
}
}
}

StreamCopier class in Java

StreamCopier class in Java

package com.elharo.io;

import java.io.*;

public class StreamCopier {

public static void main(String[] args) {

try {
copy(System.in, System.out);
}
catch (IOException ex) {
System.err.println(ex);
}

}

public static void copy(InputStream in, OutputStream out)
throws IOException {

byte[] buffer = new byte[1024];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);
}

}

}

Program for RandomInputStream Class in Java

Program for RandomInputStream Class in Java

package com.elharo.io;

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

public class RandomInputStream extends InputStream {

private Random generator = new Random();
private boolean closed = false;

public int read() throws IOException {
checkOpen();
int result = generator.nextInt() % 256;
if (result < 0) result = -result;
return result;
}

public int read(byte[] data, int offset, int length) throws IOException {
checkOpen();
byte[] temp = new byte[length];
generator.nextBytes(temp);
System.arraycopy(temp, 0, data, offset, length);
return length;

}

public int read(byte[] data) throws IOException {
checkOpen();
generator.nextBytes(data);
return data.length;

}

public long skip(long bytesToSkip) throws IOException {
checkOpen();
// It's all random so skipping has no effect.
return bytesToSkip;
}

public void close() {
this.closed = true;
}

private void checkOpen() throws IOException {
if (closed) throw new IOException("Input stream closed");
}

public int available() {
// Limited only by available memory and the size of an array.
return Integer.MAX_VALUE;
}
}

StreamPrinter Class in Java

StreamPrinter Class in Java

import java.io.*;

public class StreamPrinter {

public static void main(String[] args) {

try {
while (true) {
int datum = System.in.read();
if (datum == -1) break;
System.out.println(datum);
}
}
catch (IOException ex) {
System.err.println("Couldn't read from System.in!");
}
}
}

Program to Print ASCII values in Java

Program to Print ASCII values in Java



import java.io.*;

public class AsciiChart
{

public static void main(String[] args) {

for (int i = 32; i < 127; i++) {
System.out.write(i);
// break line after every eight characters.
if (i % 8 == 7) System.out.write('\n');
else System.out.write('\t');
}
System.out.write('\n');
}
}

Creating Threads in Java

public class ThreadDemo{
public static void main(String args[]){
Thread t1= new Thread();
t1=Thread.currentThread();
System.out.println("Thread Name : "+t1.getName());
System.out.println("Thread Priority : "+t1.getPriority());
System.out.println("Thread is Alive : "+t1.isAlive());
System.out.println("Threa is Daemon : "+t1.isDaemon());
t1.setName("Avinash");
t1.setPriority(9);
System.out.println("Thread Name : "+t1.getName());
System.out.println("Thread Priority : "+t1.getPriority());
try{
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(" "+j);
Thread.sleep(200);
}
System.out.println();
Thread.sleep(1000);
}
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
}

Creating Menu in JAVA with GUI Interface

Creating Menu in JAVA with GUI Interface


import java.awt.*;
import java.awt.event.*;
public class MenuDemo extends Frame{
private MenuBar mb;
private Menu m1,m2,m3,m4,m5;
private MenuItem ne,op,sa,ss,ps,pr,ex;
private MenuItem un,cp,ct,pa,del,fn,sl;
private MenuItem font,ww;
private MenuItem st,tl;
private MenuItem ht,an;
public MenuDemo(){
mb=new MenuBar();
m1=new Menu("File");
m2=new Menu("Edit");
m3=new Menu("Format");
m4=new Menu("View");
m5=new Menu("Help");
ne=new MenuItem("New");
op=new MenuItem("Open...");
sa=new MenuItem("Save");
ss=new MenuItem("Save As...");
ps=new MenuItem("Page Setup...");
pr=new MenuItem("Print...");
ex=new MenuItem("Exit");
un=new MenuItem("Undo");
cp=new MenuItem("Copy");
ct=new MenuItem("Cut");
ps=new MenuItem("Paste");
del=new MenuItem("Delete");
fn=new MenuItem("Find...");
sl=new MenuItem("Select All");
st=new MenuItem("Status Bar");
tl=new MenuItem("ToolBar");
ht=new MenuItem("Help Topics...");
an=new MenuItem("About NotePad...");

setMenuBar(mb);

mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);
mb.add(m5);
setBounds(45,45,500,400);
setLayout(null);
setVisible(true);
}
public static void main(String args[]){
new MenuDemo();
}
}

Creating GUI List in Java Listdemo

import java.awt.*;
import java.awt.event.*;
public class Listdemo extends Frame
{
List Demo=new List(5,false);
public Listdemo(){
setLayout(null);
setBounds(50,50,400,400);
setVisible(true);
}
public void initcomp()
{
Demo.add("red");
Demo.setBounds(50,50,100,100);
Demo.setVisible(true);
Demo.add("green");
Demo.add("blue");
add(Demo);
Demo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String s1=Demo.getSelectedItem();
if(s1=="red"){
setBackground(Color.red);
}
else if(s1=="green"){
setBackground(Color.green);
}
else if(s1=="blue"){
setBackground(Color.blue);
}
}
});
}
public static void main(String args[]){
new Listdemo();
Listdemo l1=new Listdemo();
l1.initcomp();
}
}

Two Listener Example In Java

import java.awt.*;
import java.awt.event.*;
public class TwoListener implements MouseMotionListener,MouseListener{
private Frame f;
private TextField tf;


public TwoListener(){
f=new Frame("Two Listener Example");
tf=new TextField(30);
}
public void launchFrame(){
Label label=new Label("Click and drag the mouse");
f.add(label,BorderLayout.NORTH);
f.add(tf,BorderLayout.SOUTH);
f.addMouseMotionListener(this);
f.addMouseListener(this);
f.setSize(300,200);
f.setVisible(true);
}
public void mouseDragged(MouseEvent e){
String s="Mouse dragging : X ="+e.getX()+"Y = "+e.getY();
tf.setText(s);
}
public void mouseEntered(MouseEvent e){
String s="The Mouse Entered";
tf.setText(s);
}
public void mouseExited(MouseEvent e){
String s="The mouse has Left the Building";
tf.setText(s);
}
public void mouseMoved(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public static void main(String args[]){
TwoListener two=new TwoListener();
two.launchFrame();
}

}

KeyboardInput Example In Java

import java.io.*;
public class KeyboardInput{
public static void main(String args[]){
String s;
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader in =new BufferedReader(ir);
System.out.println("Unix : Type ctrl-d to exit."+"\n Windows :ctrl-z to exit .");
try{
s=in.readLine();
while(s!=null){
System.out.println("Read : "+s);
s=in.readLine();
}
in.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}

ButtonHandler Example in Java

import java.awt.event.*;
public class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent ae){
System.out.println("Action Performed");
System.out.println("Button's command is : "+ae.getActionCommand());


}

}

We Are Founder..