Java Progs
Java Progs
class Prg1
{
public static void main(String args[])
{
float i = 40;
float j = 80;
float k = 40;
float l = i + j / k; //precedence of division higher than addition
OUTPUT:
2. Write a JAVA program to validate a date. The program should accept day,
month and year and it should report whether they form a valid date or not.
import java.util.Scanner;
class Prg2
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int day=0, month=0, year=0;
try
{
System.out.println("Enter Day: ");
day = sc.nextInt();
System.out.println("Enter Month");
month = sc.nextInt();
System.out.println("Enter Year");
year = sc.nextInt();
}
catch (Exception e)
{
System.out.println("Input Error---InValid Data---Re-Enter.. "+e);
System.exit(0);
}
System.out.print("\n"+day);
System.out.print("/");
System.out.print(+month);
System.out.print("/");
System.out.print(+year + " ");
}
else if (month == 2) // February check
{
if (year % 4 == 0) // Leap year check for February
{
if (day <= 29)
{
isTrueDate = true;
}
else
{
isTrueDate = false;
}
}
else if (year % 4 != 0)
{
if (day <= 28)
{
isTrueDate = true;
}
else
{
isTrueDate = false;
}
}
}
if(isTrueDate)
{
System.out.println("The Date is Valid");
}
if(!isTrueDate)
{
System.out.println("The Date is Invalid");
}
}
}
OUTPUT
class Prg3
{
public static void main (String args[])
{
System.out.println("Pattern Display");
for (int i=1;i<=5;i++) //Five lines
{
for (int j=6-i; j>1; j--) //Pre-fixed spaces 4, 3, 2, 1
{
System.out.print(" ");
}
for (int j=1;j<=i;j++) //Printing space and number
{
System.out.print(" ");
System.out.print(i);
}
System.out.println();
}
}
}
OUTPUT
import java.util.Scanner;
OUTPUT
import java.util.Scanner;
class Prg5
{
public static void main(String args[])
{
int m=0, n=0, i, j;
OUTPUT
6. Write a JAVA program to define a class, define instance methods for
setting and retrieving values of instance variables and instantiate its object.
class emp
{
int id;
String name;
String address;
class Prg6
{
public static void main(String arg[])
{
emp e=new emp(); //instantiate the object.
e.setdata(123, "Shreya ","Bangalore");
e.printdata();
}
}
OUTPUT
class StaticDemo
{
static int a = 42;
static int b = 99;
static void callme()
{
System.out.println("a = " + a);
}
}
class Prg7
{
public static void main(String args[])
{
StaticDemo.callme();
System.out.println("a in main method = " + StaticDemo.a);
System.out.println("b in main method = " + StaticDemo.b);
}
}
OUTPUT
Don’t start writing it from this
page. Start from next page.
Write neatly.