/* how to open notepad using java program, opening notepad in java code,how to open a text file in java with notepad, how to open notepad through java program */ public calss demo { public void note() { try { System.out.print(".....Opening notepad....."); java.lang.Runtime runTime = java.lang.Runtime.getRuntime(); java.lang.Process process = runTime.exec("notepad"); try { //notepad will be closed after 5000 ms java.lang.Thread.sleep(5000); } catch (java.lang.InterruptedException e) { e.printStackTrace(); } System.out.println("Closing notepad"); process.destroy(); } catch (java.io.IOException e) { e.printStackTrace(); } } } public class notepad {
public static void main(String[] args) { new demo().note(); } }
return dataset; }
private JFreeChart createChart(final CategoryDataset dataset) { // create the chart... final JFreeChart chart = ChartFactory.createBarChart( "Chart", // chart title "Category", // domain axis label "Value", // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips? false // URLs? );
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart... chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation... final CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white);
// set the range axis to display integers only... final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// disable bar outlines... final BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setDrawBarOutline(false); // set up gradient paints for series... final GradientPaint gp0 = new GradientPaint( 0.0f, 0.0f, Color.blue, 0.0f, 0.0f, Color.lightGray ); final GradientPaint gp1 = new GradientPaint( 0.0f, 0.0f, Color.green, 0.0f, 0.0f, Color.lightGray ); final GradientPaint gp2 = new GradientPaint( 0.0f, 0.0f, Color.red, 0.0f, 0.0f, Color.lightGray ); final GradientPaint gp3 = new GradientPaint( 0.0f, 0.0f, Color.pink, 0.0f, 0.0f, Color.lightGray ); final GradientPaint gp4 = new GradientPaint( 0.0f, 0.0f, Color.orange, 0.0f, 0.0f, Color.lightGray ); final GradientPaint gp5 = new GradientPaint( 0.0f, 0.0f, Color.gray, 0.0f, 0.0f, Color.lightGray ); renderer.setSeriesPaint(0, gp0); renderer.setSeriesPaint(1, gp1); renderer.setSeriesPaint(2, gp2); renderer.setSeriesPaint(3, gp3); renderer.setSeriesPaint(4, gp4); renderer.setSeriesPaint(5, gp5);
final CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setCategoryLabelPositions( CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0) ); // OPTIONAL CUSTOMISATION COMPLETED. return chart; }
public static void main(final String[] args) {
final graph demo = new graph("Chart"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true);
} } How to run: note: create the following files and also add following jar files to your classpath. algo.txt with content like 1.928##1.256##2.485##1.021##4.618 cocomo81.txt with content like 5.007##3.263##6.456##2.653##1.199 cocomoa.txt with content like 2.085##1.359##2.688##1.105##4.994, etc please check the above code set classpath=./lib/commons-math3-3.0.jar;./lib/jcommon-1.0.13.jar;./lib/jfreechart-1.0.13.jar;./lib/jcommon-0.9.3.jar;. javac -d . graph.java java cost.graph Output:
/* producer consumer problem in java using threads, producer consumer problem in java using (threads) synchronized block (wait() and notify() methods of Object class) example code, producer consumer problem in java using multithreading example, producer consumer problem in java using wait and notify */ Producer is the thread which will produce only one item at a time and it will made wait until it is consumed by the consumer. Consumer is also a thread which will consumes only one item at a time and it is made wait until the Producer produces next item. No Producer and no Consumer threads will produce and consume two consecutive items respectively. class Q { boolean valset=false; int n; synchronized void put(int i) { try { if(valset) wait(); else { n=i; System.out.println("put: "+n); valset=true; notify(); }//else }//try catch(Exception e) { e.printStackTrace(); } }//put() synchronized int get() { try { if(!valset) wait(); else { System.out.println("put: "+n); valset=false; notify(); }//else }//try catch(Exception e) { e.printStackTrace(); } return n; } } //Producer class class Producer implements java.lang.Runnable { Q q; java.lang.Thread t1; int i; Producer(Q q) { this.q=q; t1=new java.lang.Thread(this,"pt"); t1.start(); }//con public void run() { System.out.println("name of the thread: "+t1.getName()); i=0; while(true) q.put(++i); }//run() }
//Consumer class class Consumer implements java.lang.Runnable { Q q; java.lang.Thread t1; int i; Consumer(Q q) { this.q=q; t1=new java.lang.Thread(this,"ct"); t1.start(); }//con public void run() { System.out.println("name of the thread: "+t1.getName()); i=0; while(true) i=q.get(); }//run() } //main class public class demo1 { public static void main(String arg[]) { Q q=new Q(); Producer pro=new Producer(q); Consumer con=new Consumer(q); } } how to run: javac demo1.java java demo1 note: press ctrl+c to stop Output:
/* Client and Server Socket example code in java, Client-Server Programming in Java code, server and client program in java, Server Socket example code */ import java.io.*; import java.net.*; class ServerSocketDemo { public static void main(String []args) { java.net.ServerSocket ss=null; java.io.PrintWriter pw = null; java.io.BufferedReader br = null; int i=0; try { //server running at 40000 port number ss = new java.net.ServerSocket(40000); } catch(java.lang.Exception e) { System.out.println("Exception in Server while creating connection"+e); e.printStackTrace(); } System.out.print("Server is ready"); while (true) { System.out.println (" Waiting for connection...."); java.net.Socket s=null; try { System.out.println("connection "+s+ "\n printwriter "+pw+"\n bufferedreader "+br); s = ss.accept(); System.out.println("Connection established with client"); pw = new java.io.PrintWriter(s.getOutputStream()); br = new java.io.BufferedReader(new java.io.InputStreamReader(s.getInputStream())); System.out.println("connection "+s+ "\n printwriter "+pw+"\n bufferedreader "+br); i = new Integer(br.readLine()); System.out.println("i is "+i); } catch(java.lang.Exception e) { System.out.println("Exception in Server "+e); e.printStackTrace(); } System.out.println("Connection established with "+s); i*=i; pw.println(i); try { pw.close(); br.close(); } catch(java.lang.Exception e) { System.out.println("Exception while closing streams"); } } } } //Client Socket example code import java.net.*; import java.io.*; class ClientSocketDemo { public static void main(String []arga) throws java.lang.Exception { java.net.Socket s = null; java.io.PrintWriter pw = null; java.io.BufferedReader br = null; System.out.println("Enter a number one digit"); int i=(System.in.read()-48); // will read only one character System.out.println("Input number is "+i); try { //sending request to the server running at ip 192.16.0.100 and port 40000 s = new java.net.Socket("192.168.0.100",40000); System.out.println(s); pw = new java.io.PrintWriter(s.getOutputStream()); System.out.println(pw); br = new java.io.BufferedReader(new java.io.InputStreamReader(s.getInputStream())); System.out.println(br); System.out.println("Connection established, streams created"); } catch(java.lang.Exception e) { System.out.println("Exception in Client "+e); } pw.println(i); pw.flush(); System.out.println("Data sent to server"); String str = br.readLine(); System.out.println("The square of "+i+" is "+str); } } Output:
/* Roman To Decimal conversion in java example code, roman numeral to decimal java program, java program for converting decimal to roman */ public class RomanToDecimal { public static void romanToDecimal(String romanNumber) { //Initialization int decimal = 0; int lastNumber = 0; //operation to be performed on upper cases even if user enters roman values in lower case chars String romanNumeral = romanNumber.toUpperCase(); for (int x = romanNumeral.length() - 1; x >= 0 ; x--) { //getting each character to convert char convertToDecimal = romanNumeral.charAt(x); //choosing a number switch (convertToDecimal) { case 'M': //if char is equals to 'M' then the number is '1000' decimal = processDecimal(1000, lastNumber, decimal); lastNumber = 1000; break;
case 'D': //if char is equals to 'D' then the number is '500' decimal = processDecimal(500, lastNumber, decimal); lastNumber = 500; break;
case 'C': //if char is equals to 'C' then the number is '100' decimal = processDecimal(100, lastNumber, decimal); lastNumber = 100; break;
case 'L': //if char is equals to 'L' then the number is '50' decimal = processDecimal(50, lastNumber, decimal); lastNumber = 50; break;
case 'X': //if char is equals to 'X' then the number is '10' decimal = processDecimal(10, lastNumber, decimal); lastNumber = 10; break;
case 'V': //if char is equals to 'V' then the number is '5' decimal = processDecimal(5, lastNumber, decimal); lastNumber = 5; break;
case 'I': //if char is equals to 'I' then the number is '1' decimal = processDecimal(1, lastNumber, decimal); lastNumber = 1; break; } } //displaying output System.out.println("roman number: "+romanNumber+"\ndecimal number: "+decimal); }
public static int processDecimal(int decimal, int lastNumber, int lastDecimal) { //processing decimals if (lastNumber > decimal) { return lastDecimal - decimal; } else { return lastDecimal + decimal; } }
//main method public static void main(java.lang.String args[]) { romanToDecimal("xi"); } } Output: E:\>javac RomanToDecimal.java E:\>java RomanToDecimal roman number: xi decimal number: 11
/* how to do highlight on string in java code, java highlight word in string */ here is a sample code String startTag = "<b><font size='3' color='blue'>"; String startTag1 = "<font size='2' color='blue'>"; String endTag1 = "</font>"; String endTag = "</font></b>"; //here 'al' is arraylist which contains some elements for(int i1=0;i1<ary1.length;i1++) {
if(al.contains(ary1[i1].toLowerCase().trim())) { //flag=1; hResult.append(startTag); hResult.append(ary1[i1]+" "); hResult.append(endTag); } else { //flag=flag; hResult.append(startTag1); hResult.append(ary1[i1]+" "); hResult.append(endTag1); } } here print the output. output: