0% found this document useful (0 votes)
2 views6 pages

Exercise Static

Uploaded by

freepubg3245
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Exercise Static

Uploaded by

freepubg3245
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java OOP Practice Questions

Static Variables and Static Methods

General Instructions
Students must follow the rules below:

1. Use at least one static variable.

2. Use at least one static method.

3. Use private data fields where needed.

4. Use constructor if object initialization is needed.

5. Display output clearly.

6. Do not write all logic inside the main() method.

Question 1: Count Number of Objects


Create a class named Student.
The class should contain:

ˆ private variable name

ˆ private variable id

ˆ static variable count

Use a constructor to initialize student name and id.


Increase the value of count whenever a new object is created.
Create a static method named showCount() to display the total number of students.
In the main() method, create three student objects.

Sample Output
Total Students : 3

1
Question 2: University Name Shared by All Students
Create a class named StudentInfo.
The class should contain:
ˆ private variable name

ˆ private variable id

ˆ static variable universityName

Use a constructor to initialize name and id.


Use a static method named setUniversityName() to change the university name.
Use another method named display() to show student details.
In the main() method, create two student objects and display their information.

Sample Output
Name : Rahim
ID : 101
University : DSU

Name : Karim
ID : 102
University : DSU

Question 3: Static Method for Addition


Create a class named MathOperation.
The class should contain a static method named add().
The method should take two integer values and return their sum.
In the main() method, call the static method without creating an object.

Sample Output
First Number : 10
Second Number : 20
Sum : 30

Question 4: Static Method for Circle Area


Create a class named CircleCalculator.
The class should contain:
ˆ static variable pi

ˆ static method calculateArea()

The method should take radius as a parameter and return the area of a circle.

2
Formula
Area = πr2
In the main() method, call the static method using radius 5.

Sample Output
Radius : 5.0
Area of Circle : 78.54

Question 5: Static Method for Simple Interest


Create a class named InterestCalculator.
The class should contain a static method named calculateSimpleInterest().
The method should take principal, rate, and time as parameters.

Formula
P ×R×T
SI =
100
Call the method from main() and display the result.

Sample Output
Principal : 10000.0
Rate : 5.0
Time : 2.0
Simple Interest : 1000.0

Question 6: Static Variable for Bank Interest Rate


Create a class named BankAccount.
The class should contain:

ˆ private variable accountHolder

ˆ private variable balance

ˆ static variable interestRate

Use a constructor to initialize account holder and balance.


Create a static method named setInterestRate() to update the interest rate.
Create a method named calculateInterest() to calculate interest for each account.

3
Formula
Balance × InterestRate
Interest =
100
Create two bank account objects and display interest for both.

Sample Output
Account Holder : Rahim
Balance : 50000.0
Interest Rate : 6.0
Interest : 3000.0

Account Holder : Karim


Balance : 30000.0
Interest Rate : 6.0
Interest : 1800.0

Question 7: Static Method for Temperature Conver-


sion
Create a class named TemperatureConverter.
The class should contain a static method named celsiusToFahrenheit().
The method should take Celsius temperature as a parameter.

Formula
9
F = C + 32
5
Call the method from main() using Celsius value 37.

Sample Output
Celsius : 37.0
Fahrenheit : 98.6

Question 8: Static Variable for Gravitational Acceler-


ation
Create a class named PotentialEnergy.
The class should contain:

ˆ private variable mass

ˆ private variable height

4
ˆ static variable gravity
Use a constructor to initialize mass and height.
Create a static method named setGravity() to update gravity if needed.
Create a method named calculatePotentialEnergy().

Formula
P E = mgh
Create one object using mass 5 and height 10.

Sample Output
Mass : 5.0 kg
Height : 10.0 m
Gravity : 9.8 m / s ^2
Potential Energy : 490.0 J

Question 9: Static Method for Maximum of Three


Numbers
Create a class named NumberUtility.
The class should contain a static method named findMaximum().
The method should take three integer values as parameters and return the largest
number.
Call the method from main() using the values 25, 40, and 15.

Sample Output
First Number : 25
Second Number : 40
Third Number : 15
Maximum Number : 40

Question 10: Static Variable for Product Tax Rate


Create a class named Product.
The class should contain:
ˆ private variable productName
ˆ private variable price
ˆ static variable taxRate
Use a constructor to initialize product name and price.
Create a static method named setTaxRate() to update the tax rate.
Create a method named calculateFinalPrice().

5
Formula
P rice × T axRate
F inalP rice = P rice +
100
Create two product objects and display their final prices.

Sample Output
Product Name : Laptop
Price : 60000.0
Tax Rate : 5.0
Final Price : 63000.0

Product Name : Mouse


Price : 1000.0
Tax Rate : 5.0
Final Price : 1050.0

You might also like