0% found this document useful (0 votes)
39 views12 pages

Lab 26

The document contains multiple Java classes that demonstrate various programming tasks, including checking even/odd numbers, calculating areas and volumes of geometric shapes, determining if a set of sides can form a triangle, and calculating taxes based on age and salary. Each task is structured with a main method that tests the functionality of the defined methods. The classes also include recursive methods for summing digits and printing numbers in sequence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views12 pages

Lab 26

The document contains multiple Java classes that demonstrate various programming tasks, including checking even/odd numbers, calculating areas and volumes of geometric shapes, determining if a set of sides can form a triangle, and calculating taxes based on age and salary. Each task is structured with a main method that tests the functionality of the defined methods. The classes also include recursive methods for summing digits and printing numbers in sequence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#TASK1

A
public class Main {
public static void evenChecker(int number) {
if (number % 2 == 0) {
[Link]("Even!!");
} else {
[Link]("Odd!!");
}
}

public static void main(String[] args) {


evenChecker(10);
evenChecker(17);
}
}

B
public class Main {
public static boolean isEven(int number) {
if (number % 2 == 0) {
return true;
} else {
return false;
}
}

public static void main(String[] args) {


boolean result = isEven(10);
[Link](result);

result = isEven(17);
[Link](result);
}
}

C
public class Main {
public static boolean isPos(int number) {
if (number > 0) {
return true;
} else {
return false;
}
}

public static void main(String[] args) {


boolean result = isPos(-5);
[Link](result);

result = isPos(12);
[Link](result);
}
}

D
public class Main {
public static boolean isEven(int number) {
if (number % 2 == 0) {
return true;
} else {
return false;
}
}

public static boolean isPos(int number) {


if (number > 0) {
return true;
} else {
return false;
}
}

public static void sequence(int n) {


if (isPos(n)) {
for (int i = 0; i <= n; i++) {
if (isEven(i)) {
[Link](i + " ");
}
}
} else {
for (int i = n; i < 0; i++) {
if (!isEven(i)) {
[Link](i + " ");
}
}
}
[Link]();
}

public static void main(String[] args) {


sequence(10);
sequence(-7);
sequence(7);
sequence(-8);
}
}
#TASK2

A
public class Main {
public static double circleArea(int radius) {
double area = [Link] * radius * radius;
return area;
}

public static void main(String[] args) {


double area = circleArea(5);
[Link](area);
}
}

B
public class Main {
public static double sphereVolume(int radius) {
double volume = (4.0 / 3.0) * [Link] * radius * radius * radius;
return volume;
}

public static void main(String[] args) {


double volume = sphereVolume(5);
[Link](volume);
}
}

C
public class Main {

public static double circleArea(int radius) {


return [Link] * radius * radius;
}

public static double sphereVolume(int radius) {


return (4.0 / 3.0) * [Link] * radius * radius * radius;
}

public static void findSpace(int diameter, String shape) {


int radius = diameter / 2;

if ([Link]("circle")) {
double area = circleArea(radius);
[Link]("%.4f\n", area);
} else if ([Link]("sphere")) {
double volume = sphereVolume(radius);
[Link]("%.4f\n", volume);
} else {
[Link]("Wrong Parameter");
}
}

public static void main(String[] args) {


findSpace(10, "circle");
findSpace(5, "sphere");
findSpace(10, "square");
}
}

#TASK3

A
public class Main {
public static boolean isTriangle(int side1, int side2, int side3) {
if (side1 + side2 > side3 && side2 + side3 > side1 && side3 + side1 >
side2) {
return true;
} else {
return false;
}
}

public static void main(String[] args) {


boolean res = isTriangle(7, 5, 10);
[Link](res);
res = isTriangle(3, 2, 1);
[Link](res);
}
}

B
public class Main {

public static boolean isTriangle(int side1, int side2, int side3) {


if (side1 + side2 > side3 && side2 + side3 > side1 && side3 + side1 >
side2) {
return true;
} else {
return false;
}
}

public static void triArea(int a, int b, int c) {


if (isTriangle(a, b, c)) {
double s = (a + b + c) / 2.0;
double area = [Link](s * (s - a) * (s - b) * (s - c));
[Link]("%.3f\n", area);
} else {
[Link]("Can’t form triangle");
}
}

public static void main(String[] args) {


triArea(3, 2, 1);
triArea(7, 5, 10);
}
}

#TASK4
public class Main {
public static boolean isPrime(int number) {

if (number < 2) {
return false;
}

for (int i = 2; i <= [Link](number); i++) {


if (number % i == 0) { /
return false;
}
}

return true;
}

public static void main(String[] args) {


boolean check = isPrime(7);
[Link](check);

check = isPrime(15);
[Link](check);
}
}

B
public class Main {
public static void main(String[] args) {
boolean check1 = isPerfect(6);
[Link](check1); // true

boolean check2 = isPerfect(33);


[Link](check2); // false
}

public static boolean isPerfect(int number) {


if (number <= 1) {
return false;
}

int sum = 0;

for (int i = 1; i <= number / 2; i++) {


if (number % i == 0) {
sum += i;
}
}

if (sum == number) {
return true;
} else {
return false;
}
}
}

C
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter an integer: ");
int n = [Link]();

int result = special_sum(n);


[Link](result);
}

public static boolean isPrime(int number) {


if (number <= 1) {
return false;
}
for (int i = 2; i <= [Link](number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
public static boolean isPerfect(int number) {
if (number <= 1) {
return false;
}
int sum = 0;
for (int i = 1; i <= number / 2; i++) {
if (number % i == 0) {
sum += i;
}
}
return sum == number;
}

public static int special_sum(int n) {


int sum = 0;
for (int i = 1; i <= n; i++) {
if (isPrime(i) || isPerfect(i)) {
sum += i;
}
}
return sum;
}
}

#TASK5

A
public class Main {
public static void main(String[] args) {

showDots(5);
[Link]();
showDots(3);
}

public static void showDots(int number) {


// Loop to print dots
for (int i = 0; i < number; i++) {
[Link](".");
}
}
}

B
public class Main {
public static void main(String[] args) {

show_palindrome(5);
[Link]();
show_palindrome(3);
}

public static void show_palindrome(int number) {


for (int i = 1; i <= number; i++) {
[Link](i);
}

for (int i = number - 1; i >= 1; i--) {


[Link](i);
}
}
}

C
public class Main {
public static void main(String[] args) {

showDiamond(5);
[Link]();
showDiamond(3);
}

public static void show_palindrome(int number) {

for (int i = 1; i <= number; i++) {


[Link](i);
}

for (int i = number - 1; i >= 1; i--) {


[Link](i);
}
}

public static void showDiamond(int n) {

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= n - i; j++) {


[Link](".");
}

show_palindrome(i);

for (int j = 1; j <= n - i; j++) {


[Link]("."); // Print dot
}

[Link]();
}

for (int i = n - 1; i >= 1; i--) {


// Print leading dots
for (int j = 1; j <= n - i; j++) {
[Link](".");
}
show_palindrome(i);

// Print trailing dots


for (int j = 1; j <= n - i; j++) {
[Link](".");
}

[Link]();
}
}
}

#TASK6

A
public class Main {
public static void main(String[] args) {

double t1 = calcTax(16, 20000);


[Link](t1);

double t2 = calcTax(20, 18000);


[Link](t2);
}

public static double calcTax(int age, double salary) {

if (age < 18) {


return 0.0;
}

if (salary < 10000) {


return 0.0;
}

if (salary >= 10000 && salary <= 20000) {


return salary * 0.07;
}

if (salary > 20000) {


return salary * 0.14;
}

return 0.0;
}
}

B
import [Link];

public class Main {


public static void main(String[] args) {

calcYearlyTax();
}

public static void calcYearlyTax() {


Scanner scanner = new Scanner([Link]);

[Link]("Enter your age: ");


int age = [Link]();

double totalTax = 0;

for (int month = 1; month <= 12; month++) {

[Link]("Enter income for Month " + month + ": ");


double salary = [Link]();

double monthlyTax = calcTax(age, salary);

[Link]("Month" + month + " tax: " + monthlyTax);

totalTax += monthlyTax;
}

/
[Link]("Total Yearly Tax: " + totalTax);

[Link]();
}

public static double calcTax(int age, double salary) {

if (age < 18) {


return 0.0; // No tax if age is less than 18
}

if (salary < 10000) {


return 0.0; // No tax if salary is less than 10,000
}

if (salary >= 10000 && salary <= 20000) {


return salary * 0.07;
}

if (salary > 20000) {


return salary * 0.14;
}

return 0.0;
}
}

#TASK7
import [Link];

public class Main {


public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);


[Link]("Enter the value of N: ");
int N = [Link]();

oneToN(1, N);

[Link]();
}

public static void oneToN(int current, int N) {

if (current > N) {
return;
}

[Link](current + " ");

oneToN(current + 1, N);
}
}

B
import [Link];

public class Main {


public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);


[Link]("Enter the value of N: ");
int N = [Link]();

nToOne(N);

[Link]();
}

public static void nToOne(int N) {

if (N < 1) {
return;
}

[Link](N + " ");


nToOne(N - 1);
}
}

C
import [Link];

public class Main {


public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);


[Link]("Enter the value of N: ");
int N = [Link]();

int sum = recursiveSum(1, N);


[Link](sum);

[Link](); /
}

public static int recursiveSum(int current, int N) {

if (current > N) {
return 0;
}

return current + recursiveSum(current + 1, N);


}
}

#TASK8
import [Link];

public class Main {


public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);


[Link]("Enter an integer: ");
int n = [Link]();

reverseDigits(n);

[Link]();
}

public static void reverseDigits(int n) {

if (n == 0) {
return;
}

[Link](n % 10);
reverseDigits(n / 10);
}
}

#TASK9
import [Link];

public class Main {


public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);


[Link]("Enter an integer: ");
int n = [Link]();

int sum = sumDigits(n);


[Link](sum);

[Link]();
}

public static int sumDigits(int n) {

if (n == 0) {
return 0;
}

int lastDigit = n % 10;

return lastDigit + sumDigits(n / 10);


}
}

You might also like