100% found this document useful (1 vote)
519 views

Java Interfaces: Attribute Datatype

The document describes a program to display different types of stalls by implementing an interface. It defines an interface "Stall" with an abstract display method. It then defines three classes - GoldStall, PremiumStall, and ExecutiveStall that implement the Stall interface and override the display method. Each class has attributes like name, cost, owner to store stall details and getters/setters to access them. The main method will create objects of these classes and call their display method to output stall information.

Uploaded by

nagendra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
519 views

Java Interfaces: Attribute Datatype

The document describes a program to display different types of stalls by implementing an interface. It defines an interface "Stall" with an abstract display method. It then defines three classes - GoldStall, PremiumStall, and ExecutiveStall that implement the Stall interface and override the display method. Each class has attributes like name, cost, owner to store stall details and getters/setters to access them. The main method will create objects of these classes and call their display method to output stall information.

Uploaded by

nagendra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Java Interfaces

Write a program to calculate maintenance charges based on the type of account and
display the same using Interfaces.

[Note:   Strictly adhere to the object-oriented specifications given as a part of the problem


statement.
Follow the naming conventions as mentioned. Create separate classes in separate files.]

Consider the class Account and define the following private attributes


Attribute Datatype
name String
accountNumber String
balance Double
startDate Date

Include appropriate getters and setters.


Prototype for the Parameterized Constructor,
Account(String name, String accountNumber, Double balance, Date startDate)

Consider an interface named MaintenanceCharge and define the following abstract method.


Method Name Description
Float calculateMaintancecharge(Float noOfYears) This method is used to calculate the maintenance

Create a class named CurrentAccount which implements the MaintenanceCharge interface


Implement the abstract method of MaintenanceCharge in the class CurrentAccount interface
which should calculate the maintenance charge for the CurrentAccount.

Create a class named SavingsAccount which implements the MaintenanceCharge interface


 
Implement the abstract method of MaintenanceCharge interface in the
class SavingsAccount which should calculate the maintenance charge for the
SavingsAccount.

Consider the class Main. It includes the method main.


In the Main method,
 In the main( ) method, get the account details from the user.
 Create an Account Object with the given details and call the calculateMaintancecharge( )
method.
Note:
 The maintenance charge will be Rs. 50  for the Savings account and Rs.100 for the Current
account.
 The float value should be displayed upto 2 decimal places.
 In the Savings Account, the maintenance amount will be 2*m*n+50.
 In the Current Account, the maintenance amount will be m*n+200.
(where m is the maintenance charge per year and n is the number of years)

The link to download the template code is given below


Code Template
Input and Output format:

Refer sample input and output for the specifications.


If the input is not equal to 1 or 2, then print Invalid choice.
All text in bold corresponds to the input and the remaining text corresponds to output.

Sample input and output 1:

1.Current Account
2.Savings Account
1
Name
SB
Account Number
12345
Account Balance
5000
Enter the Start Date(yyyy-mm-dd)
2013-04-22
Enter the Years
4
Maintenance Charge For Current Account 600.00

Sample input and output 2:

1.Current Account
2.Savings Account
2
Name
SB
Account Number
54321
Account Balance
3000
Enter the Start Date(yyyy-mm-dd)
2014-04-12
Enter the Years
6
Maintenance Charge For Savings Account 650.00

Sample input and output 3:

1.Current Account
2.Savings Account
3
Invalid choice

PROGRAM
 
Account.java

…………………..

import java.util.*;

import java.text.*;

class Account {

//fill the code

private String name;

private String accountNumber;

private Double balance;

private Date startDate;

public String getName(){

return name;

public void setName(String name){

this.name=name;

}
public String getAccountNumber(){

return accountNumber;

public void setAccountNumber(String accountNumber){

this.accountNumber=accountNumber;

public Double getBalance(){

return balance;

public void setBalance(Double balance){

this.balance=balance;

public Date getStartDate(){

return startDate;

public void setStartDate(Date startDate){

this.startDate=startDate;

public Account(String name, String accountNumber, Double balance, Date startDate){

this.name=name;

this.accountNumber=accountNumber;
this.balance=balance;

this.startDate=startDate;

Cont…………..

Savings account.java

………………………………….

import java.util.*;

import java.text.*;

class SavingsAccount {

//fill the code

Float calculateMaintanceCharge(Float noOfYears){

Float charges = 2*50*noOfYears+50;

return charges;

Cont……………….

Current account.java

……………………………………

import java.util.*;

import java.text.*;

public class CurrentAccount{

//fill the code


Float calculateMaintanceCharge(Float noOfYears){

Float charges = 100*noOfYears+200;

return charges;

Cont………..

maintanceCharge.java

………………………………………

//create interface

interface MaintenanceCharge{

//create interface

Float calculateMaintancecharge(Float noOfYears);

Cont……………

Main.java

………………..

import java.util.*;

import java.io.*;

import java.text.*;

public class Main {

public static void main(String args[]) throws Exception{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.println("1.Current Account");

System.out.println("2.Savings Account");

Integer n = Integer.parseInt(br.readLine());

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

switch (n) {

case 1:

System.out.println("Name");

String name = br.readLine();

System.out.println("Account Number");

String account = br.readLine();

System.out.println("Account Balance");

Double accountBalance = Double.parseDouble(br.readLine());

System.out.println("Enter the Start Date(yyyy-mm-dd)");

String sdate = br.readLine();

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

Float years = Float.parseFloat(br.readLine());

Account accountObject = new Account(name, account, accountBalance, formatter.parse(sdate));

CurrentAccount currentAccountObject = new CurrentAccount();

Float outputValue =currentAccountObject.calculateMaintanceCharge(years);

System.out.printf("Maintenance Charge For Current Account %.2f",outputValue);

break;

case 2:

System.out.println("Name");

String name_1 = br.readLine();

System.out.println("Account Number");
String account_1 = br.readLine();

System.out.println("Account Balance");

Double accountBalance_1 = Double.parseDouble(br.readLine());

System.out.println("Enter the Start Date(yyyy-mm-dd)");

String sdate_1 = br.readLine();

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

Float years_1 = Float.parseFloat(br.readLine());

Account accountObject_1 = new Account(name_1, account_1, accountBalance_1,


formatter.parse(sdate_1));

SavingsAccount savingsAccountObject = new SavingsAccount();

Float outputValue_1 = savingsAccountObject.calculateMaintanceCharge(years_1);

System.out.printf("Maintenance Charge For Savings Account %.2f",outputValue_1);

break;

default:

System.out.println("Invalid choice");

break;

…………………………………………………………………………………………………………………………………………………………………

Interface
Write a program to display different types of Stalls by implementing interface.

Strictly adhere to the Object-Oriented specifications given in the problem statement. All class
names, attribute names, and method names should be the same as specified in the problem
statement.
Create an interface Stall with the following method.
Method Name Description
void display() abstract method.

Consider a class GoldStall which implements the Stall interface and define the following
private attributes.
Attribute Datatype
stallName String
cost Integer
ownerName String
tvSet Integer

Include appropriate getters and setters.


Prototype for the Parameterized Constructor,
GoldStall(String stallName, Integer cost, String ownerName, Integer tvSet).

Include the following method in the class GoldStall


Method Name Description
void display() To display the stall name, cost of the stall, owner name, and the number of tv sets.

Consider a class PremiumStall which implements the Stall interface and define the following
private attributes.
Attribute Datatype
stallName String
cost Integer
ownerName String
projector Integer

Include appropriate getters and setters.


Prototype for the Parameterized Constructor,
PremiumStall(String stallName, Integer cost, String ownerName, Integer projector).

Include the following method in the class PremiumStall.


Method Name Description
void display() To display the stall name, cost of the stall, owner name, and the number of projectors.

Consider a class ExecutiveStall which implements Stall interface and define the following


private attributes.
Attribute Datatype
stallName String
cost Integer
ownerName String
screen Integer

Include appropriate getters and setters.


Prototype for the Parameterized Constructor,
ExecutiveStall(String stallName, Integer cost, String ownerName, Integer screen).

Include the following method in the class ExecutiveStall.


Method Name Description
void display() To display the stall name, cost of the stall, owner name, and the number of screens.

Consider the class Main. It includes the method main.


In the Main method,
 In the main( ) method, get the stall details from the user.
 Create a Stall Object with the given details and call the display( ) method.
The link to download the template code is given below
Code Template

Input Format:

The first input corresponds to choose the stall type.


The next line of input corresponds to the details of the stall in CSV format according to the
stall type.

Output Format:

Print “Invalid Stall Type” if the user has chosen the stall type other than the given type
Otherwise, display the details of the stall.
Refer to sample output for formatting specifications.

Note: All Texts in bold corresponds to the input and the rest are output.

Sample Input and Output 1:

Choose Stall Type


1)Gold Stall
2)Premium Stall
3)Executive Stall
1
Enter Stall details in comma-separated(Stall Name, Stall Cost, Owner Name, Number of TV
sets)
The Mechanic,120000,Johnson,10
Stall Name: The Mechanic
Cost: Rs.120000
Owner Name: Johnson
Number of TV sets: 10

Sample Input and Output 2:

Choose Stall Type


1)Gold Stall
2)Premium Stall
3)Executive Stall
2
Enter Stall details in comma-separated(Stall Name, Stall Cost, Owner Name, Number of
Projectors)
Knitting plaza,300000,Zain,20
Stall Name: Knitting plaza
Cost: Rs.300000
Owner Name: Zain
Number of Projectors: 20

Sample Input Output 3:

Choose Stall Type


1)Gold Stall
2)Premium Stall
3)Executive Stall
3
Enter Stall details in comma-separated(Stall Name, Stall Cost, Owner Name, Number of
Screens)
Fruits Hunt,10000,Uber,7
Stall Name: Fruits Hunt
Cost: Rs.10000
Owner Name: Uber
Number of Screens: 7

Sample Input Output 4:

Choose Stall Type


1)Gold Stall
2)Premium Stall
3)Executive Stall
4
Invalid Stall Type

PROGRAM
 
GoldStall.java

…………………..

public class GoldStall{

//fill your code here

private String stallName;

private Integer cost;

private String ownerName;

private Integer tvSet;

public String getStallName() {

return stallName;

public void setStallName(String stallName) {

this.stallName = stallName;

public Integer getCost() {

return cost;

public void setCost(Integer cost) {

this.cost = cost;

public String getOwnerName() {

return ownerName;
}

public void setOwnerName(String ownerName) {

this.ownerName = ownerName;

public Integer getTvSet() {

return tvSet;

public void setTvSet(Integer tvSet) {

this.tvSet = tvSet;

GoldStall(String stallName, Integer cost, String ownerName, Integer tvSet){

this.stallName = stallName;

this.cost = cost;

this.ownerName = ownerName;

this.tvSet = tvSet;

void display() {

System.out.println("Stall Name: "+stallName);

System.out.println("Cost:"+"Rs."+cost);

System.out.println("Owner Name: "+ownerName);

System.out.println("Number of TV sets: "+tvSet);

}
Cont………….

Excutive Stall.java

………………………………

public class ExecutiveStall{

//fill your code here

private String stallName;

private Integer cost;

private String ownerName;

private Integer screen;

public String getStallName() {

return stallName;

public void setStallName(String stallName) {

this.stallName = stallName;

public Integer getCost() {

return cost;

public void setCost(Integer cost) {

this.cost = cost;

public String getOwnerName() {

return ownerName;

public void setOwnerName(String ownerName) {


this.ownerName = ownerName;

public Integer getScreen() {

return screen;

public void setScreen(Integer screen) {

this.screen = screen;

ExecutiveStall(String stallName, Integer cost, String ownerName, Integer screen){

this.stallName = stallName;

this.cost = cost;

this.ownerName = ownerName;

this.screen = screen;

void display() {

System.out.println("Stall Name: "+stallName);

System.out.println("Cost:"+"Rs."+cost);

System.out.println("Owner Name:"+ownerName);

System.out.println("Number of Screens: "+screen);

Cont……

primiamStallGold.java
…………………………………

public class PremiumStall{

//fill your code here

private String stallName;

private Integer cost;

private String ownerName;

private Integer projector;

public String getStallName() {

return stallName;

public void setStallName(String stallName) {

this.stallName = stallName;

public Integer getCost() {

return cost;

public void setCost(Integer cost) {

this.cost = cost;

public String getOwnerName() {

return ownerName;

public void setOwnerName(String ownerName) {

this.ownerName = ownerName;
}

public Integer getProjector() {

return projector;

public void setProjector(Integer projector) {

this.projector = projector;

PremiumStall(String stallName, Integer cost, String ownerName, Integer projector){

this.stallName = stallName;

this.cost = cost;

this.ownerName = ownerName;

this.projector = projector;

void display() {

System.out.println("Stall Name: "+stallName);

System.out.println("Cost:"+"Rs."+cost);

System.out.println("Owner Name: "+ownerName);

System.out.println("Number of Projectors: "+projector);

Cont………..

Stall.java
………….

interface Stall{

void display();

//create interface

Cont…

Main.java

…………………

import java.util.*;

import java.lang.*;

public class Main {

public static void main(String[] args){

Scanner br = new Scanner(System.in);

System.out.println("Choose Stall Type");

System.out.println("1)Gold Stall");

System.out.println("2)Premium Stall");

System.out.println("3)Executive Stall");

int type = Integer.parseInt(br.nextLine());

switch(type)

case 1:
System.out.println("Enter Stall details in comma-separated(Stall Name, Stall Cost, Owner
Name, Number of TV sets)");

String st1=br.nextLine();

String[] str1=st1.split(",");

GoldStall gd = new GoldStall(str1[0],Integer.parseInt(str1[1]),str1[2],Integer.parseInt(str1[3]));

gd.display();

break;

case 2:

System.out.println("Enter Stall details in comma-separated(Stall Name, Stall Cost, Owner


Name, Number of Projectors)");

String st2=br.nextLine();

String[] str2=st2.split(",");

PremiumStall pm = new
PremiumStall(str2[0],Integer.parseInt(str2[1]),str2[2],Integer.parseInt(str2[3]));

pm.display();

break;

case 3:

System.out.println("Enter Stall details in comma-separated(Stall Name, Stall Cost, Owner


Name, Number of Screens)");

String st3=br.nextLine();

String[] str3=st3.split(",");

ExecutiveStall ex = new
ExecutiveStall(str3[0],Integer.parseInt(str3[1]),str3[2],Integer.parseInt(str3[3]));

ex.display();

break;
default:

System.out.println("Invalid Stall Type");

break;

…………………………………………………………………………………………………………………………………………….........

Write a program to display different types of notifications for the type of bank chosen by
implementing the interface.
 

Create an interface named Notification with the following abstract methods


 notificationBySms( )
 notificationByEmail( )
 notificationByCourier( )
 

Consider a class named ICICI which implements Notification interface


 

Implement the abstract methods in the class ICICI


Method name Description

public void notificationBySms() This method is used to display the message "ICICI - Notification By SMS"

public void notificationByEmail() This method is used to display the message "ICICI - Notification By Mail"

public void notificationByCourier() This method is used to display the message "ICICI - Notification By Courier"

Consider a class named HDFC which implements Notification interface


 
Implement the abstract methods in the class HDFC
Method name Description

public void notificationBySms() This method is used to display the message "HDFC - Notification By SMS"
public void notificationByEmail() This method is used to display the message "HDFC - Notification By Mail"

public void notificationByCourier() This method is used to display the message "HDFC - Notification By Courier"

Consider a class BankFactory with two methods


Method Description

public Icici getIcici( ) This method is used to return the object for ICICI class

public Hdfc getHdfc( ) This method is used to return the object for HDFC class

Consider the Main class with main( ) method to test the above class.

The link to download the template code is given below


Code Template

Input and Output format:

The first integer corresponds to select the bank, the next integer corresponds to the type of
the notification.
If there is no valid input then display ' Invalid input'.
 

[Note: All text in bold corresponds to the input and remaining text corresponds to
output]

Sample Input and Output 1:

Welcome to Notification Setup


Please select your bank:
1)ICICI
2)HDFC
1
Enter the type of Notification you want to enter
1)SMS
2)Mail
3)Courier
1
ICICI - Notification By SMS

Sample Input and Output 2:


Welcome to Notification Setup
Please select your bank:
1)ICICI
2)HDFC
2
Enter the type of Notification you want to enter
1)SMS
2)Mail
3)Courier
4
Invalid Input

Sample Input and Output 3:

Welcome to Notification Setup


Please select your bank:
1)ICICI
2)HDFC
4
Invalid Input

Problem Requirements:

Java
Keyword Min Count Max Count

interface 1 -

PROGRAM
 

Icici.java

………………

public class ICICI implements Notification{


//Fill your code

public void notificationBySms(){

System.out.println("ICICI - Notification By SMS");

public void notificationByEmail(){

System.out.println("ICICI - Notification By Mail");

public void notificationByCourier(){

System.out.println("ICICI - Notification By Courier");

Cont…..

Hdfc.java

…………….

public class HDFC implements Notification{

// Fill your code

public void notificationBySms(){

System.out.println("HDFC - Notification By SMS");

public void notificationByEmail(){

System.out.println("HDFC - Notification By Mail");

}
public void notificationByCourier(){

System.out.println("HDFC - Notification By Courier");

Cont…….

Notification.java

……………………….

interface Notification{

//Fill your code

abstract void notificationBySms();

abstract void notificationByEmail();

abstract void notificationByCourier();

Cont…………..

Main.java

………………

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Main {


public static void main(String[] args) throws IOException {

BankFactory bankFactory = new BankFactory();

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

ICICI icici;

HDFC hdfc;

System.out.println("Welcome to Notification Setup\nPlease select your


bank:\n1)ICICI\n2)HDFC");

int select = Integer.parseInt(br.readLine());

if(select == 1){

icici = bankFactory.getIcici();

System.out.println("Enter the type of Notification you want to


enter\n1)SMS\n2)Mail\n3)Courier");

int notificationChoice = Integer.parseInt(br.readLine());

if(notificationChoice == 1){

icici.notificationBySms();

} else if (notificationChoice == 2){

icici.notificationByEmail();

}else if(notificationChoice == 3) {

icici.notificationByCourier();

} else {

System.out.println("Invalid Input");

} else if(select ==2) {

hdfc = bankFactory.getHdfc();
System.out.println("Enter the type of Notification you want to
enter\n1)SMS\n2)Mail\n3)Courier");

int notificationChoice = Integer.parseInt(br.readLine());

if(notificationChoice == 1){

hdfc.notificationBySms();

} else if (notificationChoice == 2){

hdfc.notificationByEmail();

}else if(notificationChoice == 3) {

hdfc.notificationByCourier();

} else {

System.out.println("Invalid Input");

} else {

System.out.println("Invalid Input");

……………………………………………………………………………………………………………………………………………………

Static Inner Class


 
Write a program to calculate the area of the rectangle and triangle using the static inner
class concept in java.

[Note: Strictly adhere to the object-oriented specifications given as a part of the problem
statement. Use the same class names, attribute names and method names]

Consider an outer class Shape with the following public static attributes


 
Attributes Datatype
value1 Double
value2 Double
 

Consider a static inner class Rectangle that has the outer class Shape.

Include the following method in the Rectangle class


 
Method Name Description
Here Calculate and return the area of the rectangle by accessing the
public Double computeRectangleArea()
Area of the rectangle = (length * breadth)

Consider a static inner class Triangle that has the outer class Shape.

Include the following method in the Triangle class


 
Method Name Description
Here Calculate and return the area of the triangle by accessing the attrib
public Double computeTriangleArea()
Area of the triangle = (1/2) * (base * height)
 

Include appropriate getters and setters for the outer class attributes in both the inner classes.

Get the option for the shape to compute the area and get the attribute according to the
shape option and set the values to the Shape class attributes. Calculate the area and print
the area.

Consider a driver class Main to test the above classes.

The link to download the template code is given below


Code Template

Input Format:

The first line of the input is an integer corresponds to the shape.


The next line of inputs are Double which corresponds to,
For Rectangle(Option 1) get the length and breadth.
For Triangle(Option 2) get the base and height.

Output Format:

The output consists area of the shape.


Print the double value correct to two decimal places.
Print “Invalid choice”, if the option for the shape is chosen other than the given options.
Refer to sample output for formatting specifications.

[All text in bold corresponds to input and rest corresponds to output]

Sample Input and Output 1:

Enter the shape


1.Rectangle
2.Triangle
1
Enter the length and breadth:
10
25
Area of rectangle is 250.00

Sample Input and Output 2:

Enter the shape


1.Rectangle
2.Triangle
2
Enter the base and height:
15
19
Area of triangle is 142.50

Sample Input and Output 3:

Enter the shape


1.Rectangle
2.Triangle
3
Invalid choice

PROGRAM
 
Shape.java

……………….
import java.util.*;

public class Shape {

//fill your code here

public static class Rectangle {

//fill your code here

public static double value1;

public static double value2;

public static double getValue1() {

return value1;

public static void setValue1(double value1) {

Rectangle.value1 = value1;

public static double getValue2() {

return value2;

public static void setValue2(double value2) {


Rectangle.value2 = value2;

public Double computeRectangleArea(){

Double area = (value1*value2);

return area;

public static class Triangle {

//fill your code here

public static double value1;

public static double value2;

public static double getValue1(){

return value1;

}
public static void setValue1(double value1){

Triangle.value1 = value1;

public static double getValue2(){

return value2;

public static void setValue2(double value2){

Triangle.value2 = value2;

public Double computeTriangleArea(){

double totalArea = (value1*value2)/2;

return totalArea;

Cont………

Main.java
……………….

import java.util.*;

import java.text.*;

public class Main {

public static void main(String[] args){

Scanner sc=new Scanner(System.in);

DecimalFormat df=new DecimalFormat("0.00");

System.out.println("Enter the shape\n1.Rectangle\n2.Triangle");

int input=sc.nextInt();

double value1,value2;

switch (input)

case 1:

System.out.println("Enter the length and breadth:");

Shape.Rectangle rectangle=new Shape.Rectangle();

rectangle.setValue1(sc.nextDouble());

rectangle.setValue2(sc.nextDouble());

System.out.println("Area of rectangle is "+df.format(rectangle.computeRectangleArea()));

break;

case 2:

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

Shape.Triangle triangle=new Shape.Triangle();

triangle.setValue1(sc.nextDouble());
triangle.setValue2(sc.nextDouble());

System.out.println("Area of triangle is "+df.format(triangle.computeTriangleArea()));

break;

default:

System.out.println("Invalid choice");

break;

……………………………………………………………………………………………………………………………………………………….

You might also like