import java.net.InetAddress;
class IPAddress
{
public static void main(String args[]) throws Exception
{
System.out.println(InetAddress.getLocalHost());
}
}
Java Program prints ip address
Java Program prints ip address | IP address Printing using Java
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;
}
}
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);
}
}
}
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);
}
}
}
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;
}
}
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!");
}
}
}
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');
}
}
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');
}
}
Subscribe to:
Posts (Atom)