You are on page 1of 6

Name – Tushar Sonker

Roll No. 2003630100163


Sec- C

Java Assignment 4

1. Write a program to demonstrate the difference between static & non-static variables.

Ans -
public class StaticAndNonStaticVariables {

static int count = 0; // static variable

public int increment() {


count++; // non-static variable
return count;
}

public static void main(String[] args) {


StaticAndNonStaticVariables obj1 = new StaticAndNonStaticVariables();
StaticAndNonStaticVariables obj2 = new StaticAndNonStaticVariables();

System.out.println("Obj1: count is=" + obj1.count); // 0


System.out.println("Obj2: count is=" + obj2.count); // 0

obj1.increment();
obj2.increment();

System.out.println("Obj1: count is=" + obj1.count); // 2


System.out.println("Obj2: count is=" + obj2.count); // 2
}
}

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-

// Abstract class Player


abstract class Player {
public abstract void play();
public abstract void pause();
public abstract void stop();
}

class GenericPlayer extends Player {


@Override
public void play() {
System.out.println("Playing .avi or .dat file");
}

@Override
public void pause() {
System.out.println("Pausing .avi or .dat file");
}

@Override
public void stop() {
System.out.println("Stopping .avi or .dat file");
}
}

// Class DVDPlayer, derived from GenericPlayer


class DVDPlayer extends GenericPlayer {
@Override
public void play() {
System.out.println("Playing .avi, .dat, or .mpg file");
}
}

public class PlayerDemo {


public static void main(String[] args) {
Player player1 = new GenericPlayer();
Player player2 = new DVDPlayer();

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 –

public class AddNumbers {


public int add(int a, int b) {
return a + b;
}

public int add(int a, int b, int c) {


return a + b + c;
}

public int add(int a, int b, int c, int d) {


return a + b + c + d;
}

public static void main(String[] args) {


AddNumbers calculator = new AddNumbers();

int sum2 = calculator.add(5, 10);


int sum3 = calculator.add(5, 10, 15);
int sum4 = calculator.add(5, 10, 15, 20);

System.out.println("Sum of two numbers: " + sum2);


System.out.println("Sum of three numbers: " + sum3);
System.out.println("Sum of four numbers: " + sum4);
}
}

Output –

Sum of two numbers: 15

Sum of three numbers: 30

Sum of four numbers: 50


4. Create a class SimpleCalculator that has functionality of addition, substraction
division, multiplication, square and squareroot and then create another class
ScientificCalculator that has functionality of SimpleCalculator and some other
functionality like sin, cos, tan, sin-1, cos-1, tan-1 .

Ans –

import java.lang.Math;

class SimpleCalculator {
public int add(int a, int b) {
return a + b;
}

public int subtract(int a, int b) {


return a - b;
}

public int multiply(int a, int b) {


return a * b;
}

public double divide(int a, int b) {


if (b == 0) {
System.out.println("Error: Division by zero!");
return Double.NaN;
}
return (double) a / b;
}

public int square(int a) {


return a * a;
}

public double squareRoot(int a) {


if (a < 0) {
System.out.println("Error: Cannot calculate square root of a
negative number.");
return Double.NaN;
}
return Math.sqrt(a);
}
}

class ScientificCalculator extends SimpleCalculator {


public double sin(double angle) {
return Math.sin(angle);
}
public double cos(double angle) {
return Math.cos(angle);
}

public double tan(double angle) {


return Math.tan(angle);
}

public double arcsin(double value) {


if (value < -1.0 || value > 1.0) {
System.out.println("Error: Input value is out of range for
arcsin.");
return Double.NaN;
}
return Math.asin(value);
}

public double arccos(double value) {


if (value < -1.0 || value > 1.0) {
System.out.println("Error: Input value is out of range for
arccos.");
return Double.NaN;
}
return Math.acos(value);
}

public double arctan(double value) {


return Math.atan(value);
}
}

public class CalculatorDemo {


public static void main(String[] args) {
SimpleCalculator simpleCalc = new SimpleCalculator();
ScientificCalculator scientificCalc = new ScientificCalculator();

// Basic arithmetic operations


System.out.println("Addition: " + simpleCalc.add(5, 3));
System.out.println("Subtraction: " + simpleCalc.subtract(5, 3));
System.out.println("Multiplication: " + simpleCalc.multiply(5, 3));
System.out.println("Division: " + simpleCalc.divide(10, 2));
System.out.println("Square: " + simpleCalc.square(4));
System.out.println("Square Root: " + simpleCalc.squareRoot(16));

// 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

Square Root: 4.0

Sine: 0.49999999999999994

Cosine: 0.5000000000000001

Tangent: 0.9999999999999999

Arcsine: 0.5235987755982989

Arccosine: 1.0471975511965979

Arctangent: 0.7853981633974483

You might also like