Professional Documents
Culture Documents
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.*;
{
int a,b,c,d,e,f,g,h,i,j;
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
}
Output
Exercise 2:
Write the program description given in Exercise1 in java using the Scanner class.
Code:
import java.util.Scanner;
class exercise2
{
int i, n, s=0;
for(i=1;i<=10;i++)
{
n=sc.nextInt();
s=s+n;
}
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.*;
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()
{
{
if(isPANsubmitted)
tax = 0;
}
else if(salary>1000000)
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();
}
{
obj.inputSalary();
tax = obj.calculateTax();
}
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
{
this.year = year;
this.make = make;
this.speed = speed;
}
{
return this.year;
}
Page | 6
Name: KHUSHAL ISSRANI Section: E
Reg No: 209302027
{
return this.make;
}
{
return this.speed;
}
{
speed++;
}
{
speed += increment;
}
{
speed -= Math.sqrt(b);
}
class raceTrack
{
Page | 7
Name: KHUSHAL ISSRANI Section: E
Reg No: 209302027
obj.Accelerate();
obj.Accelerate(10);
obj.Break(16);
}
Output:
Page | 8