You are on page 1of 12

ASSIGNMENT - 03

1
ASSIGNMENT FILE

2
3
QUESTION – 1:

Code:

class cmplx(var real: Double, var imag: Double) {


constructor() : this(0.0, 0.0)
// Calculate modulus
fun modulus(): Double {
return Math.sqrt((real * real) + (imag * imag))
}
// Calculate argument
fun argument(): Double {
return Math.atan2(imag, real)
}
// Add two complex numbers and store the result
fun add(com2: cmplx): cmplx {
val tempreal = this.real + com2.real
val tempimag = this.imag + com2.imag
return cmplx(tempreal, tempimag)
}
// Multiply two complex numbers and store the result
fun multiply(com2: cmplx): cmplx {
val tempreal = (this.real * com2.real) - (this.imag * com2.imag)
val tempimag = (this.real * com2.imag) + (this.imag * com2.real)
return cmplx(tempreal, tempimag)
}
// Display the complex number in x +iy format
fun display(): String {
return real.toString() + "+" + imag.toString() + "i"
}
}

fun main() {
// Creating objects for complex numbers
val c1 = cmplx(3.0, 4.0)
val c2 = cmplx(2.0, -1.0)
// Calculating modulus
println("Modulus of c1 = ${c1.modulus()}")
// Calculating argument
println("Argument of c1 = ${c1.argument()}")
// Adding c1 and c2
val c3 = c1.add(c2)
println("Addition of c1 and c2 = ${c3.display()}")

// Multiplying c1 and c2
val c4 = c1.multiply(c2)
println("Multiplication of c1 and c2 = ${c4.display()}")
}

4
Result:

5
QUESTION – 2:

class rational(var numerator: Int, var denominator: Int) {


init {
if (denominator == 0) {
throw Exception("Denominator cannot be 0")
}
}

companion object {
fun lcm(a: rational, b: rational): Int {
val lcm = (a.denominator * b.denominator) / gcd(a.denominator,
b.denominator)
println("The Value of LCM : $lcm")
return (lcm)
}

private fun gcd(a: Int, b: Int): Int {


return if (a == 0) b else gcd(b % a, a)
}
}

fun add(b: rational): rational {


val lcm = lcm(this, b)
return rational(
(this.numerator * (lcm / this.denominator)) + (b.numerator *
(lcm / b.denominator)),
lcm
)
}

fun subtract(b: rational): rational {


val lcm = lcm(this, b)
return rational(
(this.numerator * (lcm / this.denominator)) - (b.numerator *
(lcm / b.denominator)),
lcm
)
}

override fun toString(): String {


return "$numerator/$denominator"
}
}

//Example
fun main(args: Array<String>) {
val a = rational(1, 2)
val b = rational(4, 8)
val c = a.add(b)
println("$a + $b = $c")
println( )
val d = a.subtract(b)
println("$a - $b = $d")
}

6
Result:

7
QUESTION – 3:

class myTime(var hour: Int, var minute: Int, var second: Int) {

init{
if(hour<24 && minute <60 && second<60)
{
println("Valid time")
}
else
{
println("Invalid time")
}
}

//overloaded method to add time in minutes


fun addTimeByMinutes(minutes: Int): myTime{
val newHour = (minute + minutes)/60
val newMinute = (minute + minutes)%60
val newSecond = second

return myTime((hour+newHour)%24, newMinute, newSecond)


}

//overloaded method to add time in hours


fun addTimeByHours(hours: Int): myTime{
val newHour = (hour + hours)%24
val newMinute = minute
val newSecond = second

return myTime(newHour, newMinute, newSecond)


}

//displayTime() to show the time in HH:MM:SS format


fun displayTime(){
var formatTime = ""
if(hour>=24){
formatTime = "[day 2] "
}
formatTime += "$hour:$minute:$second"
println("Time: $formatTime")
}
}

//Example

fun main(){
var time = myTime(23, 55, 30)
time.displayTime()
time = time.addTimeByMinutes(50)
time.displayTime()
time = time.addTimeByHours(2)
time.displayTime()
}

8
Result:

9
QUESTION – 4:

class lineSegment {
var x1: Double
var y1: Double
var x2: Double
var y2: Double

constructor(x1: Double, y1: Double, x2: Double, y2: Double) {


this.x1 = x1
this.y1 = y1
this.x2 = x2
this.y2 = y2
}

// Function to calculate the intersection point


fun intersect(line2: lineSegment): Array<Double> {
// Line AB represented as a1x + b1y = c1
val a1 = y2 - y1
val b1 = x1 - x2
val c1 = a1 * (x1) + b1 * (y1)

// Line CD represented as a2x + b2y = c2


val a2 = line2.y2 - line2.y1
val b2 = line2.x1 - line2.x2
val c2 = a2 * (line2.x1) + b2 * (line2.y1)

val determinant = a1 * b2 - a2 * b1

if (determinant == 0.0) {
// The lines are parallel.
println("The lines are parallel - no intersection.")
return arrayOf(0.0, 0.0)
}
else {
val x = (b2 * c1 - b1 * c2) / determinant
val y = (a1 * c2 - a2 * c1) / determinant
println("The intersection point is at: ($x, $y)")
return arrayOf(x, y)
}
}
}

// Main function
fun main(args: Array<String>) {
val line1 = lineSegment(2.0, 5.0, 8.0, 5.0)
val line2 = lineSegment(2.0, 10.0, 8.0, 2.0)
line1.intersect(line2) // calls the intersect method of lineSegment
}

Result:

10
QUESTION – 5:

open class Circle2D {


var x: Double
var y: Double
var radius: Double

constructor() {
this.x = 0.0
this.y = 0.0
this.radius = 1.0
}

constructor(x: Double, y: Double, radius: Double) {


this.x = x
this.y = y
this.radius = radius
}

open fun getArea(): Double {


return Math.PI * radius * radius
}

fun getPerimeter(): Double {


return 2 * Math.PI * radius
}

fun contains(x: Double, y: Double): Boolean {


return Math.pow(x - this.x, 2.0) + Math.pow(y - this.y, 2.0) <=
Math.pow(radius, 2.0)
}

fun contains(circle2D: Circle2D): Boolean {


return Math.pow(circle2D.x - this.x, 2.0) + Math.pow(circle2D.y -
this.y, 2.0) + Math.pow(circle2D.radius - this.radius, 2.0) <=
Math.pow(radius, 2.0)
}

open fun display() {


println("Radius: $radius")
println("Center: ($x, $y)")
println("Area: ${getArea()}")
println("Perimeter: ${getPerimeter()}")
}
}

class Sphere: Circle2D {


constructor(): super()
constructor(x: Double, y: Double, radius: Double): super(x, y, radius)

override fun getArea(): Double {


return 4 * Math.PI * Math.pow(radius, 2.0)
}

fun getVolume(): Double {


return 4/3 * Math.PI * Math.pow(radius, 3.0)
}

override fun display() {

11
println("Radius: $radius")
println("Center: ($x, $y)")
println("Surface Area: ${getArea()}")
println("Volume: ${getVolume()}")
}
}

fun main() {
val circle = Circle2D(2.0, 3.0, 1.0)
println("Circle:")
circle.display()
println()
println("Contains point (3, 3): ${circle.contains(3.0, 3.0)}")

val circle2 = Circle2D(4.0, 5.0, 3.0)


println("Contains circle: ${circle.contains(circle2)}")

val sphere = Sphere(2.0, 3.0, 1.0)


println("Sphere:")
sphere.display()
}

Result:

12

You might also like