-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClass_Method_Demo.java
More file actions
77 lines (64 loc) · 2.56 KB
/
Class_Method_Demo.java
File metadata and controls
77 lines (64 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Program: Classes and Methods Demo
* Description: A Java program to demonstrate the creation of a class, instantiation of objects,
* and definition of methods for basic arithmetic and geometric calculations.
* Author: Amey Thakur
* Reference: https://github.com/Amey-Thakur/OOPM-JAVA-LAB
*/
import java.util.Scanner;
public class Class_Method_Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Creation of an object of the class Class_Method_Demo
Class_Method_Demo demoObject = new Class_Method_Demo();
System.out.println("---------------------------------------------");
System.out.println(" Class and Methods Demo");
System.out.println("---------------------------------------------");
// Input for arithmetic operations
System.out.print("Enter first number for arithmetic ops: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number for arithmetic ops: ");
int num2 = scanner.nextInt();
// Input for circle calculation
System.out.print("Enter the radius for circle: ");
int r = scanner.nextInt();
// Calling instance methods using the object
int sum = demoObject.add(num1, num2);
int difference = demoObject.subtract(num1, num2);
float circumference = demoObject.calculateCircumference(r);
System.out.println("\n---------------------------------------------");
System.out.println(" Results");
System.out.println("---------------------------------------------");
System.out.println("Addition Result: " + sum);
System.out.println("Subtraction Result: " + difference);
System.out.println("Circumference of Circle: " + circumference);
System.out.println("---------------------------------------------");
scanner.close();
}
/**
* Method to add two integers.
* @param a First integer
* @param b Second integer
* @return Sum of a and b
*/
int add(int a, int b) {
return a + b;
}
/**
* Method to subtract two integers.
* @param c First integer
* @param d Second integer
* @return Difference of c and d
*/
int subtract(int c, int d) {
return c - d;
}
/**
* Method to calculate circumference of a circle.
* @param radius Radius of the circle
* @return Circumference (2 * pi * r)
*/
float calculateCircumference(int radius) {
return 2 * 3.14f * radius;
}
}