Showing posts with label sample java. Show all posts
Showing posts with label sample java. Show all posts

Tuesday, May 8, 2012

String Reverse Example in Java Programming

It is a sample string reverse example. In this program read array of string from user and give output array of reverse string.
Here use StringBuffer class and create object name[] of StringBuffer class. Use method reverse() to reverse inserted string.

Program Code

//developed by Om Prakash
import java.util.*;
class StringReverse
{
public static void main(String argv[])
{
int size;
Scanner console=new Scanner(System.in);
System.out.println("*******STRING REVERSE*******");
System.out.print("How many string do you want to enter : ");
size=console.nextInt();
StringBuffer name[]=new StringBuffer[size+1];
String temp;
int i;
System.out.println("Enter "+size+" string : ");
for(i=0;i<=size;i++)
{
temp=console.nextLine();
name[i]=new StringBuffer(temp);
}
System.out.println("String after Reverse");
for(i=0;i<=size;i++)
{
name[i].reverse();
System.out.println(name[i]);
}
}
}

Output





Sunday, January 15, 2012

Factorial Program in Java Using Class

It is a factorial program in java using class and method.
Public class is fact_main where main method is defined. Another class Factorial where fact method is defined.

Aim: Write a java program to calculate factorial of a number using class concept

Java Program Code
import java.util.*;
class Factorial
{
 private int i=1; 
 public int fact(int n)
 { 
  while(n>0)
  {
   i=i*n; 
   n--;
  }
  return i;
 }
}

public class fact_main
{
 public static void main(String[] args)
 {
  Factorial fact1=new Factorial();
  Scanner object1= new Scanner(System.in);
  System.out.println("******** FACTORIAL PROGRAM ********");
  System.out.print("Enter number to find factorial: ");
  int num=object1.nextInt();
  int k=fact1.fact(num);
  System.out.println("Factorial of "+num+" is:  "+k);
 }
}

Output
Program name: fact_main.java






Friday, December 23, 2011

Basic Java Program To Print Hello World

It is a basic JAVA program to say to the world "Hello World". It is example for beginners to understand basic java code.
The name Java was derived at a local coffee shop frequented by some of the members and it's related to coffee material.
So In this basic program print Hello WORLD inside the coffee mug.



First install JDK(JAVA Development Kit) with latest version because to run java program need JVM(java virtual machine) when you install JDK then JVM installed in your system.
Java Virtual Machine(JVM) makes JAVA as platform independent.


Hello World JAVA Program

Program Code
class hello   //create class hello
{
  public static void main(String[] args)  //main method 
  {
    System.out.println("\nExample to print HELLO WORLD in Coffee Mug\n");
    System.out.println(" _______\n|       |\n|       |__\n| HELLO"
+" |  |\n| WORLD |  |\n|       |__|    \n|       |\n|_______|");
  }
}

Save Program
Type the program code in notepad and save Java program with classname.java. In this program save as hello.java.
File name and class name must be same.

Compile Program
javac hello.java
When compile program then it creates bytecode named with hello.class. class file created in same location where program stored.
Bytecode is a code which as same as machine language code.

Exicute the Program
java hello
Now we execute the bytecode in the Java interpreter with using this command.

Output