Java Exercises Set1
Java Exercises Set1
1. Write a program that calculates the prime number up to the given integer.
2. Sales tax in New York City is 8.25%. Write a program that accepts a price on
the command line and prints out the appropriate tax and total purchase
price.
3. Modify the sales tax program to accept an arbitrary number of prices, total
them and calculate the sales tax and print the total amount.
5. Modify the previous program to add another method getDay() takes number
of days as input, adds this value to the current date and returns the day of
the resultant date as output.
6. Add tThe third method getMonth() to the same program that takes the same
input(number of days), adds this to the current date and returns the month of
the resultant date as output.
7. Return the "centered" average of an array of ints, which we'll say is the mean
average of the values, except not counting the largest and smallest values in
the array. Use int division to produce the final average. You may assume that
the array is of length 3 or more.
centeredAverage({1, 2, 3, 4, 100}) ? 3
8. Write a class that keeps a running total of all characters passed to it (one at a
time) and throws an exception if it is passed a non-alphabetic character.
Level 2:
1. Create a Circle class that contains a radius field. Give it a constructor where
you pass in the radius. Have your test routine create a few circles, assign a
value to the radius, then print out some information about the circles. Put
your Circle class and your test routine in two separate classes, like this:
Circle.java
public class Circle {
public double radius;
...
}
CircleTest.java
public class CircleTest {
public static void main(String[] args) {
...
}
}
• Give your Circle a getArea method that calculates its area, and a
printInfo method that prints out the radius and area.
• Make a program that creates an array of 100 circles, each with a
random radius. Print out the sum of the areas of the 100 circles. Also
print the biggest and smallest areas.
• Create a Rectangle class that contains width and height fields. Also
give it a getArea method.
• Create a Square class with width and getArea. Then, give both Square
and Circle setArea methods that let you specify a desired area.
• Have your Circle and Rectangle inherit from a common Shape class.
Change all your existing classes so that the fields are private and you
have getXxx and setXxx methods to lookup and change the values of
the fields. Make a method that will take an array of Shape objects and
sum their areas.
2. Create a Fraction class that will extend the abstract class Number in java.lang
package. The Fraction class will consist of a method to add two Fraction class
objects and a method to represent a Fraction object as String
3. Write a program to generate a random int in some large range. Make an
ArrayList containing that many Circle objects. Give each Circle a random
radius.
4. Loop down the list in the previous program and write a program to find the
Circle with the biggest and the smallest area. Print the result with exactly
three numbers after the decimal point.
5. Write a program to find all pairs of integers such that their sum is equal to
the given integer number N and the second number results from the first one
by striking out one of its digits. The first integer always has at least two digits
and starts with a non-zero digit. The second integer always has one digit less
than the first integer and also starts with a non-zero digit. The input number
consists of a single integer N (10 <= N <= 100000). If the given input
doesn’t satisfy the given constraints then return -1.
Constraints
• The first integer must contain same number of digits of given
input.
• The second integer must be less than one digits of given input.
Example 1:
Input
Value : 256
Combinations are,
1. 233 + 23
2. 228 + 28
3. 214 + 42
4. 178 + 78
Output
No. of Combinations : 4
Example 2:
Input
Value : 7567
Combinations are,
1. 7324 + 243
2. 7289 + 278
3. 7280 + 287
4. 7243 + 324
5. 6869 + 698
6. 6807 + 760
7. 6798 + 769
8. 6781 + 786
9. 6698 + 869
Output
No. of Combinations : 9
Level 3:
1. Write a coin-flipping class using Thread. The program to start 5 threads, have
each flip 1000 coins and print out whenever they get 3 consecutive heads. In
the printouts, you can use the Thread getName method to identify the
thread. Write the same thing with Runnable.
2. A person may play one or more of the following games (cricket, football,
basketball, tennis). Write a program that takes the player name as input and
displays the game he plays. Design the most optimal way to store this
information