-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackage
119 lines (93 loc) · 2.57 KB
/
Package
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package pack1;
public class MathOperation {
public int add(int a,int b){
return a+b;
}
public int subtact(int a,int b){
return a-b;
}
}
package pack2;
public class StringOperation {
String CatStr,RevStr="";
public String concatenate(String a,String b){
CatStr = a+" "+b;
return CatStr;
}
public String reverse(String str){
char ch[] = str.toCharArray();
int strlen = str.length();
for(int i=strlen-1;i>=0;i--){
RevStr=RevStr+ch[i];
}
return RevStr;
}
}
package pack3;
class UDException extends Exception{
public UDException(String message){
super(message);
}
}
public class calculator {
double div;
public int multiply(int a,int b){
return a*b;
}
public double divide(double a,double b){
try{
if(b==0){
throw new Exception("Do not enter 0 as denominator.");
}
else{
div=a/b;
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
return div;
}
}
// Distance converter
// Create a package named "converter"
package converter;
// Create a class named "DistanceConverter" in the "converter" package
public class DistanceConverter {
// Method to convert meters to kilometers
public static double metersToKilometers(double meters) {
return meters / 1000.0;
}
// Method to convert miles to kilometers
public static double milesToKilometers(double miles) {
return miles * 1.60934;
}
// Method to convert kilometers to miles
public static double kilometersToMiles(double kilometers) {
return kilometers / 1.60934;
}
// Method to convert kilometers to meters
public static double kilometersToMeters(double kilometers) {
return kilometers * 1000.0;
}
}
// Create a package named "main"
package main;
// Import the "converter" package to use the DistanceConverter class
import converter.DistanceConverter;
public class Main {
public static void main(String[] args) {
// Example conversions
double meters = 1000.0;
double miles = 10.0;
// Convert meters to kilometers
double metersToKm = DistanceConverter.metersToKilometers(meters);
System.out.println(meters + " meters is equal to " + metersToKm + " kilometers");
// Convert miles to kilometers
double milesToKm = DistanceConverter.milesToKilometers(miles);
System.out.println(miles + " miles is equal to " + milesToKm + " kilometers");
// Convert kilometers to miles
double kmToMiles = DistanceConverter.kilometersToMiles(metersToKm);
System.out.println(metersToKm + " kilometers is equal to " + kmToMiles + " miles");
}
}