a. Write a Java program to compute the area of a polygon.
Area of a polygon = (n*s^2)/(4*tan(π/n))
where n is n-sided polygon and s is the length of a side
Input Data:
Input the number of sides on the polygon: 7
Input the length of one of the sides: 6
b. Explain each of the following Java programming terms
i. extended [1]
ii. protected [1]
iii. instanceOf [1]
iv. abstract [1]
v. Write a Java program to print the contents of a two-dimensional Boolean array where t will represent true and f will represent false.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input the number of sides on the polygon: ");
int n = in.nextInt();
System.out.print("Input the length of one of the sides: ");
int s = in.nextInt();
System.out.println("Area of a polygon = " + (n * s * s) / (4 * Math.tan(Math.PI / n)));
}
}
1 It means that the class inherited the functionality of the parent class and extended it with its own functionality.
2. One of the types of encapsulation means that a method or variable can be used either by inherited classes or by classes located in the same package.
3. Checks if an object is an object of some class.
4. The abstract keyword in the class name means that the class contains methods that must be implemented in the inherited class, you cannot create an object of such a class. If the method signature contains the abstract keyword, it means that such a method must be implemented in the inherited class.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
boolean[][] arr = {{true, false, true}, {false, true, false}, {true, false, true}};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] ? "t " : "f ");
}
System.out.println();
}
}
}
Comments
Leave a comment