Java Programs

How to compile and run java programs in your  own folder?

Steps:-

1.Create a directory in any drive

for example e:/javaprograms( created javaprograms folder in e drive)

Save all java files into this folder

2.To compile source code files

 create an environment variable in my computer properties

3.My computer->right click-> properties->Advanced system settings->
Create a new user variable

JAVA_HOME: set value pointing the path of jdk as shown.




4.create path variable and set the value as shown


5.Create classpath variable and set its value to current working directory
E:/javaprograms( I created newfolder current working directory as javaprograms).


6.Write java source code
sample program is here..
7.open note pad and type

 Sample program for printing(Welcome to Java programming) in java

class sample
{
public static void main(String args[])
{
System.out.println("Welcome to Java programming");
}
}
8.
Save it as sample.java in javaprograms folder and compile using java compiler as
9.Open cmd from run command.
E:
E: cd javaprograms
e:/javaprograms>javac  sample.java(programname)

10. running JAVA program

e:/javaprograms>java sample(program name).

11.Output: Welcome to Java programming.


Program to calculate Factorial of a number using java.

source code:

import java.util.Scanner;

class Factorial
{
   public static void main(String args[])
   {
      int n, c, fact = 1;

      System.out.println("Enter an integer to calculate it's factorial");
      Scanner in = new Scanner(System.in);

      n = in.nextInt();

      if ( n < 0 )
         System.out.println("Number should be non-negative.");
      else
      {
         for ( c = 1 ; c <= n ; c++ )
            fact = fact*c;

         System.out.println("Factorial of "+n+" is = "+fact);
      }
   }
}


Program  for calculating multiplication table

import java.util.Scanner;

class MultiplicationTable
{
   public static void main(String args[])
   {
      int n, c;
      System.out.println("Enter an integer to print it's multiplication table");
      Scanner in = new Scanner(System.in);
      n = in.nextInt();
      System.out.println("Multiplication table of "+n+" is :-");

      for ( c = 1 ; c <= 20 ; c++ )
         System.out.println(n+"*"+c+" = "+(n*c));
   }
}


Program to calculate max of 2 numbers

Source code:
class Max{
  public static void main(String args[]){
      //taking value as command line argument.
      //Converting String format to Integer value
      int i = Integer.parseInt(args[0]);
      int j = Integer.parseInt(args[1]);
      if(i > j)
          System.out.println(i+" is greater than "+j);
      else
          System.out.println(j+" is greater than "+i);
  }
}



Write a program to Reverse a given no.
Source code:
class Reverse{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);               //take argument as command line
          int remainder, result=0;
          while(num>0){
              remainder = num%10;
              result = result * 10 + remainder;
              num = num/10;
         }
         System.out.println("Reverse number is : "+result);
    }
}

Program to copy one file to other

Source code:

import java.io.*;
class copy
{
public static void main(String args[]) throws IOException
{
int ch;
FileInputStream fin= new FileInputStream(args[0]); 
//for writing data into args[1]
FileOutputStream fout= new FileOutputStream(args[1]);
//read from FIS and write to FOS
while((ch=fin.read())!=-1)
fout.write(ch);
//close
fin.close();
fout.close();
System.out.println("1 file copied");
}
}

Program to identify the entered character.


//wrapper class whose object wraps  r contains a primitive //data type
import java.io.*;
class CharTest
{
public static void main(String args[]) throws IOException
{
char ch;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.println("Enter a character");
ch=(char)br.read();
System.out.print("u entered");
if(Character.isDigit(ch))
System.out.println("a digit");
else if(Character.isUpperCase(ch))
System.out.println("an upper case leeter");
else if(Character.isLowerCase(ch))
System.out.println("is  lowerc ase leeter");
else if(Character.isSpaceChar(ch))
System.out.println("is  spacebar character");
else if(Character.isSpaceChar(ch))
{
System.out.println("white space");
return;
}
else System.out.println("unknown");
br.skip(2);
}
}
}














1 comment:

teaching methods- gamification