Lablist Java
Lablist Java
1. Program to accept student name and marks in three subjects. Find the total
marks, average and grade (depending on the average marks).
import java.util.*;
class student
int total,m1,m2,m3;
float avg;
String name;
void getdata()
name=sc.next();
m1=sc.nextInt();
m2=sc.nextInt();
m3=sc.nextInt();
void calculate()
total=m1+m2+m3;
avg=total/3;
System.out.println("Total is : "+total);
System.out.println("Average is : "+avg);
void putdata()
if(avg>=80)
System.out.println("Distinction");
else if(avg>=60)
System.out.println("First class");
else if(avg>=50)
System.out.println("Second class");
else if(avg>=35)
System.out.println("Pass");
else
System.out.println("Fail");
s1.getdata();
s1.calculate();
s1.putdata();
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 java.util.Scanner;
class SumData
int n1;
int n2;
void getData()
n2 = sc.nextInt();
int Sum = 0;
n1 /= 10;
n2 /= 10;
sd.getData();
sd.Sum();
3. Program to input Start and End limits and print all Fibonacci numbers between the ranges.
( Use for loop)
import java.util.Scanner;
class fibo
{
int start,end,f1,f2,f3;
void getdata()
start=sc.nextInt();
end=sc.nextInt();
void generate()
f1=0;
f2=1;
f3=f1+f2;
for(int i=2;f3<=end;i++)
if(f3>=start)
System.out.println(f3);
f1=f2;
f2=f3;
f3=f1+f2;
f.generate();
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]
import java.util.Scanner;
class Pay
String name;
double salary,da,hra,pf,grossal,netsal;
salary=sal;
name=n;
da=0;
hra=0;
pf=0;
grossal=0;
netsal=0;
}
void calculate()
da=salary*15/100;
hra=salary*10/100;
pf=salary*12/100;
grossal=salary+da+hra;
netsal=grossal-pf;
void Display()
System.out.println("DA is : "+da);
System.out.println("HRA is : "+hra);
System.out.println("PF is : "+pf);
String name;
double salary;
name = sc.next();
salary =sc.nextDouble();
Pay obj=new Pay(name,salary);
obj.calculate();
obj.Display();
5. Program 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.).
import java.util.Scanner;
class Distance
this.feet = feet;
this.inches = inches;
System.out.println("Feet ? ");
System.out.println("Inches ? ");
System.out.println("Feet ? ");
System.out.println("Inches ? ");
d1.sumData(d2);
sc.close();
6. Program to create a class “Matrix” that would contain integer values having varied numbers of
columns for each row. Print row-wise sum.
import java.util.Scanner;
class Matrix
int[][] values;
this.values = values;
}
public void printRowSum()
int rowSum = 0;
rowSum += values[i][j];
int row=Sc.nextInt();
int colm=Sc.nextInt();
for(int i=0;i<row;i++)
for(int j=0;j<colm;j++)
value[i][j]=Sc.nextInt();
}
Matrix m=new Matrix(value);
m.printRowSum();
7. Program to extract portion of character string and print extracted string. Assume that ‘n’
characters extracted starting from mth character position.
import java.util.Scanner;
String input=sc.nextLine();
int m = sc.nextInt();
int n = sc.nextInt();
import java.util.Scanner;
import java.util.Vector;
while (true)
System.out.println("4. Exit");
switch (choice)
case 1:
vector.add(elementToAdd);
System.out.println("Element added.");
break;
case 2:
if (vector.remove(elementToRemove))
System.out.println("Element removed.");
}
else
break;
case 3:
break;
case 4:
System.out.println("Exiting program.");
return;
PART B
import java.io.*;
import java.util.Scanner;
class Student
int id;
String name;
id = rno;
name = nm;
}
class StudentExam extends Student
StudentExam(int rno, String nm, int m1, int m2, int m3)
super(rno, nm);
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
total = m1 + m2 + m3;
double per;
String grade;
super(rno,nm,m1,m2,m3);
void findgrade()
per=(double)total/3;
if(m1<35||m2<35||m3<35)
grade="FAIL";
grade="Distinction";
}
else if(per >= 60)
grade="First class";
grade="Second class";
else
grade="Pass";
int n = sc.nextInt();
System.out.println("Id: ");
int id = sc.nextInt();
System.out.println("Name: ");
System.out.println("M1 : ");
int m1 = sc.nextInt();
System.out.println("M2 : ");
int m2 = sc.nextInt();
System.out.println("M3 : ");
int m3 = sc.nextInt();
System.out.println("********Students details*********");
sd[i].findgrade();
System.out.println("ID : "+sd[i].id);
System.out.println("Name : "+sd[i].name);
System.out.println("Percentage : "+sd[i].per);
System.out.println("Grade : "+sd[i].grade);
4. Write a Program to create an abstract class named shape that contains two integers and
an empty method named print Area(). Provide three classes named Rectangle, Triangle and
Ellipse such that each one of the classes extends the class shape. Each one of the class
contains only the method print Area() that print the area of the given shape.[Abstract class].
import java.util.Scanner;
int h=0,w=0;
this.h=a;
this.w=b;
float area=h*w;
this.h=a;
this.w=b;
float area=0.5f*h*w;
this.h=a;
this.w=b;
float area=3.14f*h*w;
}
class pb4
int v1,v2;
v1=sc.nextInt();
v2=sc.nextInt();
r.printArea(v1,v2);
v1=sc.nextInt();
v2=sc.nextInt();
t.printArea(v1,v2);
v1=sc.nextInt();
v2=sc.nextInt();
e.printArea(v1,v2);
5. Create a package to convert temperature in centigrade into Fahrenheit, and one more package to
calculate the simple Interest. Implement both package in the Main () by accepting the required
inputs for each application.
package Temp;
package Temp;
Process completed.
package interest;
Process completed.
import Temp.Temp;
import interest.Interest;
import java.util.Scanner;
// Temperature conversion
fahrenheit = scanner.nextDouble();
celsius = Temp.fahrenheitToCelsius(fahrenheit);
scanner.close();