0% found this document useful (0 votes)
10 views4 pages

Java Classes for Complex Numbers and Points

Uploaded by

Ikrame Mimi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Java Classes for Complex Numbers and Points

Uploaded by

Ikrame Mimi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Correction Lab 4

Exercise 1
public class Complex {
private double real;
private double imaginary;

public Complex(double real, double imaginary) {


[Link] = real;
[Link] = imaginary;
}

public Complex add(Complex other) {


double newReal = [Link] + [Link];
double newImaginary = [Link] + [Link];
return Complex(newReal, newImaginary);
}

public Complex conjugate() {


return Complex([Link], -[Link]);
}

public void display() {


if ([Link] >= 0) {
[Link]([Link] + " + " + [Link] + "i");
} else {
[Link]([Link] + " - " + (-[Link]) + "i");
}
}
public static void main(String[] args) {

Complex c1 = new Complex(3, 4);


Complex c2 = new Complex(1, -2);

Complex sum = [Link](c2);


[Link]("Sum: ");
[Link]();

Complex conjugate = [Link]();


[Link]("Conjugate of c1: ");
[Link]();
}
}
Exercise 2
import [Link];
public class Point {
private String name;
private double x;
private double y;
public Point(String name, double x, double y) {
[Link] = name;
this.x = x;
this.y = y;
}

public double distanceTo(Point other) {


double deltaX = this.x - other.x;
double deltaY = this.y - other.y;
return [Link](deltaX * deltaX + deltaY * deltaY);
}
public static void main(String[] args) {

Point pointA = new Point("A", 0, 0);


Point pointB = new Point("B", 3, 4);
Point pointC = new Point("C", -1, 2);

double distanceAB = [Link](pointB);


double distanceAC = [Link](pointC);
double distanceBC = [Link](pointC);

[Link]("Distance between A and B: " + distanceAB);


[Link]("Distance between A and C: " + distanceAC);
[Link]("Distance between B and C: " + distanceBC);
}
}
Exercise 6
public class SortedArray {
private static int[] array;
private int size;
private int capacity;

public SortedArray() {
[Link] = 10; // default capacity
array = new int[capacity];
size = 0;
}

public SortedArray(int capacity) {


[Link] = capacity;
array = new int[capacity];
size = 0;
}

public boolean insert(int integer) {


if (size == capacity) {
return false; // Array is full
}
int i;
for (i = size - 1; i >= 0 && array[i] > integer; i--) {
array[i + 1] = array[i];
}
array[i + 1] = integer;
size++;
return true;
}

public boolean remove(int integer) {


int index = searchIndex(integer);
if (index == -1) {
return false; // Integer not found
}
for (int i = index; i < size - 1; i++) {
array[i] = array[i + 1];
}
size--;
return true;
}

public boolean search(int integer) {


return searchIndex(integer) != -1;
}

private int searchIndex(int integer) {


for (int i = 0; i < size; i++) {
if (array[i] == integer) {
return i;
}
}
return -1;
}

public boolean equals(SortedArray T) {


if ([Link] != [Link]) {
return false;
}
for (int i = 0; i < size; i++) {
if ([Link][i] != [Link][i]) {
return false;
}
}
return true;
}

public static void main(String[] args) {

SortedArray arr = new SortedArray(5);


[Link](3);
[Link](1);
[Link](4);
[Link](2);
[Link](5);
[Link]("Array contents:");
for (int i = 0; i < [Link]; i++) {
[Link]([Link][i] + " ");
}
[Link]();
[Link]("Searching for 3: " + [Link](3));
[Link]("Removing 3: " + [Link](3));
[Link]("Searching for 3 after removal: " +
[Link](3));
[Link]("Array contents after removal:");
for (int i = 0; i < [Link]; i++) {
[Link]([Link][i] + " ");
}
[Link]();
SortedArray arr2 = new SortedArray(5);
[Link](1);
[Link](2);
[Link](4);
[Link](5);
[Link](3);
[Link]("Arrays are equal: " + [Link](arr2));
}
}

You might also like