0% found this document useful (0 votes)
1 views

Lablist Java

The document contains multiple Java programs demonstrating various concepts such as student grade calculation, Fibonacci sequence generation, salary calculation, distance addition, matrix row sum, string extraction, vector manipulation, and object-oriented programming with inheritance and abstraction. Each program includes classes, methods, and user input handling to perform specific tasks. Additionally, it mentions the creation of packages for temperature conversion and simple interest calculation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lablist Java

The document contains multiple Java programs demonstrating various concepts such as student grade calculation, Fibonacci sequence generation, salary calculation, distance addition, matrix row sum, string extraction, vector manipulation, and object-oriented programming with inheritance and abstraction. Each program includes classes, methods, and user input handling to perform specific tasks. Additionally, it mentions the creation of packages for temperature conversion and simple interest calculation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

PART-A

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()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the name:");

name=sc.next();

System.out.println("Enter the marks of 3 subjects:");

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");

public class Prog1

public static void main(String[] args)

student s1=new student();

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

Scanner sc = new Scanner(System.in);

int n1;

int n2;

void getData()

System.out.println("enter the value of n1= ");


n1 = sc.nextInt();

System.out.println("enter the value of n2= ");

n2 = sc.nextInt();

public void Sum()

int Sum = 0;

while (n1 > 0 && n2 > 0)

Sum += ((n1 % 10) * (n2 % 10));

n1 /= 10;

n2 /= 10;

System.out.println("The sum of product " + Sum);

public class prog2

public static void main(String[] args)

SumData sd = new SumData();

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()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the Start limit");

start=sc.nextInt();

System.out.println("Enter the end limit");

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;

public class Prog3

public static void main(String[] args)

fibo f=new fibo();


f.getdata();

System.out.println("The fibonacci numbers within the limit are : ");

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;

Pay(String n,double sal)

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("Enter the name Employee : "+name);

System.out.println("Enter the Basic Salary : "+salary);

System.out.println("DA is : "+da);

System.out.println("HRA is : "+hra);

System.out.println("PF is : "+pf);

System.out.println("Gross Salary is : "+grossal);

System.out.println("Net Salary is : "+netsal);

public class Prog4

public static void main(String[] args)

String name;

double salary;

Scanner sc= new Scanner(System.in);

System.out.println("Enter the Name : ");

name = sc.next();

System.out.println("Enter the Basic Salary : ");

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

double feet, inches;

public Distance(double feet, double inches)

this.feet = feet;

this.inches = inches;

void sumData(Distance d2)

double totFeet = this.feet + d2.feet;

double totInches = this.inches + d2.inches;

if (totInches >= 12)

totFeet += totInches / 12;

totInches = totInches % 12;

System.out.println("Sum of Distance " + totFeet + " Feet " + totInches + "Inches");;

public class Prog5


{

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Enter the disance 1");

System.out.println("Feet ? ");

int feet1 = sc.nextInt();

System.out.println("Inches ? ");

int inches1 = sc.nextInt();

System.out.println("Enter the disance 2");

System.out.println("Feet ? ");

int feet2 = sc.nextInt();

System.out.println("Inches ? ");

int inches2 = sc.nextInt();

Distance d1=new Distance(feet1,inches1);

Distance d2=new Distance(feet2,inches2);

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;

public Matrix(int[][] values)

this.values = values;

}
public void printRowSum()

for (int i = 0; i < values.length; i++)

int rowSum = 0;

for (int j = 0; j < values[i].length; j++)

rowSum += values[i][j];

System.out.println("Row "+(i+1)+" Sum is : "+rowSum);

public class Prog6

public static void main(String[] args)

Scanner Sc=new Scanner(System.in);

System.out.println("Enter the size of Row");

int row=Sc.nextInt();

System.out.println("Enter the size of Column");

int colm=Sc.nextInt();

int[][] value=new int[row][colm];

System.out.println("Entre the array elements");

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;

public class Prog7

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

System.out.println("Enter the String");

String input=sc.nextLine();

System.out.println("Enter the start element");

int m = sc.nextInt();

System.out.println("Enter the end element");

int n = sc.nextInt();

String extracted = extractString(input, m, n);

System.out.println("Extracted String : "+extracted);

public static String extractString(String input, int m, int n)

int endIndex = Math.min(m + n, input.length());

return input.substring(m, endIndex);

8. Program to add, remove and display elements of a Vector.

import java.util.Scanner;
import java.util.Vector;

public class Prog8

public static void main(String[] args)

Scanner scanner = new Scanner(System.in);

Vector<String> vector = new Vector<>();

while (true)

System.out.println("1. Add element");

System.out.println("2. Remove element");

System.out.println("3. Display elements");

System.out.println("4. Exit");

System.out.println("Enter the choice");

int choice = scanner.nextInt();

scanner.nextLine(); // consume the newline character

switch (choice)

case 1:

System.out.print("Enter element to add: ");

String elementToAdd = scanner.nextLine();

vector.add(elementToAdd);

System.out.println("Element added.");

break;

case 2:

System.out.print("Enter element to remove: ");

String elementToRemove = scanner.nextLine();

if (vector.remove(elementToRemove))

System.out.println("Element removed.");

}
else

System.out.println("Element not found.");

break;

case 3:

System.out.println("Elements in vector: " + vector);

break;

case 4:

System.out.println("Exiting program.");

return;

default: System.out.println("Invalid choice. Please try again.");

System.out.println(); // add a blank line for readability

PART B
import java.io.*;

import java.util.Scanner;

class Student

int id;

String name;

public Student(int rno, String nm)

id = rno;

name = nm;

}
class StudentExam extends Student

int m1, m2, m3,total;

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;

class StudentResult extends StudentExam

double per;

String grade;

StudentResult(int rno,String nm,int m1,int m2,int m3)

super(rno,nm,m1,m2,m3);

void findgrade()

per=(double)total/3;

if(m1<35||m2<35||m3<35)

grade="FAIL";

else if(per >= 80)

grade="Distinction";

}
else if(per >= 60)

grade="First class";

else if(per >= 50)

grade="Second class";

else

grade="Pass";

public class pb2

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of Students");

int n = sc.nextInt();

StudentResult[] sd = new StudentResult[n];

for(int i = 0; i < n; i++)

System.out.println("Enter the details of Students " + (i + 1) + ":");

System.out.println("Id: ");

int id = sc.nextInt();

System.out.println("Name: ");

String name = sc.next();

System.out.println("Marks in three subject ");

System.out.println("M1 : ");
int m1 = sc.nextInt();

System.out.println("M2 : ");

int m2 = sc.nextInt();

System.out.println("M3 : ");

int m3 = sc.nextInt();

sd[i] = new StudentResult(id, name, m1, m2, m3);

System.out.println("********Students details*********");

for (int i = 0; i < n; i++)

sd[i].findgrade();

System.out.println("ID : "+sd[i].id);

System.out.println("Name : "+sd[i].name);

System.out.println("Marks In Subject 1 :"+sd[i].m1);

System.out.println("Marks In Subject 2 :"+sd[i].m2);

System.out.println("Marks In Subject 3 :"+sd[i].m3);

System.out.println("Total Marks in Three Subject : "+sd[i].total);

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;

abstract class shape

int h=0,w=0;

abstract void printArea(int h,int w);


}

class rectangle extends shape

void printArea(int a,int b)

this.h=a;

this.w=b;

float area=h*w;

System.out.println("The area of Rectangle is :"+area);

class triangle extends shape

void printArea(int a,int b)

this.h=a;

this.w=b;

float area=0.5f*h*w;

System.out.println("The area of Triangle is :"+area);

class ellipse extends shape

void printArea(int a,int b)

this.h=a;

this.w=b;

float area=3.14f*h*w;

System.out.println("The area of Rectangle is :"+area);

}
class pb4

public static void main(String[] args)

rectangle r=new rectangle();

triangle t=new triangle();

ellipse e=new ellipse();

int v1,v2;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the length and width of rectangle");

v1=sc.nextInt();

v2=sc.nextInt();

r.printArea(v1,v2);

System.out.println("Enter the base and height of triangle");

v1=sc.nextInt();

v2=sc.nextInt();

t.printArea(v1,v2);

System.out.println("Enter axis values of ellipse");

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;

public class Temp

public static double celsiusToFahrenheit(double celsius)


{

return (celsius * 9 / 5) + 32;

public static double fahrenheitToCelsius(double fahrenheit)

return (fahrenheit - 32) * 5 / 9;

package Temp;

public class Temp

public static double celsiusToFahrenheit(double celsius)

return (celsius * 9 / 5) + 32;

public static double fahrenheitToCelsius(double fahrenheit)

return (fahrenheit - 32) * 5 / 9;

Process completed.

package interest;

public class Interest

public static double calculateSimpleInterest(double principal, double rate, double time)

return (principal * rate * time) / 100;

Process completed.
import Temp.Temp;

import interest.Interest;

import java.util.Scanner;

public class pb5

public static void main(String[] args)

Scanner scanner = new Scanner(System.in);

// Temperature conversion

System.out.print("Enter temperature in Celsius: ");

double celsius = scanner.nextDouble();

double fahrenheit = Temp.celsiusToFahrenheit(celsius);

System.out.println("Temperature in Fahrenheit: " + fahrenheit);

System.out.print("Enter temperature in Fahrenheit: ");

fahrenheit = scanner.nextDouble();

celsius = Temp.fahrenheitToCelsius(fahrenheit);

System.out.println("Temperature in Celsius: " + celsius);

// Simple interest calculation

System.out.print("Enter principal amount: ");

double principal = scanner.nextDouble();

System.out.print("Enter interest rate: ");

double rate = scanner.nextDouble();

System.out.print("Enter time period (in years): ");

double time = scanner.nextDouble();

double interest = Interest.calculateSimpleInterest(principal, rate, time);

System.out.println("Simple interest: " + interest);

scanner.close();

You might also like