
Q. 1 programes on if....else 1. write a program to check whether entered year is leap year or not 2. write a program to check whether entered no. ends with 5 or not 3. write a program to find minimum of 3 nos using nested if else. 4. write a program to check whether entered no. is divisible by 5 or 7 or both 5. write a program to input score of a student out of 100 and print grades using else .... if ladder
Q. 2 programes on switch 1. write a program to design menu driven arithmetic calculator 2. write a program to print no of days in a given month
average.
and print the information.
Q. 11 WAP to demonstrate a) static data members b) static methods
In our java programming language we have 2 types of variables those are static variables and instance variables.In the same way we have 2 types
of methods those are instance methods and static methods.
Methods and Variables in Java
In our java programming language we have 2 types of variables those are static variables and instance variables. In the same way we have 2
types of methods those are instance methods and static methods. Instance variables and methods do not preceded by the static keyword where as
static variables methods followed by the keyword static.
Instance variables
Instance data members are those whose memory space is creating each and every time when we create an object. If any data member is taking a
specific value then that data member is recommended make as instance data member that means any data member is created in a class it will
belongs to only those objects. Programmatic instance data members declaration should not followed by a keyword static.
Main points
1.Each and every instance data member must be accessed with respect to object. 2.the value of instance data member is not sharable. 3.Instance data members are also known as object level members since they depend on object name but independent from class name.
Static Data Members :
Static data members are those whose memory space is creating only once when ever the class is loaded in main memory irrespective of number of
objects are created. If any data member is taking a specific value then that data member common value for many numbers of objects then that
data member is recommended make as static variable. Programmatic static variables declaration must be preceded by a keyword static.
Main points
Instance Methods
Instance methods are those which perform repeated operation such as such as reading the data form file, reading the data from data base and
etc. Programmatic if we want make any method as instance method that method can not preceded by the keyword static.
Syntax for Instance method
Return-type method-name(list of parameters if any){
// Block of statements;
}
Main points
from the class name.
Static methods
Static methods are those which are recommended to perform one time operations such as opening a file either in read or write mode, opening the
data base connection, verification of user name and password and etc. programmatic if we want make any method as static then the definition of
such method must be preceded by keyword static
Syntax for the Static method
static Return-type Method-name(list of parameters if any){
//Block of statements.
}
Main points
1.All static members in java must be accessed with respect to class name. Syntax: class-name.static-method-name();
Example: Integer.parseInt("100");
2.The result of static method is always sharable. static methods are also known as class-level methods since they depend on class.
eg
// Demonstrate static variables, methods, and blocks.
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}
class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
Here is the output of this program:
a = 42
b = 99
Q, 12 WAP to calculate addition of two distances in feets and inches using objects as functions arguments in java
ANS.......
/* Area Of Rectangle and Triangle using Interface * /
interface Area { float compute(float x, float y); }
class Rectangle implements Area { public float compute(float x, float y) { return(x * y); } }
class Triangle implements Area { public float compute(float x,float y) { return(x * y/2); } }
class InterfaceArea { public static void main(String args[]) { Rectangle rect = new Rectangle(); Triangle tri = new Triangle(); Area area; area = rect; System.out.println("Area Of Rectangle = "+ area.compute(1,2));
area = tri; System.out.println("Area Of Triangle = "+ area.compute(10,2)); } } /* OUTPUT *
Area Of Rectangle = 2.0 Area Of Triangle = 10.0 */
