MyLibrary.java
MyLibrary.java
import
java
.
util
.
*
;
public
class
MyLibrary
{
private
Scanner
ob
;
private
static
Scanner
ob2
;
public
static
double
CalcFee
(
int
n
)
throws
creditException
{
int
bill
=
0
,
lab
=
50
,
tech
=
20
;
if
(
n
<
1
||
n
>
33
)
{
throw
new
creditException
(
"Enter the number in the range of 1 to 33 only"
);
}
else
{
if
(
n
<
12
)
{
bill
=
(
n
*
110
)
+
lab
;
return
bill
;
}
else
{
bill
=
(
12
*
110
)
+
lab
+
tech
;
return
bill
;
}
}
}
public
static
long
fac
(
int
f
)
throws
facException
{
int
i
=
1
;
if
(
f
>=
0
&&
f
<=
20
)
{
for
(
int
j
=
1
;
j
<=
f
;
j
++
)
{
i
=
i
*
j
;
}
return
i
;
}
else
{
throw
new
facException
(
"Enter the number in the range of 0 to 20 only"
);
}
}
public
static
int
locate
(
double
a
[],
double
i
)
throws
ArrayException
{
int
f
=
0
;
if
(
a
.
length
==
0
)
{
throw
new
ArrayException
(
"The array is empty"
);
}
else
{
for
(
int
j
=
0
;
j
<
a
.
length
;
j
++
)
{
if
(
i
==
a
[
j
])
{
f
=
j
;
break
;
}
}
}
if
(
f
!=
0
)
{
return
f
;
}
else
return
-
1
;
}
public
int
getInteger
()
{
boolean
val
=
false
;
int
i
=
0
;
ob
=
new
Scanner
(
System
.
in
);
while
(
val
!=
true
)
{
System
.
out
.
println
(
"Enter an integer value:"
);
String
n
=
ob
.
next
();
try
{
i
=
Integer
.
parseInt
(
n
);
val
=
true
;
}
catch
(
NumberFormatException
e
)
{
System
.
out
.
println
(
"Please enter a valid Integer"
);
continue
;
}
}
return
i
;
}
public
String
substitute
(
String
s
,
char
r
,
char
p
)
throws
InvalidStringException
{
String
subs
;
if
((
s
.
equals
(
""
))
||
(
s
.
equals
(
null
)))
{
throw
new
InvalidStringException
(
"The string is empty. Please enter a non empty string"
);
}
else
{
subs
=
s
.
replace
(
r
,
p
);
}
return
subs
;
}
public
double
sine
(
double
ang
,
double
n
)
throws
angleException
,
facException
{
// angle to radians
double
rad
=
ang
*
1.
/
180.
*
Math
.
PI
;
// the first element of the taylor series
double
sum
=
rad
;
// add them up until a certain precision (eg. 10)
if
(
ang
<
0
||
ang
>
360
)
{
throw
new
angleException
(
"Enter the angle value between 0 and 360"
);
}
if
(
n
<
1
||
n
>
10
)
{
throw
new
facException
(
"Enter the numbers of terms to be only 1 to 10"
);
}
else
{
System
.
out
.
println
(
"The sine value using the Math.sin() is: "
+
Math
.
sin
(
rad
));
for
(
int
i
=
1
;
i
<=
n
;
i
++
)
{
if
(
i
%
2
==
0
)
try
{
sum
+=
Math
.
pow
(
rad
,
2
*
i
+
1
)
/
fac
(
2
*
i
+
1
);
}
catch
(
facException e
)
{
// TODO Auto-generated catch block
System
.
out
.
println
(
e
.
getMessage
());
}
else
try
{
sum
-=
Math
.
pow
(
rad
,
2
*
i
+
1
)
/
fac
(
2
*
i
+
1
);
}
catch
(
facException e
)
{
// TODO Auto-generated catch block
System
.
out
.
println
(
e
.
getMessage
());
}
}
}
return
sum
;
}
public
static
void
main
(
String
args
[])
{
System
.
out
.
println
(
"1. Calculate Fees"
);
System
.
out
.
println
(
"2. Calculate Factorial"
);
System
.
out
.
println
(
"3. Locate Value"
);
System
.
out
.
println
(
"4. Get Integer Value"
);
System
.
out
.
println
(
"5. Substitute Characters"
);
System
.
out
.
println
(
"6. Calulate Sine of an Angle"
);
System
.
out
.
println
(
"Enter your Choice:"
);
ob2
=
new
Scanner
(
System
.
in
);
int
ch
=
ob2
.
nextInt
();
switch
(
ch
)
{
case
1
:
System
.
out
.
println
(
"Enter the number of credits:"
);
int
n
=
ob2
.
nextInt
();
try
{
System
.
out
.
println
(
"The amount which the student owes to the college: "
+
(
CalcFee
(
n
)));
}
catch
(
creditException e
)
{
// TODO Auto-generated catch block
System
.
out
.
println
(
e
.
getMessage
());
}
break
;
case
2
:
System
.
out
.
println
(
"Enter the number to compute the factorial:"
);
int
a
=
ob2
.
nextInt
();
try
{
System
.
out
.
println
(
"The factorial of "
+
a
+
" is: "
+
(
fac
(
a
)));
}
catch
(
facException e
)
{
// TODO Auto-generated catch block
System
.
out
.
println
(
e
.
getMessage
());
}
break
;
case
3
:
System
.
out
.
println
(
"Enter the number of array elements"
);
int
b
=
ob2
.
nextInt
();
int
c
,
search
;
double
[]
arr
=
new
double
[
b
];
for
(
int
i
=
0
;
i
<
b
;
i
++
)
{
System
.
out
.
println
(
"Enter element "
+
(
i
+
1
)
+
" :"
);
c
=
ob2
.
nextInt
();
arr
[
i
]
=
c
;
}
System
.
out
.
println
(
"Enter the Search value"
);
search
=
ob2
.
nextInt
();
try
{
System
.
out
.
println
(
"The index of "
+
search
+
" is: "
+
(
locate
(
arr
,
search
)));
}
catch
(
ArrayException
e
)
{
// TODO Auto-generated catch block
System
.
out
.
println
(
e
.
getMessage
());
}
break
;
case
4
:
MyLibrary
my
=
new
MyLibrary
();
System
.
out
.
println
(
"The valid integer value entered is: "
+
(
my
.
getInteger
()));
break
;
case
5
:
MyLibrary
my1
=
new
MyLibrary
();
System
.
out
.
println
(
"Enter the String: "
);
String
str
=
ob2
.
next
();
System
.
out
.
println
(
"Enter the character to be replaced: "
);
char
rep
=
ob2
.
next
().
charAt
(
0
);
System
.
out
.
println
(
"Enter the substitution character: "
);
char
sub
=
ob2
.
next
().
charAt
(
0
);
try
{
System
.
out
.
println
(
"The new string is: "
+
(
my1
.
substitute
(
str
,
rep
,
sub
)));
}
catch
(
InvalidStringException
e
)
{
// TODO Auto-generated catch block
System
.
out
.
println
(
e
.
getMessage
());
}
break
;
case
6
:
MyLibrary
my2
=
new
MyLibrary
();
System
.
out
.
println
(
"Enter the angle: "
);
double
k
=
ob2
.
nextDouble
();
System
.
out
.
println
(
"Enter the number of terms"
);
double
q
=
ob2
.
nextDouble
();
try
{
System
.
out
.
println
(
"Whereas the Value of sin "
+
k
+
" as calculated from the taylor series is: "
+
my2
.
sine
(
k
,
q
));
}
catch
(
facException e
)
{
// TODO Auto-generated catch block
System
.
out
.
println
(
e
.
getMessage
());
}
catch
(
angleException e
)
{
// TODO Auto-generated catch block
System
.
out
.
println
(
e
.
getMessage
());
}
break
;
default
:
System
.
out
.
println
(
"Incorrect Choice"
);
}
}
}
creditException.java
creditException.java
public
class
creditException
extends
Exception
{
/**
*
*/
private
static
final
long
serialVersionUID
=
1L
;
creditException
(
String
mes
)
{
super
(
mes
);
}
}
InvalidStringException.java
InvalidStringException.java
public
class
InvalidStringException
extends
Exception
{
/**
*
*/
private
static
final
long
serialVersionUID
=
1L
;
InvalidStringException
(
String
mess
)
{
super
(
mess
);
}
}
facException.java
facException.java
public
class
facException
extends
Exception
{
/**
*
*/
private
static
final
long
serialVersionUID
=
1L
;
facException
(
String
mess
)
{
super
(
mess
);
}
}
ArrayException.java
ArrayException.java
public
class
ArrayException
extends
Exception
{
/**
*
*/
private
static
final
long
serialVersionUID
=
1L
;
ArrayException
(
String
mess
)
{
super
(
mess
);
}
}
angleException.java
angleException.java
public
class
angleException
extends
Exception
{
private
static
final
long
serialVersionUID
=
1L
;
angleException
(
String
mess
)
{
super
(
mess
);
}
}