[go: up one dir, main page]

0% found this document useful (0 votes)
74 views11 pages

Day 3 FN Assignment

The document contains 6 programming assignments involving inheritance in Java: 1. Implementing an interface to calculate interest rates for different banks. 2. Creating an interface for shapes and implementing it for rectangle, triangle, and square classes. 3. Creating an abstract class for geometric shapes and subclasses for triangle and square. 4. Implementing multiple inheritance with interfaces to print employee information. 5. Demonstrating hierarchical inheritance to calculate square and cube of a number. 6. Calculating student percentage using multi-level inheritance with classes to accept marks, calculate total, and display percentage.

Uploaded by

santhudeepu7272
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)
74 views11 pages

Day 3 FN Assignment

The document contains 6 programming assignments involving inheritance in Java: 1. Implementing an interface to calculate interest rates for different banks. 2. Creating an interface for shapes and implementing it for rectangle, triangle, and square classes. 3. Creating an abstract class for geometric shapes and subclasses for triangle and square. 4. Implementing multiple inheritance with interfaces to print employee information. 5. Demonstrating hierarchical inheritance to calculate square and cube of a number. 6. Calculating student percentage using multi-level inheritance with classes to accept marks, calculate total, and display percentage.

Uploaded by

santhudeepu7272
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/ 11

Day 3 FN Assignment

1 .Write a java program for implementing interface by defining an interface Bank that
provides a method setdata(Rate,Amount) and getdata(). The rate of interest may differ
according to banks. For example, SBI, ICICI, and AXIS banks are providing 8.4%,
7.3%, and 9.7% rate of interest respectively and Amount is 10000.

setdata() // setting Amount and Rate into the data members in cass
getdata() // It should return “The interest is 8.4% and Interest is 840.0”
Program:-
package bank;
interface Bank {
void setData(double rate, double amount);
String getData();
}
class SBI implements Bank {
private double rate;
private double amount;
public void setData(double rate, double amount) {
this.rate = rate;
this.amount = amount;
}
public String getData() {
double interest = (rate / 100) * amount;
return "SBI: The interest is"+rate+"% and Interest is "+interest;
}
}
class ICICI implements Bank {
private double rate;
private double amount;
public void setData(double rate, double amount) {
this.rate = rate;
this.amount = amount;
}
public String getData() {
double interest = (rate / 100) * amount;
return "ICICI: The interest is"+rate+"% and Interest is "+interest;
}
}
class AXIS implements Bank {
private double rate;
private double amount;
public void setData(double rate, double amount) {
this.rate = rate;
this.amount = amount;
}
public String getData() {
double interest = (rate / 100) * amount;
return "AXIS: The interest is"+rate+"% and Interest is "+interest;
}
}
public class Main {
public static void main(String[] args) {
SBI a=new SBI();
ICICI b=new ICICI();
AXIS c =new AXIS();
a.setData(8.4, 10000);
b.setData(7.3, 10000);
c.setData(9.7, 10000);
System.out.println(a.getData());
System.out.println(b.getData());
System.out.println(c.getData());
}
}

Output:-

2. Build a Java program for creating an interface Polygon which consists of getArea() of
void return type and define the classes Rectangle, Triangle and Square which
implements the interface Polygon with constructors in every class to set the respective
parameters which require. Create objects of every class with parameterized constructor
by getting the respective inputs through keyboard as follows and call the getArea() to
print the area of the above shapes.

Program:-
package santhu;
import java.util.Scanner;
interface Polygon
{
void getArea();
}
class Rectangle implements Polygon
{
private double length;
private double breadth;
public Rectangle(double length, double breadth)
{
this.length = length;
this.breadth = breadth;
}
public void getArea()
{
double area = length * breadth;
System.out.println("The area of the rectangle is " + area);
}
}
class Triangle implements Polygon
{
private double base;
private double height;
public Triangle(double base, double height)
{
this.base = base;
this.height = height;
}
public void getArea()
{
double area = 0.5 * base * height;
System.out.println("The area of the Triangle is " + area);
}
}
class Square implements Polygon
{
private double side;
public Square(double side)
{
this.side = side;
}
public void getArea()
{
double area = side * side;
System.out.println("The area of the Square is " + area);
}
}
public class Main
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter length and breadth of rectangle: ");
double Length = s.nextDouble();
double Breadth = s.nextDouble();
System.out.print("Enter base and height of triangle: ");
double Base = s.nextDouble();
double Height = s.nextDouble();
System.out.print("Enter side of square: ");
double Side = s.nextDouble();
Rectangle rectangle = new Rectangle(Length,Breadth);
Triangle triangle = new Triangle(Base,Height);
Square square = new Square(Side);
rectangle.getArea();
triangle.getArea();
square.getArea();
}
}

Output:-

3. Write a Java program to create an abstract class GeometricShape with abstract


methods area() and perimeter(). Create subclasses Triangle and Square that extend the
GeometricShape class and implement the respective methods to calculate the area and
perimeter of each shape by getting inputs through keyboard .
Program:-
package santhu;
import java.util.Scanner;
abstract class GeometricShape {
abstract double area();
abstract double perimeter();
}
class Triangle extends GeometricShape {
private double s1;
private double s2;
private double s3;
public Triangle(double s1, double s2, double s3) {
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
}
double area() {
double s = (s1 + s2 + s3) / 2;
return Math.sqrt(s * (s - s1) * (s - s2) * (s - s3));
}
double perimeter() {
return s1 + s2 + s3;
}
}
class Square extends GeometricShape {
private double side;
public Square(double side) {
this.side = side;
}
double area() {
return side * side;
}
double perimeter() {
return 4 * side;
}
}
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter side of square: ");
double Side = s.nextDouble();
System.out.print("Enter sides of triangle: ");
double s1 = s.nextDouble();
double s2 = s.nextDouble();
double s3 = s.nextDouble();
Square square = new Square(Side);
Triangle triangle = new Triangle(s1, s2, s3);
System.out.println("Triangle Area: " + triangle.area());
System.out.println("Triangle Perimeter: " + triangle.perimeter());
System.out.println("Square Area: " + square.area());
System.out.println("Square Perimeter: " + square.perimeter());
}
}

Output:-
4. Write a JAVA program to implement multiple inheritance to print the followinginformation
using interface.

Program:-

package santhu;
import java.util.Scanner;
interface BasicInfo {
void inputBasicInfo();
void displayBasicInfo();
}
interface DepartmentInfo {
void inputDepartmentInfo();
void displayDepartmentInfo();
}
class Employee implements BasicInfo, DepartmentInfo {
private String name;
private int employeeId;
private String gender;
private String departmentName;
private int departmentNo;
private int departmentStrength;
public void inputBasicInfo() {
Scanner s = new Scanner(System.in);
System.out.print("Enter Name: ");
name = s.nextLine();
System.out.print("Enter Employee Id: ");
employeeId = s.nextInt();
s.nextLine();
System.out.print("Enter Gender: ");
gender = s.nextLine();
}
public void displayBasicInfo() {
System.out.println("Basic Information...:");
System.out.println("Name: " + name);
System.out.println("Employee ID: " + employeeId);
System.out.println("Gender: " + gender);
}
public void inputDepartmentInfo() {
Scanner s = new Scanner(System.in);
System.out.print("Enter Department Name: ");
departmentName = s.nextLine();
System.out.print("Enter Department No: ");
departmentNo = s.nextInt();
System.out.print("Enter Department strength: ");
departmentStrength = s.nextInt();
}
public void displayDepartmentInfo() {
System.out.println("Department Information...:");
System.out.println("Department Name: " + departmentName);
System.out.println("Department No: " + departmentNo);
System.out.println("Department strength: " + departmentStrength);
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Enter employee's basic info:");
Employee employee = new Employee();
employee.inputBasicInfo();
System.out.println("Enter employee's department info:");
employee.inputDepartmentInfo();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Employee's Information is:");

System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
employee.displayBasicInfo();
employee.displayDepartmentInfo();

System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
Output:-

5. Write a Java program to demonstrate example of hierarchical inheritance to get


square and cube of a number.

Program:-
package santhu;
import java.util.Scanner;
class Number
{
protected double num;
public Number(double num)
{
this.num = num;
}
}
class Square extends Number
{
public Square(double num)
{
super(num);
}
public double calculate()
{
return num * num;
}
}
class Cube extends Number {
public Cube(double num)
{
super(num);
}
public double calculate()
{
return num * num * num;
}
}
public class Main
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter a number: ");
double n = s.nextDouble();
Square square = new Square(n);
Cube cube = new Cube(n);
System.out.println("Square of " + n + " is: " + square.calculate());
System.out.println("Cube of " + n + " is: " + cube.calculate());
}
}

Output:-
6. Write a JAVA program to calculate the percentage of a student using multi-level
inheritance. Accept the marks of three subjects sub1,sub2,sub3 (through function
accept_marks()) in base class called Addclass. The class Addclass will be derived by the
subclass called Total (in which print the sum of all the 3 subjects using function total()).
The class Total will be inherited by the class called Percentage (in which show_result()
function is defined to perform calculation of percentage and print the percentage).
Create the object for the Percentage and Call total() and show_result().

Program:-
package santhu;
import java.util.Scanner;
class Addclass {
protected int sub1, sub2, sub3;
public void acceptMarks() {
Scanner s = new Scanner(System.in);
System.out.println("Enter Marks for Three Subjects");
System.out.print("subject 1: ");
sub1 = s.nextInt();
System.out.print("subject 2: ");
sub2 = s.nextInt();
System.out.print("subject 3: ");
sub3 = s.nextInt();
}
}
class Total extends Addclass {
public int total() {
return sub1 + sub2 + sub3;
}
}
class Percentage extends Total {
public void showResult() {
int totalMarks = total();
double percentage = (totalMarks / 3.0);
System.out.println("\nTotal Mark : " + totalMarks);
System.out.println("Percentage Mark : " + String.format("%.4f",
percentage));
}
}
public class Main {
public static void main(String[] args) {
Percentage student = new Percentage();
student.acceptMarks();
student.showResult();
}
}
Output:-

You might also like