exo-preferred-applicationssobota, 20 września 2014
java - how to check execution time.
package practice;
public class dateTime
{
public static void main (String[] args)
{
long t1 = System.currentTimeMillis();
//something complicated
for (int i =0; i<1E6; i++)
{
double x = Math.pow(Math.random(), Math.random());
}
//end operation
long t2 = System.currentTimeMillis();
System.out.println((t2-t1)/1000.0 + " sec ");
}
}
public class dateTime
{
public static void main (String[] args)
{
long t1 = System.currentTimeMillis();
//something complicated
for (int i =0; i<1E6; i++)
{
double x = Math.pow(Math.random(), Math.random());
}
//end operation
long t2 = System.currentTimeMillis();
System.out.println((t2-t1)/1000.0 + " sec ");
}
}
piątek, 19 września 2014
Java localhost port scanner
threadNetwork.java
package threads;
public class threadNetwork
{
private static String host;
public static void main(String[] args)
{
host = "localhost";
for (int i = 22; i < 65356; i++)
{
portThread t = new portThread(host, i);
t.start();
}
}
}
portThread.java
package threads;
import java.net.Socket;
public class portThread extends Thread
{
private String host;
private int port;
public portThread(String host, int port)
{
this.host = host;
this.port = port;
}
public void run()
{
try
{
Socket socket = new Socket(host, port);
System.out.println("Port: "+ port + " is open...");
socket.close()
}
catch (Exception e)
{
//System.out.println("Port: "+ port + " is not in use");
}
}
}
package threads;
public class threadNetwork
{
private static String host;
public static void main(String[] args)
{
host = "localhost";
for (int i = 22; i < 65356; i++)
{
portThread t = new portThread(host, i);
t.start();
}
}
}
portThread.java
package threads;
import java.net.Socket;
public class portThread extends Thread
{
private String host;
private int port;
public portThread(String host, int port)
{
this.host = host;
this.port = port;
}
public void run()
{
try
{
Socket socket = new Socket(host, port);
System.out.println("Port: "+ port + " is open...");
socket.close()
}
catch (Exception e)
{
//System.out.println("Port: "+ port + " is not in use");
}
}
}
sobota, 6 września 2014
Java - simple gui example program
package gui;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class myWindow extends JFrame implements ActionListener
{
JButton bGetDate, bExit, bSysCheck;
JLabel lshowDate;
private static final long serialVersionUID = 1L;
public myWindow()
{
setSize(400,300);
setTitle("My Stratus");
setLayout(null);
bGetDate = new JButton("Date");
bGetDate.setBounds(50, 50, 100, 20);
add(bGetDate);
bGetDate.addActionListener(this);
bSysCheck = new JButton("SysChceck");
bSysCheck.setBounds(150, 50, 100, 20);
add(bSysCheck);
bSysCheck.addActionListener(this);
bExit = new JButton("Exit");
bExit.setBounds(250, 50, 100, 20);
add(bExit);
bExit.addActionListener(this);
lshowDate = new JLabel("Date: ");
lshowDate.setBounds(50, 100, 200, 20);
lshowDate.setForeground(Color.RED);
lshowDate.setFont(new Font("SansSerif",Font.BOLD, 10));
add(lshowDate);
}
public static void main(String[] args)
{
myWindow window = new myWindow();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object src = e.getSource();
if(src==bExit)
{
dispose();
}
else if(src==bGetDate)
{
//System.out.println(new Date());
lshowDate.setText(new Date().toString());
}
else if(src==bSysCheck)
{
System.out.print("sys details: ");
Runtime r = Runtime.getRuntime();
Process p;
try {
p = r.exec("uname -a");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = b.readLine()) != null)
{
System.out.println(line);
}
} catch (IOException | InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class myWindow extends JFrame implements ActionListener
{
JButton bGetDate, bExit, bSysCheck;
JLabel lshowDate;
private static final long serialVersionUID = 1L;
public myWindow()
{
setSize(400,300);
setTitle("My Stratus");
setLayout(null);
bGetDate = new JButton("Date");
bGetDate.setBounds(50, 50, 100, 20);
add(bGetDate);
bGetDate.addActionListener(this);
bSysCheck = new JButton("SysChceck");
bSysCheck.setBounds(150, 50, 100, 20);
add(bSysCheck);
bSysCheck.addActionListener(this);
bExit = new JButton("Exit");
bExit.setBounds(250, 50, 100, 20);
add(bExit);
bExit.addActionListener(this);
lshowDate = new JLabel("Date: ");
lshowDate.setBounds(50, 100, 200, 20);
lshowDate.setForeground(Color.RED);
lshowDate.setFont(new Font("SansSerif",Font.BOLD, 10));
add(lshowDate);
}
public static void main(String[] args)
{
myWindow window = new myWindow();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object src = e.getSource();
if(src==bExit)
{
dispose();
}
else if(src==bGetDate)
{
//System.out.println(new Date());
lshowDate.setText(new Date().toString());
}
else if(src==bSysCheck)
{
System.out.print("sys details: ");
Runtime r = Runtime.getRuntime();
Process p;
try {
p = r.exec("uname -a");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = b.readLine()) != null)
{
System.out.println(line);
}
} catch (IOException | InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
Java methods - simple calc
file: Methods.java
import java.util.Scanner;
import practice.fromOperation;
public class Methods
{
public static void main(String[] args)
{
System.out.print("Plase enter a: ");
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
System.out.print("Please enter b: ");
int b = scan.nextInt();
System.out.println("first: " +a);
System.out.println("second: " +b);
System.out.println("sum: "+fromOperation.addInt(a, b));
System.out.println("min: "+fromOperation.minusInt(a, b));
System.out.println("multiply: "+fromOperation.multiplyInt(a, b));
if (b !=0)
{
System.out.println("div: "+fromOperation.divInt(a, b));
}
else
{
System.err.println("Divide by zero");
}
scan.close();
}
}
file fromOperation.java
public class fromOperation
{
static int addInt(int a, int b)
{
int sum = a + b;
return sum;
}
static int minusInt(int a, int b)
{
int min = a - b;
return min;
}
static int multiplyInt(int a, int b)
{
int multipl = a * b;
return multipl;
}
static int divInt(int a, int b)
{
int div = a / b;
return div;
}
}
import java.util.Scanner;
import practice.fromOperation;
public class Methods
{
public static void main(String[] args)
{
System.out.print("Plase enter a: ");
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
System.out.print("Please enter b: ");
int b = scan.nextInt();
System.out.println("first: " +a);
System.out.println("second: " +b);
System.out.println("sum: "+fromOperation.addInt(a, b));
System.out.println("min: "+fromOperation.minusInt(a, b));
System.out.println("multiply: "+fromOperation.multiplyInt(a, b));
if (b !=0)
{
System.out.println("div: "+fromOperation.divInt(a, b));
}
else
{
System.err.println("Divide by zero");
}
scan.close();
}
}
file fromOperation.java
public class fromOperation
{
static int addInt(int a, int b)
{
int sum = a + b;
return sum;
}
static int minusInt(int a, int b)
{
int min = a - b;
return min;
}
static int multiplyInt(int a, int b)
{
int multipl = a * b;
return multipl;
}
static int divInt(int a, int b)
{
int div = a / b;
return div;
}
}
czwartek, 4 września 2014
Scala jar
scala -savecompiled helloWorld.scala
[marek@localhost src]$ jar tvf helloWorld.jar
128 Thu Sep 04 10:21:26 CEST 2014 META-INF/MANIFEST.MF
867 Thu Sep 04 10:21:26 CEST 2014 Main$.class
555 Thu Sep 04 10:21:26 CEST 2014 Main.class
[marek@localhost src]$ jar tvf helloWorld.jar
128 Thu Sep 04 10:21:26 CEST 2014 META-INF/MANIFEST.MF
867 Thu Sep 04 10:21:26 CEST 2014 Main$.class
555 Thu Sep 04 10:21:26 CEST 2014 Main.class
Subskrybuj:
Posty (Atom)