You are on page 1of 8

Name: KHUSHAL ISSRANI Section: E

Reg No: 209302027

Lab Assignment – 2
Object-Oriented Programming [IT 2130]
KHUSHAL ISSRANI Section-E
209302027

Exercise 1:
Write a program in java to take 10 integer numbers as user input using the BufferedReader and
print the sum of these numbers.

Code:
import java.io.*;

public class exercise1

    public static void main(String[] args) throws IOException

  {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        String[] input = new String[10];

        int a,b,c,d,e,f,g,h,i,j;

        System.out.print("Enter Ten Integers: ");

        input = in.readLine().split(" ");

        a = Integer.parseInt(input[0]);

        b = Integer.parseInt(input[1]);

        c = Integer.parseInt(input[2]);

        d = Integer.parseInt(input[3]);

        e = Integer.parseInt(input[4]);

        f = Integer.parseInt(input[5]);

        g = Integer.parseInt(input[6]);

        h = Integer.parseInt(input[7]);

        i = Integer.parseInt(input[8]);

        j = Integer.parseInt(input[9]);

Page | 1
Name: KHUSHAL ISSRANI Section: E
Reg No: 209302027

        System.out.println("\nThe sum of Integers is " + (a+b+c+d+e+f+g+h+i+j));

  }

Output

Exercise 2:
Write the program description given in Exercise1 in java using the Scanner class.

Code:
import java.util.Scanner;

class exercise2

    public static void main(String[] args)

  {

        Scanner sc = new Scanner(System.in);

        int i, n, s=0;

        System.out.println("Enter 10 integers: ");

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

    {

            n=sc.nextInt();

            s=s+n;

    }

        System.out.println("Sum of the numbers = "+s);

Page | 2
Name: KHUSHAL ISSRANI Section: E
Reg No: 209302027

  }

Output:

Exercise 3:
Write the java implementation for a class named ‘TaxOnSalary’ to calculate tax on salary. The
class TaxOnSalary is described as follows.
Code:
import java.util.*;

public class taxOnSalary

    double salary;

    boolean isPANsubmitted;

    Scanner sc;

    taxOnSalary()

  {

        sc = new Scanner(System.in);

Page | 3
Name: KHUSHAL ISSRANI Section: E
Reg No: 209302027

        isPANsubmitted = false;

        salary = 0.0;

  }

    taxOnSalary(boolean isPaNsubmitted)

  {

        isPANsubmitted = isPaNsubmitted;

        salary = 1000.0;

  }

    double calculateTax()

  {

        double tax = 0.0;

        if(salary < 180000)

    {

            if(isPANsubmitted)

                tax = 0;

            else tax = 0.05 * salary;

    }

        else if((salary>180000) && (salary<500000))

            tax = 0.1 * salary;

        else if((salary>500000) && (salary<1000000))

            tax = 0.2 * salary;

        else if(salary>1000000)

            tax = 0.3 * salary;

Page | 4
Name: KHUSHAL ISSRANI Section: E
Reg No: 209302027

        else

            System.out.println("Invalid Input");

        return tax;

  }

    void inputSalary()

  {

        System.out.println("Enter salary");

        salary = sc.nextInt();

  }

    public static void main(String[] args)

  {

        double salary, tax = 0.0;

        taxOnSalary obj = new taxOnSalary();

        obj.inputSalary();

        tax = obj.calculateTax();

        System.out.println("The applicable tax is: "+tax);

  }

Output:

Page | 5
Name: KHUSHAL ISSRANI Section: E
Reg No: 209302027

Exercise 4:
Define a class Car which encapsulates following attributes and methods
Attributes: private scope
• year - The year field is an int that holds a car's year model (e.g. 2010)
• make - The make field is a String object that holds the make of the car (e.g. "TATA")
• speed - The speed field is an double that holds a car's current speed (e.g. 25.0)

Code:
class Car

    private int year;

    private String make;

    private double speed;

    Car(int year, String make, double speed)

  {

        this.year = year;

        this.make = make;

        this.speed = speed;

  }

    public int getYear()

  {

        return this.year;

  }

Page | 6
Name: KHUSHAL ISSRANI Section: E
Reg No: 209302027

    public String getMake()

  {

        return this.make;

  }

    public double getSpeed()

  {

        return this.speed;

  }

    public void Accelerate()

  {

        speed++;

  }

    public void Accelerate(int increment)

  {

        speed += increment;

  }

    public void Break(int b)

  {

        speed -= Math.sqrt(b);

  }

class raceTrack

    public static void main(String[] args)

  {

        Car obj = new Car(2010, "TATA", 25.0);

        System.out.println("Year : "+ obj.getYear());

        System.out.println("Make : "+ obj.getMake());

Page | 7
Name: KHUSHAL ISSRANI Section: E
Reg No: 209302027

        System.out.println("Speed: "+ obj.getSpeed());

        obj.Accelerate();

        System.out.println("Speed: "+ obj.getSpeed());

        obj.Accelerate(10);

        System.out.println("Speed: "+ obj.getSpeed());

        obj.Break(16);

        System.out.println("Speed: "+ obj.getSpeed());

  }

Output:

Page | 8

You might also like