0% found this document useful (0 votes)
10 views

Java Progs

The document contains 7 programming problems to demonstrate various Java concepts like operators precedence and associativity, date validation, pattern printing, Fibonacci series, multiplication tables, class and objects, and static members.

Uploaded by

TANMAY BHARGAVA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java Progs

The document contains 7 programming problems to demonstrate various Java concepts like operators precedence and associativity, date validation, pattern printing, Fibonacci series, multiplication tables, class and objects, and static members.

Uploaded by

TANMAY BHARGAVA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

Develop a JAVA program to demonstrate the precedence and associativity


among arithmetic operators. The program should also demonstrate how the
default precedence can be overridden.

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

System.out.println("value of L: " + l);

System.out.println("Precedence overriding: Using brackets");


l = (i + j) / k; // overriding default precedence using brackets
System.out.println("Value of L: " + l);

float n = i / k * 2 + j; //Associativity- division first then multiplication.


System.out.println("\n\n value of n: " + n);

System.out.println("Overriding associativity: Using brackets");


n = i / (k * 2) + j;// overriding associativity using brackets
System.out.println("Value of n: " + n);
}
}

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 + " ");

boolean isTrueDate = true;

if ((day<=0) || (month<=0) || (year<=0))


{
isTrueDate = false;
}
else if(month > 12)
{
isTrueDate = false;
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month
== 8 || month == 10 || month == 12)
{
if (day <= 31)
{
isTrueDate = true;
}
else
{
isTrueDate = false;
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day <= 30)
{
isTrueDate = true;
}
else
{
isTrueDate = false;
}

}
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

3. Write a JAVA program to display the following pattern.


1
22
333
4444
55555

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

4. Write a JAVA program to print the first n members of Fibonacci series.

import java.util.Scanner;

public class Prg4


{
public static void main(String args[])
{
int fib1=0, fib2=1, temp=0, num=0;
Scanner sc=new Scanner(System.in); //invoke Constructor of Scanner
System.out.print("Enter Fibonacci Limit:");
try
{
num=sc.nextInt();
if(num<=0)
{
throw new myexception ("Input Limit not in Range...Enter valid Limit ");
}
else
System.out.println("Fibonacci Limit: "+num);
}
catch(Exception e)
{
System.out.println("Input Error---InValid Limit---Re-Enter.. "+e);
System.exit(0);
}
System.out.println("Fibonacci series:");
System.out.print(fib1);
System.out.print(" " + fib2);
for(int i=2;i<num;i++)
{
temp=fib1+fib2;
System.out.print(" " + temp);
fib1=fib2;
fib2=temp;
}
}
}

OUTPUT

5. Write a program to generate the multiplication tables of a range of


numbers between m and n inclusive and m < n.

import java.util.Scanner;

class Prg5
{
public static void main(String args[])
{
int m=0, n=0, i, j;

System.out.println("Enter range of numbers to print their multiplication


tables");
Scanner in = new Scanner(System.in);
try
{
m = in.nextInt();
n = in.nextInt();
if(m > n || m<=0 || n<=0)
{
throw new Exception ("Input Numbers are not Valid...Enter valid Numbers");
}
else
System.out.print("Given Numbers: "+m+", "+n+"\n");
}
catch(Exception e)
{
System.out.println("Input Error---InValid Range---Re-Enter.. "+e);
System.exit(0);
}

for (i = m; i <= n; i++)


{
System.out.println("\n"+"Multiplication table of "+i);

for (j = 1; j <= 10; j++)


{
System.out.println(i+"*"+j+" = "+(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;

void setdata(int i,String n,String ad)


{
id=i;
name=n;
address=ad;
}
void printdata()
{
System.out.println("Employee details are :");
System.out.println("ID :" +id);
System.out.println("Name :" +name);
System.out.println("Address :" +address);
}
}

class Prg6
{
public static void main(String arg[])
{
emp e=new emp(); //instantiate the object.
e.setdata(123, "Shreya ","Bangalore");
e.printdata();
}
}

OUTPUT

7. Write a JAVA program to demonstrate static member data and static


member methods.

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.

This side Write


only the code.

On the other side


write the output.

Write neatly.

You might also like