Faculty of Applied and Computer Sciences
ICT Department
ASDSY3A / ASBDY3A - Development Software 2.2 – Java
EXERCISES
JAVA PRACTICAL EXERCISES
1. Write a Java program to convert radian to degree.
The formula is : degrees= radians*180/𝑝𝑖
You should know the value of pi, make it a constant value
Your program must use Scanner object to accept the radian value
Note: use Math.PI for the pi value
Sample output
Enter Radian: 0.25
The degree is: 14.32394487827058
The degree formatted is: 14.32
2. Write a Java program to calculate the area of a trapezoid.
Note : A trapezoid is a quadrilateral with two sides parallel. The trapezoid
is equivalent to the British definition of the trapezium. An isosceles
trapezoid is a trapezoid in which the base angles are equal so the formula
is given in the figure below. Your variables must take float input values,
using input function.
1
Sample output
Enter Height of trapezoid: 6
Enter Base one value: 10
Enter Base two value: 5
Area is: 45.0
3. Write a Java program to calculate surface volume and area of a cylinder.
Note: A cylinder is one of the most basic curvilinear geometric shapes,
the surface formed by the points at a fixed distance from a given straight
line, the axis of the cylinder.
Note: use Math.pow ie Math.pow(r,2)
Sample Output: Note format the output values to 2 decimal
Enter Height of cylinder: 4
Enter Radius of cylinder: 6
Volume is: 452.57142857142856
Surface Area is: 377.1428571428571
2
4. Write a Java program which accepts the user's first and last name and
print them in reverse order with a space between them.
Sample Output
Input your First Name : Sam
Input your Last Name : Mkhwanazi
Hello Mkhwanazi Sam
5. Write a Java program that will accept the base and height of a triangle
and compute the area.
Area of a triangle
A triangle is a polygon with three edges and three vertices. It is one of the
basic shapes in geometry. A triangle with vertices A, B, and C is denoted
triangle ABC.
Vertex of a triangle: The point at which two sides of a triangle
meet.
Altitude of a triangle: The perpendicular segment from a vertex of a
triangle to the line containing the opposite side.
Base of a triangle: The side of a triangle to which an altitude is
drawn.
Height of a triangle: The length of an altitude.
6. Create a java class called Student with the following:
Attributes
- Name, subjcode, test1, test2, test3
Methods
3
- Overloaded constructors
Default constructor – assign defaults values to attributes
and parameterized constructor – assign parameter values to attributes
- Setter and getter methods for all attributes
- The function called calcSemesterMark() that receive test1, test2 and test3
as parameters and return calculated Semester. To calculate semester
mark, you must divide the total of all 3 tests by 3.
- A toString() method that returns all the instance variables in one line,
separated by tabs.
Code a class called StudentTester (the application-(Java Main Class)) to test the
Student class. Your application should perform the following functionalities:
- Get the inputs for all instance variables, see screenshot below
- Instantiate an object of the Student class
- Invoke calcSemesterMark() function from Student class and pass correct
parameters
- Print headings with correct tabs
- Invoke (call) the toString() method to display name, subject code, all tests
and calculated semester mark.
****************END EXERCISES******************