ceng 111 exercise questions

CENG 111 EXERCISE QUESTIONS
1. Write a static method called countCapital that accepts a String parameter and returns
the number of times a capital letter is found in the string.
2. Write a static method called evenlyDivisible that accepts two integer parameters and
returns true if the first parameter is evenly divisible by the second, or vise versa, and false
otherwise. You may assume that neither parameter is zero.
3. Write a static method called randomInRange that accepts two integer parameters
representing a range. You may assume that the first parameter is less than or equal to the
second, and that both are positive. The method should return a random integer in the
specified range.
4. Write a static method called sumRange that accepts two integer parameters that represent
a range. You may assume the first parameter is less than or equal to the second. The
method should return the sum of the integers in that range.
5. Write a static method called uniqueNumbers that accepts an int[] as parameter and
returns the number of unique values in the array. E.g. 6 unique values in [3,5,7,4,3,7,9,2].
6. Write a static method called max2D that accepts an int[][] as parameter and returns the
maximum number in 2-dimensional array.
7. Write a static method called isConsecutiveFour that accepts an ArrayList<Integer> as
parameter and returns true if ArrayList contains 4 consecutive values in a row, false
otherwise. E.g. the ArrayList [3,5,8,4,-2,3,3,3,3,8] contains four consecutive “3’s” in a
row.
8. Write a static method called Circle that accepts the radius of a circle and prints the
circumference and area of the circle, rounded to 2 decimal places. The method returns
nothing.
9. Write a static method called differentRandom that accepts no parameter and prints 100
different random numbers in the range of [-1000,1000]. The method returns nothing.
10. Write a static method called countWords that accepts variable number of String
parameters and returns the total number of words in all of the Strings in the parameter.
E.g. countWords(“Hello world”, “CENG121”, “Java Software Solutions”) returns 6,
countWords(“Hello”, “world”, “Java”, “Software”, “Solutions”, “Final exam”) returns 7.
11. Write a static method called getLines that accepts a file name(String) as parameter and
returns a String array that contains lines of the file. Each element of the String array will
be the seperate lines of the file.
12. Write a class called Triangle that contains instance data a, b, c (int) for three side
lengths of the triange. Triangle class contains the following methods:
a. Define Triangle constructor that accepts three integer parameters and
initializes the instance data. All three parameters must be positive and must
satisfy the triangle inequality. The triangle inequality states that for any triangle,
the sum of the lengths of any two sides must be greater than the length of the
remaining side. If one of the parameters is not positive or the parameters are not
satisfying the triangle inequality, initialize all sides of the triangle to 10.
b. Define the copy constructor. Copy constructor accepts a Triangle object as
parameter and initalizes instance data with the sides of the Triangle in the
parameter.
c. Define getter methods for all three sides of the triangle.
d. Define setter methods for all three sides of the triangle. In setter methods if the
parameter is not positive or triangle inequality is violated, do not change the side
of the triangle.
e. Define toString method that returns a String containing the sides of the
triangle.
f. Define perimeter method that returns the perimeter of the triangle.
g. Define area method that returns the area of the triangle.
Area =
, where
h. Define isEquilateral method that returns true if the triangle is an equilateral
triangle, false otherwise. An equilateral triangle is a triangle in which all three
sides are equal.
i. Define isIsosceles method that returns true if the triangle is an isosceles
triangle, false otherwise. An isosceles triangle is a triangle that has two sides of
equal length.
j. Define isScalene method that returns true if the triangle is a scalene triangle,
false otherwise. In a scalene triangle, all sides are unequal.
k. Define isPythagorean method that returns true if the triangle is a
Pythagorean (right) triangle, false otherwise. A Pythagorean triangle is
a triangle that the square of the length of the one side (hypotenuse) equals the sum
of the squares of the lengths of the two other sides.
l. Define angleA, angleB and angleC methods that return the angle A, angle B,
and angle C of the triangle with using following formulas:
angle A = arccos((b2 + c2 – a2)/(2*b*c))
angle B = arccos((a2 + c2 – b2)/(2*a*c))
angle C = arccos((a2 + b2 – c2)/(2*a*b))
Note than acos method of Math class return the angle in radians. Use
toDegrees method to convert the angle to degrees.