Day 3 FN Assignment
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:-
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:-
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:-