Upload files to "OOP/Java/Lab/Week5"

This commit is contained in:
Aadit Agrawal 2024-09-02 00:08:48 +05:30
parent ba0ee79bc1
commit f7d1aa3358
4 changed files with 160 additions and 11 deletions

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -1,4 +1,5 @@
public class Time {
int hr;
int min;
int sec;
@ -17,10 +18,22 @@ public class Time {
}
public void display() {
System.out.printf("%02d:%02d:%02d\n", (hr%24), (min%60), (sec%60)); }
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);
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.sec = (totalSeconds % 60);
@ -40,7 +53,5 @@ public class Time {
sum.add(t1, t2);
System.out.print("The sum is: ");
sum.display();
}
}