Thursday, 16 February 2017

Java program using if, else if, else

public class ifElseIf
{
public static void main (String args[])
{
int marks = 90;
if (marks <50)
{
System.out.println(" Fail ");
}
else if(marks >=50 && marks<70)
{
System.out.println("Pass");
}
else if(marks >=70 && marks<80)
{
System.out.println(" Grade C");
}
else if (marks >=80 && marks<90)
{
System.out.println("Grade B");
}
else if (marks>=90 && marks<100)
{
System.out.println("Grade A");
}
else
{
System.out.println("Fail");
}
}
}

Java program to find odd or even number with ifelse

public class ifElse
{
public static void main (String[] args)
{
int a = 20;
if (a %2 == 0)
{
System.out.println(" Number is even");
}
else
{
System.out.println("Number is odd");
}
}
}

Wednesday, 15 February 2017

Parsing Strings into Dates

import java.util.*;
import java.text.*;

public class parseDate {
public static void main(String args[]) {
SimpleDateFormat a = new SimpleDateFormat("yyyy-MM-dd");
String input = args.length == 0 ? "1978-12-20" : args[0]; // enter the date to know the day

System.out.print(input + "Parses as");
Date t;
try {
t=a.parse(input);
System.out.println(t);
}
catch (ParseException e) {
System.out.println("Unparseable using " + a);
}
}
}

Tuesday, 14 February 2017

Date and time formatting can be done very easily using printf method

import java.util.*;
public class datePrintf {
public static void main(String args[]) {
Date date = new Date();
String str= String.format("Current Date/Time : %tc", date);
System.out.printf(str);
}
}

Date Formatting Using SimpleDateFormat

import java.util.*;
import java.text.*;

public class dateDemo1
{
public static void main(String args[])
{
Date dNow = new Date( );
SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

System.out.println("Current Date : " + ft.format(dNow));
}
}

Java program to get current Date and Time

import java.util.Date;
public class datePc
{
public static void main (String[] args)
{
Date today = new Date();
System.out.println(today.toString());
}
}


//output : Wed Feb 15 08:38:00 IST 2017

Monday, 13 February 2017

Program is an example of length method.

class stringLength
{
public static void main(String myArr[])
{
String a = " what is the length of this string ";
int len = a.length();
System.out.println("String length is : " + len);
}
}

The method determines whether the specified char value is a letter.

class character
{
public static void main(String args[])
{
System.out.println(Character.isLetter('c'));
System.out.println(Character.isLetter('5'));
System.out.println(Character.isLetter('y'));
}
}

Sunday, 12 February 2017

java program for Fibonacci Series

class fibonacci
{
public static void main(String[] args)
{
int a = 20;
long[] b = new long[a];
b[0] = 0;
b[1] = 1;
//create the Fibonacci series and store it in an array
for(int i=2;i<a;i++)
{
b[i]=b[i-1]+b[i-2];
}
   //print the Fibonacci series numbers
System.out.println("Fibonacci Series upto" + a);
for(int i=0;i<a;i++)
{
System.out.print(b[i] + ", ");
}
    }
}

java program to sum all array elements and then find the average of their sum

class loopavg
{
public static void main(String args[])
{
//deine an array
int[] a = new int[]{10,20,30,40,50};
//calculate sum of all array elements
int sum = 0;
for(int i=0;i<a.length;i++)

sum=sum+a[i];
double avg = sum /a.length;
System.out.println(" Average value of array elements is : " + avg);
}
}

Saturday, 11 February 2017

Java program to enter Roll number, Name and Fee from command line.

//Java Scanner class which reads the int, string and double value as an input
import java.util.Scanner;
class fees
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);

System.out.println("Enter Roll No. : ");
int rollno = obj.nextInt();
System.out.println("Enter Name : ");
String name = obj.next();
System.out.println("Enter fee");
double fee = obj.nextDouble();
System.out.println("Roll no:" + rollno+" name:" +name+ "fee :" +fee);
}
}

How to write a Java program to input 10 number and print the sum?

import java.util.Scanner;
public class sum1{
public static void main(String arr[]){
Scanner obj= new Scanner(System.in);
int sum = 0;
for (int i = 0; i < 10; i += 1){
sum += obj.nextInt();
}
System.out.println("The total sum is: " + sum );
}
}

Java program to calculate sum of 10 numbers entered by the user through the command line arguments.

import java.util.Scanner;

public class sum1
{

public static void main(String []args)
{

Scanner obj=new Scanner(System.in);

int[] array=new int[10];

int sum=0;

for(int i=0;i<array.length;i++){

System.out.println("Enter the "+(i+1)+" number");

array[i]=obj.nextInt();

sum+=array[i];

}

System.out.println("The sum is:"+ sum);

}

}

Tuesday, 7 February 2017

JAVA Program to display name and salary of an employee.

import java.io.*;
public class Employeesal
{
public String name;
private double salary;
public Employeesal (String empName)
{
name = empName;
}
public void setSalary(double empSal)
{
salary=empSal;
}
public void printEmp()
{
System.out.println("Name :" + name);
System.out.println("Salary :" + salary);
}
public static void main(String args[])
{
Employeesal empOne = new Employeesal("Rahul");
empOne.setSalary(10000);
empOne.printEmp();
}
}

Monday, 6 February 2017

Accessing Instance Variables and Methods

This example explains how to access instance variables and methods of a class.

import java.io.*;
public class Rahul
{
 
int rahulAge;



    public Rahul(String name)
{
      // This constructor has one parameter, name.
     
System.out.println("Name chosen is :" + name );
 
}

 
public void setAge( int age )
{
     
rahulAge = age;
 
}

 
public int getAge( )
{
     
System.out.println("Rahul's age is :" + rahulAge );
     
return rahulAge;
 
}

public static void main(String []args)
{
      /* Object creation */
     
Rahul myRahul = new Rahul( "Rahul" );

      /* Call class method to set Rahul's age */
     
myRahul.setAge( 2 );

      /* Call another class method to get Rahul's age */
     
myRahul.getAge( );

      /* You can access instance variable as follows as well */
     
System.out.println("Variable Value :" + myRahul.rahulAge );
 
}

}



Result will be :
Name chosen is :Rahul
Rahul's age is :2
Variable Value :2

Java program to find simple interest

import java.io.*;
public class SimpleInterest
{
public static void main(String []args)
  {
float p=100000,r=7.50f,t=5,SI,total; // p : principle amount, r = rate of interest, t : time period, SI : Simple interest, total : total amount paid in 5 years
SI=(p*r*t)/100;
total=c*t;

System.out.println(" SI is " + c);
System.out.println(" Total amount paid in 5 Years :" + total);
}
}



//javac SimpleInterest.java

//java SimpleInterest

Output :

SI is 37500.0

Total amount paid in 5 Years : 187500.0

First Java Program

public class FirstJavaProgram \
{
                    public static void main(String []args) {
                    System.out.println("Hello World");
   }
}


//javac FirstJavaProgram.java

// java FirstJavaProgram

Output is : Hello World