Professional Documents
Culture Documents
inheritance.
From this extends a child class Clockwise and Anticlockwise. Both these child classes should contain the
method rotate().
Input format
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
Output format
Print the matrix after performing the n number of clockwise and anticlockwise rotations
Code constraints
0<Column <=10
Sample testcases
123
456
789
Sample output
Clockwise
412
753
896
Anti clockwise
123
456
789
package virtusaInternship;
import java.util.*;
class Clock {
}
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() {
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;
}
}
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
To try this let's create a parent class Event with following attributes,
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
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) {
}
}
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.
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.*;
@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);
}
}
@Override
public double calculateInterest(Date dueDate) {
return getBalance() * INTEREST_RATE;
}
}
} 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.
Attribute Datatype
noOfStall Integer
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
Book expo,special,sale,Academics,Mahesh,10
Output
public Event() {
}
public Exhibition() {
}
public StageEvent() {
}
Write a program to calculate the area of the circle and rectangle using
area Double
Method name
Create a class Circle which extends Shape with the following private attributes,
Attribute Datatype
radius double
Method name
Create a class Rectangle which extends Shape with the following private
attributes,
Attribute Datatype
Length double
Breadth double
Method name
Create a class Triangle which extends Shape with the following private attributes,
Attribute Datatype
base double
height double
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
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() { }
}
@Override
public void computeArea() {
area = (3.14) * (radius * radius);
}
}
@Override
public void computeArea() {
area = 0.5 * base * height;
}
}
Person
Staff
Student
TeachingStaff
Non TeachingStaff
Person
Student
3. method to calculate whether the student is eligible to attend the exam or not
Staff
1.salary
TeachingStaff
2. Parameterized constructor and overload toString method 3. method to calculate new salary based on
result percentage
Non TeachingStaff
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.
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
Department and Attendance percentage if Person code is 1 Subject, Result percentage and salary if
Person code is 2
Output format
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
Subject : Maths
Input 3
Kumar
1984
Maths Lab
15000
Output1
Name : Kumar
BirthYear : 1986
Lab : Maths
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
protected attributes,
Create a class FixedAccount which extends SavingAccount with the following private attributes,
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")
[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
Output format
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;
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);
}
}
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
write a program using the above specification for computing the reward points.
holderName Striing
ccv String
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
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
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 second line consists of the CCV number. The third line consists of the bill amount.
Output format
The output displays the reward points.
Sample testcases
Input 1
Shubham
1000
Fuel
Output 1
10.0
Input2
Shubham
1000
Fuel
Output 2
20.0
Input 3
Shubham
1000
Fuel
Output 3
Invalid
package virtusaInternship;
import java.util.Scanner;
class VISACard {
private String holderName;
private String 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);
}
}
}