Assignment 4
Assignment 4
Java Assignment 4
1. Write a program to demonstrate the difference between static & non-static variables.
Ans -
public class StaticAndNonStaticVariables {
obj1.increment();
obj2.increment();
2. Create an abstract class with name “Player”. The class should have play(), pause(),
and stop() methods. Extend the class in another class named “GenericPlayer”. The
GenericPlayer can play .avi and .dat files. Derive another class from GenericPlayer
named “DVDPlayer”. This class can play .mpg file also.
Ans-
@Override
public void pause() {
System.out.println("Pausing .avi or .dat file");
}
@Override
public void stop() {
System.out.println("Stopping .avi or .dat file");
}
}
System.out.println("GenericPlayer:");
player1.play();
player1.pause();
player1.stop();
System.out.println("\nDVDPlayer:");
player2.play();
player2.pause();
player2.stop();
}
}
Output-
GenericPlayer:
Playing .avi or .dat file
Pausing .avi or .dat file
Stopping .avi or .dat file
DVDPlayer:
Playing .avi, .dat, or .mpg file
Pausing .avi or .dat file
Stopping .avi or .dat file
3. Create a java program that has three version of add method which can add two,
three, and four integers.
Ans –
Output –
Ans –
import java.lang.Math;
class SimpleCalculator {
public int add(int a, int b) {
return a + b;
}
// Trigonometric functions
System.out.println("Sine: " + scientificCalc.sin(Math.PI / 6));
System.out.println("Cosine: " + scientificCalc.cos(Math.PI / 3));
System.out.println("Tangent: " + scientificCalc.tan(Math.PI / 4));
System.out.println("Arcsine: " + scientificCalc.arcsin(0.5));
System.out.println("Arccosine: " + scientificCalc.arccos(0.5));
System.out.println("Arctangent: " + scientificCalc.arctan(1.0));
}
}
Output -
Addition: 8
Subtraction: 2
Multiplication: 15
Division: 5.0
Square: 16
Sine: 0.49999999999999994
Cosine: 0.5000000000000001
Tangent: 0.9999999999999999
Arcsine: 0.5235987755982989
Arccosine: 1.0471975511965979
Arctangent: 0.7853981633974483