You are on page 1of 31

Write a java program to rotate a matrix n times in clockwise and anti-clockwise direction using

inheritance.

Create a base class Clock.

From this extends a child class Clockwise and Anticlockwise. Both these child classes should contain the
method rotate().

Input format

The first line contains the number of rows

The second line contains the number of columns

The following lines contain the matrix as input rows and columns with space as separator between each
item in the matrix Then the following line contains the number of rotations to be performed

Refer Sample input

Output format

Print the matrix after performing the n number of clockwise and anticlockwise rotations

Refer sample output

Code constraints

0 < Row <=10

0<Column <=10

0< Number Of Rotations <=10

Sample testcases

123

456

789

Sample output

Clockwise

412

753

896
Anti clockwise

123

456

789

package virtusaInternship;

import java.util.*;

class Clock {
}

class Clockwise extends Clock {


int[][] mat1;
int rows, colms, r;

Clockwise(int rows, int colms, int r, int[][] mat1input) {


this.rows = rows;
this.colms = colms;
this.r = r;
this.mat1 = mat1input;
}

void rotation() {
while (r > 0) {
mat1 = rotate(rows, colms, mat1);
r -= 1;
}
System.out.println("Clockwise");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < colms; j++)
System.out.print(mat1[i][j] + " ");
System.out.print("\n");
}
}

int[][] rotate(int rows, int colms, int mat[][]) {


int row = 0, col = 0;
int prev, curr;
while (row < rows && col < colms) {

if (row + 1 == rows || col + 1 == colms)


break;
prev = mat[row + 1][col];
for (int i = col; i < colms; i++) {
curr = mat[row][i];
mat[row][i] = prev;
prev = curr;
}
row++;
for (int i = row; i < rows; i++) {
curr = mat[i][colms - 1];
mat[i][colms - 1] = prev;
prev = curr;
}
colms--;
if (row < rows) {
for (int i = colms - 1; i >= col; i--) {
curr = mat[rows - 1][i];
mat[rows - 1][i] = prev;
prev = curr;
}
}
rows--;
if (col < colms) {
for (int i = rows - 1; i >= row; i--) {
curr = mat[i][col];
mat[i][col] = prev;
prev = curr;
}
}
col++;
}
return mat;
}
}

class Anticlockwise extends Clock {


int[][] mat2;
int r;
int top, left;
int bottom, right;
int prev, curr;

Anticlockwise(int[][] mat2input, int r) {


mat2 = mat2input;
this.r = r;
}
void rotation() {
mat2 = rotate();
System.out.println("Anti clockwise");
for (int i = 0; i < mat2.length; i++) {
for (int j = 0; j < mat2.length; j++)
System.out.print(mat2[i][j] + " ");
System.out.print("\n");
}
}

int[][] rotate() {
for (int z = 0; z < r; z++) {
top = 0;
left = 0;
this.bottom = mat2.length - 1;
this.right = mat2[0].length - 1;
while (left < right && top < bottom) {
prev = mat2[top + 1][right];
for (int i = right; i > left - 1; i--) {
curr = mat2[top][i];
mat2[top][i] = prev;
prev = curr;
}
top += 1;
for (int i = top; i < bottom + 1; i++) {
curr = mat2[i][left];
mat2[i][left] = prev;
prev = curr;
}
left += 1;
for (int i = left; i < right + 1; i++) {
curr = mat2[bottom][i];
mat2[bottom][i] = prev;
prev = curr;
}
bottom -= 1;
for (int i = bottom; i > top - 1; i--) {
curr = mat2[i][right];
mat2[i][right] = prev;
prev = curr;
}
right -= 1;
}
}
return mat2;
}
}

public class MatrixRotation {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();
int colms = sc.nextInt();
int[][] matrix1 = new int[rows][colms];
int[][] matrix2 = new int[rows][colms];
sc.nextLine();
while (sc.hasNextLine()) {
for (int i = 0; i < rows; i++) {
String[] line = sc.nextLine().trim().split(" ");
for (int j = 0; j < line.length; j++) {
matrix1[i][j] = Integer.parseInt(line[j]);
}
}
break;
}
int r = sc.nextInt();
sc.close();
System.arraycopy(matrix1, 0, matrix2, 0, matrix1.length);
Clockwise clockwise = new Clockwise(rows, colms, r, matrix1);
Anticlockwise anticlockwise = new Anticlockwise(matrix2, r);
clockwise.rotation();
anticlockwise.rotation();
}
}
GST Calculation

Let's go for simple manipulation along with super class. This will be needed in our application as some
class share common attributes so they can be

grouped as child classes of some super class.

To try this let's create a parent class Event with following attributes,

Attribute type Data type


Name String
Details String
type String
ownerName String
costPErDay Int

Then create child class Exhibition that extends Event with the following

Attribute Datatype
noOfStall Integer

And create another child class StageEvent that extends Event with the

following attribute,

Attribute Datatype
noOfSeat Integer

Add suitable constructor and getters/setters for the classes. Get starting and ending date of the event
from user and calculate the total cost for the event. Then calculate GST for the event according to the
event type. GST is 5% for Exhibition and 15% for StageEvent.

Input format

First line of the input consists of an integer

Second,third and fourth line of the input consists of string

Fifth line of the input consists of a double value.

Next two lines of the input consists of starting and ending date of the event.( Refer sample input for the
exact format)

Sample testcases

Science exhibition

Exciting experiment
Fair

John

10

10/10/2017

12/10/2017

Output

1000.0

package virtusaInternship;

import java.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

class Event {
String name;
String detail;
String type;
String ownerName;
double costPerDay;
public Event(String name, String detail, String type, String ownerName, double
costPerDay) {

}
}

class Exhibition extends Event {


int noOfStall;
double gst;
public Exhibition(String name, String detail, String type, String ownerName,
double costPerDay, int noOfStall) {
super(name, detail, type, ownerName, costPerDay);
gst = costPerDay*0.05;
}
public double getGST() {
return gst;
}
}

class StageEvent extends Event {


int noOfSeats;
double gst;
public StageEvent(String name, String detail, String type, String ownerName,
double costPerDay, int noOfSeats) {
super(name, detail, type, ownerName, costPerDay);
this.noOfSeats = noOfSeats;
gst = costPerDay*0.05;
}

public double getGST() {


return gst;
}
}

public class GST {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
String detail = sc.nextLine();
String type = sc.nextLine();
String ownerName = sc.nextLine();
double costPerDay = sc.nextDouble();
int number = sc.nextInt();
sc.nextLine();
String startDate = sc.nextLine();
String endDate = sc.nextLine();
sc.close();
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate dateBefore = LocalDate.parse(startDate, format);
LocalDate dateAfter = LocalDate.parse(endDate, format);
long noOfDays = ChronoUnit.DAYS.between(dateBefore, dateAfter);

switch(choice) {
case 1:
Exhibition exhibition = new Exhibition(name, detail, type,
ownerName, costPerDay, number);
double gst = exhibition.getGST();
System.out.println(gst*noOfDays);
break;
case 2:
StageEvent stageEvent = new StageEvent(name, detail, type,
ownerName, costPerDay, number);
double gst1 = stageEvent.getGST();
System.out.println(gst1*noOfDays);
break;
default:
System.out.println("Invalid Input");
}
}
}
Considering the Banking Scenario, You have different types of accounts like Current Account, Savings
Account which inherits the base class Account.

Create a base class Account with the fields-String name, int number, int balance and Date startDate.

Create two subclasses CurrentAccount & Savings Account which extends Account.

Declare a method in Account class calculateInterest which would return the interest (double) and get
duedate (Date) as parameter. Since the Account class itself does not know how to compute
calculateinterest, we mark the method and class abstract.

In SavingsAccount & CurrentAccount - The interest is calculated as simple

interest. (Interest 12% for the savings account and 5% for the Current account.) Get the input from the
user and print calacuated interest. Refer sample input and output.

Sample input1

Karthick

101521502

7000

22/04/2013

22/04/2013

Output1

2520.0

Sample input2

Karthick

111521502

22/04/2014

22/04/2015

Output 2

200.0
package virtusaInternship;
import java.util.*;
import java.text.*;

abstract class Account {


private String name;
private int number;
private int balance;
private Date startDate;

public Account(String name, int number, int balance, Date startDate) {


this.name = name;
this.number = number;
this.balance = balance;
this.startDate = startDate;
}

public abstract double calculateInterest(Date dueDate);

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public int getNumber() {


return number;
}

public void setNumber(int number) {


this.number = number;
}

public int getBalance() {


return balance;
}

public void setBalance(int balance) {


this.balance = balance;
}

public Date getStartDate() {


return startDate;
}

public void setStartDate(Date startDate) {


this.startDate = startDate;
}
}

class SavingsAccount extends Account {


private static final double INTEREST_RATE = 0.12;
public SavingsAccount(String name, int number, int balance, Date startDate) {
super(name, number, balance, startDate);
}

@Override
public double calculateInterest(Date dueDate) {
long diff = dueDate.getTime() - getStartDate().getTime();
int days = (int) (diff / (1000 * 60 * 60 * 24));
return getBalance() * INTEREST_RATE * (days / 365);
}
}

class CurrentAccount extends Account {


private static final double INTEREST_RATE = 0.05;

public CurrentAccount(String name, int number, int balance, Date startDate) {


super(name, number, balance, startDate);
}

@Override
public double calculateInterest(Date dueDate) {
return getBalance() * INTEREST_RATE;
}
}

public class Main {


public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
int accountType = sc.nextInt();
sc.nextLine();

String name = sc.nextLine();


int number = sc.nextInt();
int balance = sc.nextInt();
sc.nextLine();

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");


Date startDate = df.parse(sc.nextLine());
Date dueDate = df.parse(sc.nextLine());

Account account = null;


if (accountType == 1) {
account = new SavingsAccount(name, number, balance, startDate);

} else if (accountType == 2) {
account = new CurrentAccount(name, number, balance, startDate);
}

System.out.println(account.calculateInterest(dueDate));
}
}
Super keyword - Constructor

We use Inheritance to group classes under some common Class for code reusability. In our application,
many exhibitions and Stage show may happen. Since they have common attributes like name, type,
organiser, those attributes are declared in Event class and both the Exhibition and Stage event inherit
them. You'll understand the difference between 'super' and 'this' keywords in this problem. Let's
implement them.

Create an Event class with protected attributes

Attribute type Data type


Name String
Details String
type String
ownerName String

Create an Exhibition class with following private attributes

Attribute Datatype
noOfStall Integer

Create a StageEvent class with following private attributes

Attribute Datatype
noOfSeat Integer

Now include appropriate getters and setters for all the classes. Include a default constructor and
parameterized constructor for all the classes.

The format for Parameterized constructors: public Event(String name, String detail, String type, String
organiserName)

public Exhibition(String name, String detail, String type, StringorganiserName, Integer noOfStalls)

public StageEvent(String name, String detail, String type, StringorganiserName, Integer noOfSeats)

Create a driver class called Main. In the Main method, obtain input from the user and create StageEvent
or Exhibition objects accordingly. At last print the details of the object appropriately.

Note: If choice other than specified is chosen, print "Invalid input" and

terminate.

[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.]

Input format

First line of the input consists of an integer


Next line of the input consists of the details of event in the CSV format.

Sample test case

Book expo,special,sale,Academics,Mahesh,10

Output

Book expo special sale Academics Mahesh 10


//package otherPrograms;
//import java.util.*;
abstract class Event {
protected String name;
protected String details;
protected String type;
protected String organiserName;

public Event() {
}

public Event(String name, String details, String type, String ownerName) {


this.name = name;
this.details = details;
this.type = type;
this.organiserName = ownerName;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getDetails() {


return details;
}

public void setDetails(String details) {


this.details = details;
}

public String getType() {


return type;
}

public void setType(String type) {


this.type = type;
}
public String getOrganiserName() {
return organiserName;
}

public void setOrganiserName(String ownerName) {


this.organiserName = ownerName;
}
}

class Exhibition extends Event {


private int noOfStalls;

public Exhibition() {
}

public Exhibition(String name, String details, String type, String ownerName,


int noOfStall) {
super(name, details, type, ownerName);
this.noOfStalls = noOfStall;
}

public int getNoOfStalls() {


return noOfStalls;
}

public void setNoOfStalls(int noOfStall) {


this.noOfStalls = noOfStall;
}

public String getOrganiserName() {


return organiserName;
}

public void setOrganiserName(String ownerName) {


this.organiserName = ownerName;
}
}

class StageEvent extends Event {


private int noOfSeats;

public StageEvent() {
}

public StageEvent(String name, String details, String type, String ownerName,


int noOfSeat) {
super(name, details, type, ownerName);
this.noOfSeats = noOfSeat;
}

public int getNoOfSeat() {


return noOfSeats;
}
public void setNoOfSeat(int noOfSeat) {
this.noOfSeats = noOfSeat;
}

public String getOrganiserName() {


return organiserName;
}

public int getNoOfSeats() {


return noOfSeats;
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
sc.nextLine();
String eventDetails = sc.nextLine();
sc.close();
String[] detailsArray = eventDetails.split(",");
if (choice == 1) {
Exhibition exhibition = new Exhibition(detailsArray[0],
detailsArray[1], detailsArray[2], detailsArray[3],
Integer.parseInt(detailsArray[4]));
System.out.println(exhibition.getName() + " " +
exhibition.getDetails() + " " + exhibition.getType() + " "
+ exhibition.getOrganiserName() + " " +
exhibition.getNoOfStalls());
} else if (choice == 2) {
StageEvent stageEvent = new StageEvent(detailsArray[0],
detailsArray[1], detailsArray[2], detailsArray[3],
Integer.parseInt(detailsArray[4]));
System.out.println(stageEvent.getName() + " " +
stageEvent.getDetails() + " " + stageEvent.getType() + " "
+ stageEvent.getOrganiserName() + " " +
stageEvent.getNoOfSeats());
} else {
System.out.println("Invalid input");
}
}
}
Area of shape

Write a program to calculate the area of the circle and rectangle using

overriding concept in java.

Create a class Shape with the following protected attributes,

Atrritbute Data type

area Double

Create the following method in the Shape class,

Method name

Public void computeArea()

Create a class Circle which extends Shape with the following private attributes,

Attribute Datatype

radius double

Override the following method in the Circle class,

Method name

Public void computeArea()

Create a class Rectangle which extends Shape with the following private

attributes,

Attribute Datatype

Length double

Breadth double

Override the following method in the Rectangle class,

Method name

Public void computeArea()

Create a class Triangle which extends Shape with the following private attributes,

Attribute Datatype
base double

height double

Override the following method in the Triangle class,

Public void computeArea()

Get the option for the shape to compute area and get the attribute according to the shape option.
Calculate the area and print the area.

While printing round off the area to 2 decimal formats. Create a driver class Main to test the above
classes.

[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]

Input format

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

The following inputs are Double which corresponds to the shape.

For Circle(Option 1) get the radius.

For Rectangle(Option 2) get the length and breadth. For Triangle(Option 3) get the base and height.

Output format

The output consists area of the shape. Refer sample output for formatting specifications.

Sample testcases

Input 1

11

Output 1

379.94

Input 2

10

25

Output 2

750.00
Input 2

15

29

Output 2

142.50

Input 2

Output 2

Invalid Input

package virtusaInternship;

import java.util.Scanner;

class Shape {
protected double area;
public void computeArea() { }
}

class Circle extends Shape {


private double radius;

public Circle(double radius) {


this.radius = radius;
}

@Override
public void computeArea() {
area = (3.14) * (radius * radius);
}
}

class Rectangle extends Shape {


private double length;
private double breadth;

public Rectangle(double length, double breadth) {


this.length = length;
this.breadth = breadth;
}
@Override
public void computeArea() {
area = length * breadth;
}
}

class Triangle extends Shape {


private double base;
private double height;

public Triangle(double base, double height) {


this.base = base;
this.height = height;
}

@Override
public void computeArea() {
area = 0.5 * base * height;
}
}

public class AreaCal {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int option = sc.nextInt();
if (option == 1) {
Circle c = new Circle(sc.nextDouble());
c.computeArea();
System.out.printf("%.2f\n", c.area);
} else if (option == 2) {
Rectangle r = new Rectangle(sc.nextDouble(), sc.nextDouble());
r.computeArea();
System.out.printf("%.2f\n", r.area);
} else if (option == 3) {
Triangle t = new Triangle(sc.nextDouble(), sc.nextDouble());
t.computeArea();
System.out.printf("%.2f\n", t.area);
} else {
System.out.println("Invalid Input");
}
}
}
Create 5 classes as shown in below diagram and create data members and methods as mentioned
below.

Person

Staff

Student

TeachingStaff

Non TeachingStaff

Person

1. name and birth Year

2. Parameterized constructor and overload toString method

Student

1. department and attendance percentage

2. Parameterized constructor and overload toString method

3. method to calculate whether the student is eligible to attend the exam or not

Staff

1.salary

2.parameterize constructor and overloaded toString method

TeachingStaff

1. subject and result percentage

2. Parameterized constructor and overload toString method 3. method to calculate new salary based on
result percentage

Non TeachingStaff

1. Lab and experience

2. Parameterized constructor and overload toString method 3. method to calculate new salary based on
experience
Create a Main class to test above classes based on below conditions.

1. Minimum attendance percentage required to attend exam is 75

2. Teaching staff new salary will be calculated based on their result percentage (i.e.if result percentage is
87, increment will be 8.7%)

3. Non teaching staff new salary will be calculated based on their experience (i.e.if experience is 2 years,
the increment will be 2%)

Input format

Person code (1 for student, 2 for Teaching Staff, 3 for Nonteaching staff)

Name

Year of birth

Next 2 or 3 lines are based on below conditions

Department and Attendance percentage if Person code is 1 Subject, Result percentage and salary if
Person code is 2

Lab Name, Experience and Salary if Person code is 3

Output format

Refer sample outputs

Sample testcases

Input 1

Kumar

1986

MCA

85

Output1

Name : Kumar

BirthYear : 1986

Department : MCA

Eligible : Yes
Input 2

Kumar

1986

Maths

90

50000

Output1

Name : Kumar

BirthYear : 1986

Old Salary : 50000.0

Subject : Maths

New Salary : 54500.0

Input 3

Kumar

1984

Maths Lab

15000

Output1

Name : Kumar

BirthYear : 1986

Old Salary : 50000.0

Lab : Maths

New Salary : 54500.0


Account Details

Multi level inheritance means inheriting a class which has already inherited another. So before going to
our application taking this fresh concept, let's try it out in a simple example first. We can create 3 classes
where one is the parent, next is the child and the third is the child of the child class. Take an example of
account handling application, where you get details from the user in comma separated format and save
them as objects and then display them

Create a class Account with the following protected attributes,

Create a class SavingAccount which extends Account with the following

protected attributes,

Create a class FixedAccount which extends SavingAccount with the following private attributes,

Create a class AccountBO with the following methods,

accountName Strring

balance Double

accountHolder String

minimumBalance Double

lockingPeriod Integer

method name getAccountDetails(String details) this method takes the acoount detaiinls and prnt them

Get the FixedAccount detail from the user as a comma seperated value, the account detail should be
given in the below format, accountNumber,balance,accountHolderName,minimunBalance,locking
Period Split the details and Display the details in the below format, System.out.format("%-20s %-10s %-
20s %-20s %s\n","Account Number", "Balance","Account holder name","Minimum balance","Locking
period")

Create a driver class Main to test the above classes.

[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]

Input formate

The account detail should be given in the below format,


accountNumber,balance,accountHolderName,minimun Balance,locking Period.

Output format

The output consists of the fixed account detail.

Sample testcases
Input 1

ACC001,5456,45,Tony Blake,10

Output 1
Account Number Balance Account holder name Minimum balance Locking
period
ACC001 5456.45 Tony Blake 500.0 10

package virtusaInternship;

import java.util.Scanner;

class Account {
protected String accountNumber;
protected double balance;
protected String accountHolder;

public Account(String accountNumber, double balance, String accountHolder) {


this.accountNumber = accountNumber;
this.balance = balance;
this.accountHolder = accountHolder;
}
}

class SavingAccount extends Account {


protected double minimumBalance;

public SavingAccount(String accountNumber, double balance, String


accountHolder, double minimumBalance) {
super(accountNumber, balance, accountHolder);
this.minimumBalance = minimumBalance;
}
}

class FixedAccount extends SavingAccount {


int lockingPeriod;

public FixedAccount(String accountNumber, double balance, String


accountHolder, double minimumBalance,
int lockingPeriod) {
super(accountNumber, balance, accountHolder, minimumBalance);
this.lockingPeriod = lockingPeriod;
}
}

class AccountBO {
public static void getAccountDetails(String details) {
String[] accountDetails = details.split(",");
FixedAccount fixedAccount = new FixedAccount(accountDetails[0],
Double.parseDouble(accountDetails[1]),
accountDetails[2], Double.parseDouble(accountDetails[3]),
Integer.parseInt(accountDetails[4]));
System.out.format("%-20s %-10s %-20s %-20s %s\n", "Account Number",
"Balance", "Account holder name",
"Minimum balance", "Locking period");
System.out.format("%-20s %-10s %-20s %-20s %s\n",
fixedAccount.accountNumber, fixedAccount.balance,
fixedAccount.accountHolder, fixedAccount.minimumBalance,
fixedAccount.lockingPeriod);
}
}

public class AccountDetails {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String details = scanner.nextLine();
AccountBO.getAccountDetails(details);
scanner.close();
}
}
Reward calculation

A new announcement has been made by the Mayor, the Fair will be on for more. than a month. For
rewarding customers who actively purchase in the fair, the developers are asked to compute reward
points for edit card purchasing. For a small demo implementation, we now compute reward points for
VISA card and HP VISA card. The reward points for VISA card is 1% of the spending for all kinds of
purchases. For HP Visa card, 10 additional points are given for fuel purchases. Also, include method
Overriding for the method

computeRewardPoints() which computes the reward points for both types.

write a program using the above specification for computing the reward points.

Create a class named VISACard with the following private attributes

holderName Striing

ccv String

the VISACaard class has the following method

public double computerRewardPoint(String purchesType,Double amount) this method accept the type
and amount as input and the display the rewas points which is 1% of the amount

the HPVISACard class has the following method

public double computerRewardPoint(String purchesType,Double amount) this method accept the type
and amount as input and the display the rewas points which is 1% of the amount if the type is fule 10
more points are given

Create a driver class called Main. In the Main method, obtain inputs from the user and compute the
reward points by calling appropriate methods. If choice other than specified is chosen, print "Invalid
choice" Hint: Call Super() to access the computeReward Points in the derived class and

then add additional points if given criteria qualifies.

Note: Display one digit after the decimal point for Double values.

[Strictly adhere to the Object-Oriented Specifications given in the problem statement. All class names,
attribute names and method names should be the same asspecified in the problem statement.]

Input format

The first line of the input consists of the holder name.

The second line consists of the CCV number. The third line consists of the bill amount.

The fourth line consists of the type.

The fifth line of the input consists of the choice.

Output format
The output displays the reward points.

Sample testcases

Input 1

Shubham

4548 4545 5484

1000

Fuel

Output 1

10.0

Input2

Shubham

4548 4545 5484

1000

Fuel

Output 2

20.0

Input 3

Shubham

4548 4545 5484

1000

Fuel

Output 3

Invalid
package virtusaInternship;

import java.util.Scanner;

class VISACard {
private String holderName;
private String ccv;

public VISACard(String holderName, String ccv) {


this.holderName = holderName;
this.ccv = ccv;
}

public double computeRewardPoint(String purchaseType, Double amount) {


if(purchaseType == "Fuel")
return amount * 0.01+10;
else return amount * 0.01;
}
}

class HPVISACard extends VISACard {


public HPVISACard(String holderName, String ccv) {
super(holderName, ccv);
}

@Override
public double computeRewardPoint(String purchaseType, Double amount) {
if (purchaseType.equalsIgnoreCase("Fuel")) {
return super.computeRewardPoint(purchaseType, amount) + 10;
} else {
return super.computeRewardPoint(purchaseType, amount);
}
}
}

public class Card {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

String holderName = sc.nextLine();

String ccv = sc.nextLine();

Double amount = sc.nextDouble();


sc.nextLine();

String purchaseType = sc.nextLine();

int choice = sc.nextInt();


sc.close();

VISACard visaCard = new VISACard(holderName, ccv);


HPVISACard hpVisaCard = new HPVISACard(holderName, ccv);
if (choice == 1) {
System.out.format("%.1f", visaCard.computeRewardPoint(purchaseType,
amount));
} else if (choice == 2) {
System.out.format("%.1f", hpVisaCard.computeRewardPoint(purchaseType,
amount));
} else {
System.out.println("Invalid choice");
}
}
}

You might also like