Java programs for beginners, list of programs in java on various topics, interesting programs in java. Java programs on various topics
Wednesday, August 29, 2012
What is the output?
class ConditionalIf{
public static void main(String args[]){
int i = 1;
int j = i++;
if ((i == ++j) | (i++ == j)) {
i += j;
}
System.out.println("i = " + i);
} }
public static void main(String args[]){
int i = 1;
int j = i++;
if ((i == ++j) | (i++ == j)) {
i += j;
}
System.out.println("i = " + i);
} }
Factorial using While in Java
import java.util.Scanner;
class FactorialWhile{
public static void main(String args[]){
int fact=1,num,i=1;
System.out.println("Enter the number:");
Scanner in=new Scanner(System.in);
num=in.nextInt();
while(i<=num){
fact=fact*i;
i++;
}
System.out.println("factorial of given number is "+fact);
} }
class FactorialWhile{
public static void main(String args[]){
int fact=1,num,i=1;
System.out.println("Enter the number:");
Scanner in=new Scanner(System.in);
num=in.nextInt();
while(i<=num){
fact=fact*i;
i++;
}
System.out.println("factorial of given number is "+fact);
} }
File creation in Java
import java.io.*;
public class FileCreation {
public static void main(String args[]) throws IOException{
// throws IOException to handle Exception
File f=new File("F:/sam.txt"); //F is the drive and sam.txt is the name of file...
if(!f.exists()){
f.createNewFile();
System.out.println("File created successfully");
}
else
System.out.println("file creation error/file exists already");
}
}
public class FileCreation {
public static void main(String args[]) throws IOException{
// throws IOException to handle Exception
File f=new File("F:/sam.txt"); //F is the drive and sam.txt is the name of file...
if(!f.exists()){
f.createNewFile();
System.out.println("File created successfully");
}
else
System.out.println("file creation error/file exists already");
}
}
Java program to get Environment variables
public class getSystemProperties {
public static void main(String[] args) {
String os=System.getProperty("os.name");
System.out.println("Current OS Name: "+os);
String home=System.getProperty("user.home");
System.out.println("Home directory of Current user : "+home);
String version=System.getProperty("os.version");
System.out.println("Versio of OS: "+ version);
String arch=System.getProperty("os.arch");
System.out.println("Architecture of OS: "+ arch);
String jver=System.getProperty("java.version");
System.out.println("Java Version: "+jver);
String uname=System.getProperty("user.name");
System.out.println(uname);
}
}
public static void main(String[] args) {
String os=System.getProperty("os.name");
System.out.println("Current OS Name: "+os);
String home=System.getProperty("user.home");
System.out.println("Home directory of Current user : "+home);
String version=System.getProperty("os.version");
System.out.println("Versio of OS: "+ version);
String arch=System.getProperty("os.arch");
System.out.println("Architecture of OS: "+ arch);
String jver=System.getProperty("java.version");
System.out.println("Java Version: "+jver);
String uname=System.getProperty("user.name");
System.out.println(uname);
}
}
SImple Thread Program To Pause For Loop
class ThreadExample{
public static void main(String args[])
{
Thread th=new Thread();
try{
for(int i=0;i<10;i++)
{
System.out.println(" Value "+i);
th.sleep(2000);
}
}
catch(Exception e)
{
System.out.println(" "+e.getMessage());
}
}
}
public static void main(String args[])
{
Thread th=new Thread();
try{
for(int i=0;i<10;i++)
{
System.out.println(" Value "+i);
th.sleep(2000);
}
}
catch(Exception e)
{
System.out.println(" "+e.getMessage());
}
}
}
Java Program To Open Default Browser
import java.awt.Desktop;
import java.net.*;
class DesktopTools{
public static void main(String args[])
{
try{
URI u=URI.create("www.google.com");
Desktop ds=Desktop.getDesktop();
ds.browse(u);
}catch(Exception e)
{
System.out.println(" "+ e);
}
}
}
It Will open your default browser and redirect you to google Page. It is user defined if you want to change the website address it will works fine.
import java.net.*;
class DesktopTools{
public static void main(String args[])
{
try{
URI u=URI.create("www.google.com");
Desktop ds=Desktop.getDesktop();
ds.browse(u);
}catch(Exception e)
{
System.out.println(" "+ e);
}
}
}
It Will open your default browser and redirect you to google Page. It is user defined if you want to change the website address it will works fine.
Subscribe to:
Posts (Atom)