Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views2 pages

Questions

The document outlines several Java programming tasks, including calculating electricity bills based on connection types, implementing a sports academy interface for different sports, creating a leap year checker with an inner class, validating applicant names for vowels, ensuring a list of numbers is negative, simulating EPFO account contributions with multithreading, and managing a Post Office service counter with customer threads. Each task includes specific requirements for class structures, methods, and exception handling. The document serves as a comprehensive guide for implementing various object-oriented programming concepts in Java.

Uploaded by

harshanadapa07
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)
10 views2 pages

Questions

The document outlines several Java programming tasks, including calculating electricity bills based on connection types, implementing a sports academy interface for different sports, creating a leap year checker with an inner class, validating applicant names for vowels, ensuring a list of numbers is negative, simulating EPFO account contributions with multithreading, and managing a Post Office service counter with customer threads. Each task includes specific requirements for class structures, methods, and exception handling. The document serves as a comprehensive guide for implementing various object-oriented programming concepts in Java.

Uploaded by

harshanadapa07
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/ 2

1. Write a Java program to calculate electricity bills based on the type of electricity connection.

There
are two types of connections: Commercial and Domestic. The rate per unit varies depending on the
type of connection:
 Commercial Connection: ₹5.00 per unit
 Domestic Connection: ₹2.60 per unit
 Create an abstract class Plan with an instance variable rate to store the rate per unit.
 Define an abstract method getRate() in Plan, which will be implemented by its subclasses.
 Create two subclasses: CommercialPlan (sets the rate to ₹5.00 per unit), DomesticPlan (sets
the rate to ₹2.60 per unit)
 Implement a class Calculate, which will calculate and display the electricity bill amount
based on the number of units consumed.
 Use an abstract class reference to refer to objects of CommercialPlan and DomesticPlan
while calculating the bill.

2. A sports academy organizes different sports activities, including Football, Volleyball, and
Basketball. Each sport follows a standard way to start the game, but the method of playing differs
based on the sport. To maintain consistency, the academy wants a structured approach where all
sports should implement a common interface for playing the game. Create an interface Playable
with a method of void play(). Implement three classes: Football, Volleyball, and Basketball that
implement the Playable interface. Each class should override the play() method to print a message
indicating the respective sport is being played. Write a Java program that demonstrates
polymorphism by calling the play() method for each sport.

3. You are developing a Calendar Utility application in Java that helps users determine whether a
given year is a leap year. To ensure encapsulation and modular design, you decide to implement
this feature using an inner class.
a) Implement an outer class named LeapYearChecker, which contains an inner class
named Year that takes an integer value representing a year. A method inside the Year
class called isLeap() determines whether the given year is a leap year using the
standard leap year conditions, a year is a leap year if it is divisible by 4 but not
divisible by 100, except when it is also divisible by 400.
b) In the main method Prompt the user to enter a year. Create an instance of the inner
class Year and invoke the isLeap() method. Display an appropriate message based on
the result.

4. A software company validates the applicant's name that contains at least one vowel (a, e, i, o, u). If
the input string does not contain a vowel, the application should throw an appropriate exception to
notify the user. As a Java developer, you are tasked with implementing a method
checkVowels(String input) that takes a string as input and throws an IllegalArgumentException if
the string does not contain any vowels. Write a Java program that Defines a method
checkVowels(String input) that checks if the input string contains at least one vowel. If the string
contains vowels, print "Valid input: [input]". If the string does not contain vowels, throw an
IllegalArgumentException with the message "Input must contain at least one vowel." Write a
main method to test your function with different inputs.

5. A Negative Number Validator program in Java that reads a list of numbers and ensures that all
numbers are negative or zero. If any positive number is found, the program should throw and
handle a custom exception named PositiveNumberException.

Page 1 of 2
a. Implement a custom exception class PositiveNumberException that extends
Exception and takes a message as a parameter.
b. Create a class NegativeNumberChecker that contains: A method
validateNumbers(String filename) that reads numbers from a file and checks for any
positive numbers. If found, throw PositiveNumberException.
c. In the main method: Read the numbers from a file (one number per line). Call
validateNumbers() to check for positives. If a positive number is found, catch the
exception and display an appropriate message. Otherwise, print "All numbers are
negative or zero."

6. The EPFO needs a multi-threaded application to simulate monthly PF contributions and manage
employee accounts safely. Implement the following in your Java program:
1. Create a class EPFOAccount with fields: employeeId, name, basicSalary, pfBalance.
2. Use a synchronized instance method to safely update the PF balance
(updateMonthlyContribution()).
3. Use a synchronized block inside the displayAccountDetails() method to ensure consistent
output.
4. Track the number of EPFO accounts using a static variable. Use a static synchronized
method to return this count (getAccountCount()).
5. Create multiple threads to simulate concurrent PF contributions for multiple months.
6. Display final account details and the total number of accounts.

7. In a local Post Office, there are multiple customers who want to access a single service counter to
send parcels. However, the counter can serve only one customer at a time. You are required to
simulate this scenario using multithreading in Java. Write a Java program using the Thread class
to simulate the above scenario with the following requirements:
 Create a class PostOfficeCounter that has a method serveCustomer(String
customerName) which simulates serving a customer by printing messages and using
Thread.sleep() to mimic processing time.
 Ensure that only one customer is served at a time by synchronizing the serveCustomer
method.
 Create a class Customer that extends Thread and represents each customer. Each customer
thread should try to access the PostOfficeCounter.
 Create a main method where you simulate at least 3 customer threads accessing the same
PostOfficeCounter.

Page 2 of 2

You might also like