PART A
[Link] to accept student name and marks in three subjects. Find the total marks, average and grade
(depending on the average marks).
import [Link];
public class student
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int id;
String name;
int m1, m2, m3;
double total;
double avg;
String grade;
[Link]("Enter Reg No:");
id = [Link]();
[Link]("Enter Student Name:");
name = [Link]();
[Link]("Enter Marks 1:");
m1 = [Link]();
[Link]("Enter Marks 2:");
m2 = [Link]();
[Link]("Enter Marks 3:");
m3 = [Link]();
total = m1 + m2 + m3;
avg = total / 3;
if (m1 < 35 || m2 < 35 || m3 < 35) {
grade = "Fail";
} else if (avg >= 70) {
grade = "Distinction";
} else if (avg >= 60) {
grade = "First Class";
} else if (avg >= 50) {
grade = "Second Class";
} else {
grade = "Third Class";
}
[Link]("Percentage: " + avg);
[Link]("Grade :" + grade);
}
}
OUTPUT
Enter Reg No:
101
Enter Student Name:
JOHN
Enter Marks 1:
52
Enter Marks 2:
65
Enter Marks 3:
45
Percentage: 54.0
Grade :Second Class
2. Program, which reads two numbers having same number of digits. The program outputs the sum of
product of corresponding digits.(Hint Input 327 and 539 output 3x5+2x3+7x9=84)
import [Link].*;
public class sum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n1, n2, d1, d2, sum = 0;
[Link]("Enter first number: ");
n1 = [Link]();
[Link]("Enter second number: ");
n2 = [Link]();
while (n1 > 0 && n2 > 0) {
d1 = n1 % 10;
d2 = n2 % 10;
sum = sum + (d1 * d2);
n1 = n1 / 10;
n2 = n2 / 10;
}
[Link]("Sum of product of digits = " + sum);
}
}
OUTPUT
Enter first number: 325
Enter second number: 456
Sum of product of digits = 52
3. Program to input Start and End limits and print all Fibonacci numbers between the ranges.( Use for
loop)
import [Link].*;
public class sume {
public static void main(String[] args) {
int f1 = 0;
int f2 = 1;
int f3 = 0;
int start, end;
Scanner sc = new Scanner([Link]);
[Link]("Enter Start:");
start = [Link]();
[Link]("Enter end:");
end = [Link]();
[Link]("Fibonacci Series are:");
for (int i = start; f1 <= end; i++) {
if(f1>=start)
{
[Link]("" + f1);
}
f3 = f1 + f2;
f1 = f2;
f2 = f3;
}
}
}
OUTPUT
Enter Start:
5
Enter end:
20
Fibonacci Series are:
5
8
13
4. Define a class named Pay with data members String name, double salary, double da, double hra, double
pf, double grossSal, double netSal and methods: Pay(String n, double s) - Parameterized constructor to
initialize the data members, void calculate() - to calculate the following salary components, and void
display() - to display the employee name, salary and all salary components.
Dearness Allowance = 15% of salary
House Rent Allowance = 10% of salary
Provident Fund = 12% of salary
Gross Salary = Salary + Dearness Allowance + House Rent Allowance
Net Salary = Gross Salary - Provident Fund
Write a main method to create object of the class and call the methods to compute and display the salary
details. [class basics]
package pay;
import [Link];
public class Pay {
String name;
double salary;
double da;
double hra;
double pf;
double grosssal;
double netsal;
public Pay(String n, double s) {
name = n;
salary = s;
}
void calculate() {
da = salary * 15 / 100;
hra = salary * 10 / 100;
pf = salary * 12 / 100;
grosssal = salary + da + hra;
netsal = grosssal - pf;
void display() {
[Link]("Employee Details :");
[Link]("Employee Name :" + name);
[Link]("Employee Salary :" + salary);
[Link]("DA :" + da);
[Link]("HRA :" + hra);
[Link]("PF :" + pf);
[Link]("GROSS SALARY :" + grosssal);
[Link]("NET SALARY :" + netsal);
}
public static void main(String[] args) {
Scanner sc= new Scanner([Link]);
[Link]("Enter Employee Name");
String name=[Link]();
[Link]("Enter Employee Salary");
double salary=[Link]();
Pay obj = new Pay(name,salary);
[Link]();
[Link]();
}
}
OUTPUT
Enter Employee Name
JOHN
Enter Employee Salary
25000
Employee Details :
Employee Name :JOHN
Employee Salary :25000.0
DA :3750.0
HRA :2500.0
PF :3000.0
GROSS SALARY :31250.0
NET SALARY :28250.0
[Link] to create a class DISTANCE with the data members feet and inches. Use a constructor to read
the data and a member function Sum ( ) to add two distances by using objects as method arguments and
show the result. (Input and output of inches should be less than 12.).
package distance;
import [Link];
public class DISTANCE {
int feet, inches;
DISTANCE(int f, int i) {
feet = f;
inches = i;
}
void Sum(DISTANCE d1, DISTANCE d2)
{
feet = [Link] + [Link];
inches = [Link] + [Link];
if (inches >= 12) {
feet = feet + (inches / 12);
inches = inches % 12;
}
[Link]("Total Distance = " + feet + " feet " + inches + " inches");
}
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter feet and inches for Distance 1: ");
[Link]("Enter Feet 1:");
int f1 = [Link]();
[Link]("Enter Inche 1:");
int i1 = [Link]();
[Link]("Enter feet and inches for Distance 2: ");
[Link]("Enter Feet 2:");
int f2 = [Link]();
[Link]("Enter Inche 2:");
int i2 = [Link]();
DISTANCE d1 = new DISTANCE(f1, i1);
DISTANCE d2 = new DISTANCE(f2, i2);
DISTANCE d3 = new DISTANCE(0, 0);
[Link](d1, d2);
}
}
OUTPUT
Enter feet and inches for Distance 1:
Enter Feet 1:
10
Enter Inche 1:
2
Enter feet and inches for Distance 2:
Enter Feet 2:
20
Enter Inche 2:
2
Total Distance = 30 feet 4 inches
6. Program to create a class “Matrix” that would contain integer values having varied numbers of columns
for each row. Print row-wise sum.
package matrix;
import [Link];
public class Matrix {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();
[Link]("Enter number of columns: ");
int cols = [Link]();
int arr[][] = new int[rows][cols];
[Link]("Enter array elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = [Link]();
}
}
[Link]("Row-wise Sum:");
for (int i = 0; i < rows; i++) {
int sum = 0;
for (int j = 0; j < cols; j++) {
sum = sum + arr[i][j];
}
[Link]("Sum of row " + (i + 1) + " = " + sum);
}
}
}
OUTPUT
Enter number of rows: 2
Enter number of columns: 2
Enter array elements:
1
2
3
4
Row-wise Sum:
Sum of row 1 = 3
Sum of row 2 = 7
7. Program to extract portion of character string and print extracted string. Assume that „n‟ characters
extracted starting from Nth character position.
import [Link];
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter String: ");
String str = [Link]();
[Link]("Enter Start Position: ");
int start = [Link]();
[Link]("Enter Number of Characters: ");
int n = [Link]();
String sub = [Link](start - 1, start - 1 + n);
[Link]("Extracted String: " + sub);
}
}
OUTPUT
Enter String: I AM GOOD BOY
Enter Start Position: 6
Enter Number of Characters: 4
Extracted String: GOOD
8. Program to add, remove and display elements of a Vector.
import [Link].*;
public class Ve {
public static void main(String[] args) {
Vector vec = new Vector();
int n, i, pos, ele;
Scanner sc = new Scanner([Link]);
[Link]("Enter The size:");
n = [Link]();
[Link]("Enter The elements:");
for (i = 0; i < n; i++) {
ele = [Link]();
[Link](ele);
}
[Link]("Elmets are:" + vec);
[Link]("Enter The position To Remove an Element:");
pos = [Link]();
[Link]("Removed Element is:" + [Link](pos));
[Link]("After Elmets are:" + vec);
}
}
OUTPUT:
Enter The size:
5
Enter The elements:
4
5
7
8
1
Elmets are:[4, 5, 7, 8, 1]
Enter The position To Remove an Element:
2
Removed Element is:7
After Elmets are:[4, 5, 8, 1]