From f7d1aa3358be0ddc0023fa651a60acff85cc5a7c Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Mon, 2 Sep 2024 00:08:48 +0530 Subject: [PATCH] Upload files to "OOP/Java/Lab/Week5" --- OOP/Java/Lab/Week5/ComplexAdder.java | 61 ++++++++++++++++++++++ OOP/Java/Lab/Week5/Employee.java | 2 +- OOP/Java/Lab/Week5/Number.java | 77 ++++++++++++++++++++++++++++ OOP/Java/Lab/Week5/Time.java | 31 +++++++---- 4 files changed, 160 insertions(+), 11 deletions(-) create mode 100644 OOP/Java/Lab/Week5/ComplexAdder.java create mode 100644 OOP/Java/Lab/Week5/Number.java diff --git a/OOP/Java/Lab/Week5/ComplexAdder.java b/OOP/Java/Lab/Week5/ComplexAdder.java new file mode 100644 index 0000000..d85c915 --- /dev/null +++ b/OOP/Java/Lab/Week5/ComplexAdder.java @@ -0,0 +1,61 @@ +import java.util.Scanner; + +class Complex { + + double real; + double imaginary; + + public Complex(double real, double imaginary) { + this.real = real; + this.imaginary = imaginary; + } + + public Complex add(int realPart) { + return new Complex(this.real + realPart, this.imaginary); + } + + public Complex add(Complex other) { + return new Complex( + this.real + other.real, + this.imaginary + other.imaginary + ); + } + + public String toString() { + return this.real + " + " + this.imaginary + "i"; + } +} + +public class ComplexAdder { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int a, b, c, d, e; + + System.out.print( + "Enter the values of a and b, for the first Complex Number of the form a + ib: " + ); + a = sc.nextInt(); + b = sc.nextInt(); + + System.out.print( + "Enter the values of c and d, for the second Complex Number of the form c + id: " + ); + c = sc.nextInt(); + d = sc.nextInt(); + + System.out.print( + "Enter the integer you want to add to the first complex number:" + ); + e = sc.nextInt(); + + Complex c1 = new Complex(a, b); + Complex c2 = new Complex(c, d); + + Complex sum1 = c1.add(e); + Complex sum2 = c1.add(c2); + + System.out.println("Sum of complex number and integer: " + sum1); + System.out.println("Sum of two complex numbers: " + sum2); + } +} diff --git a/OOP/Java/Lab/Week5/Employee.java b/OOP/Java/Lab/Week5/Employee.java index 5ad63ae..c72cf81 100644 --- a/OOP/Java/Lab/Week5/Employee.java +++ b/OOP/Java/Lab/Week5/Employee.java @@ -43,4 +43,4 @@ public class Employee { emp.calculateCTC(); emp.display(); } -} \ No newline at end of file +} diff --git a/OOP/Java/Lab/Week5/Number.java b/OOP/Java/Lab/Week5/Number.java new file mode 100644 index 0000000..40c2044 --- /dev/null +++ b/OOP/Java/Lab/Week5/Number.java @@ -0,0 +1,77 @@ +import java.util.Scanner; + +class Number { + + private double value; + + public Number(double value) { + this.value = value; + } + + public boolean isZero() { + return value == 0; + } + + public boolean isPositive() { + return value > 0; + } + + public boolean isNegative() { + return value < 0; + } + + public boolean isOdd() { + return (int) value % 2 != 0; + } + + public boolean isEven() { + return (int) value % 2 == 0; + } + + public boolean isPrime() { + if (value <= 1 || value != (int) value) { + return false; + } + for (int i = 2; i <= Math.sqrt(value); i++) { + if ((int) value % i == 0) { + return false; + } + } + return true; + } + + public boolean isArmstrong() { + if (value != (int) value) { + return false; + } + int num = (int) value; + int originalNum = num; + int sum = 0; + int digits = String.valueOf(num).length(); + + while (num > 0) { + int digit = num % 10; + sum += Math.pow(digit, digits); + num /= 10; + } + + return sum == originalNum; + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter a number: "); + double input = sc.nextDouble(); + Number number = new Number(input); + + System.out.println("Is Zero: " + number.isZero()); + System.out.println("Is Positive: " + number.isPositive()); + System.out.println("Is Negative: " + number.isNegative()); + System.out.println("Is Odd: " + number.isOdd()); + System.out.println("Is Even: " + number.isEven()); + System.out.println("Is Prime: " + number.isPrime()); + System.out.println("Is Armstrong: " + number.isArmstrong()); + + sc.close(); + } +} diff --git a/OOP/Java/Lab/Week5/Time.java b/OOP/Java/Lab/Week5/Time.java index 5f6a7bf..8888191 100644 --- a/OOP/Java/Lab/Week5/Time.java +++ b/OOP/Java/Lab/Week5/Time.java @@ -1,33 +1,46 @@ public class Time { + int hr; int min; int sec; - Time(){ + Time() { // initializing to zero hr = 0; min = 0; min = 0; } - Time (int hour, int minute, int second){ + Time(int hour, int minute, int second) { hr = hour; min = minute; sec = second; } - public void display(){ - System.out.printf("%02d:%02d:%02d\n", (hr%24), (min%60), (sec%60)); } + public void display() { + System.out.printf( + "%02d:%02d:%02d\n", + (hr % 24), + (min % 60), + (sec % 60) + ); + } - public void add(Time t1, Time t2){ - int totalSeconds = (t1.hr * 3600) + (t2.hr * 3600) + (t1.min * 60) + (t2.min * 60) + (t1.sec) + (t2.sec); + public void add(Time t1, Time t2) { + int totalSeconds = + (t1.hr * 3600) + + (t2.hr * 3600) + + (t1.min * 60) + + (t2.min * 60) + + (t1.sec) + + (t2.sec); this.hr = totalSeconds / 3600; - this.min = (totalSeconds%3600) / 60; + this.min = (totalSeconds % 3600) / 60; this.sec = (totalSeconds % 60); } public static void main(String[] args) { - Time t1 = new Time(13, 30, 0); + Time t1 = new Time(13, 30, 0); Time t2 = new Time(05, 45, 00); System.out.print("Time T1 is: "); @@ -40,7 +53,5 @@ public class Time { sum.add(t1, t2); System.out.print("The sum is: "); sum.display(); - } - }