NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

main.java
public class Main {
public static void main(String[] args) {
Vector vector = new Vector(3);
vector.setElement(0, 1.5);
vector.setElement(1, 2.7);
vector.setElement(2, 3.9);

System.out.println("Vector length: " + vector.getLength());
System.out.println("Element at index 1: " + vector.getElement(1));

System.out.println("Min element: " + vector.findMin());
System.out.println("Max element: " + vector.findMax());

vector.sort();
System.out.println("Sorted vector: ");
for (int i = 0; i < vector.getLength(); i++) {
System.out.println(vector.getElement(i));
}

System.out.println("Euclidean norm: " + vector.calculateEuclideanNorm());

vector.multiplyByScalar(2);
System.out.println("Vector after multiplication: ");
for (int i = 0; i < vector.getLength(); i++) {
System.out.println(vector.getElement(i));
}

Vector vector2 = new Vector(3);
vector2.setElement(0, 0.5);
vector2.setElement(1, 1.0);
vector2.setElement(2, 1.5);
Vector sum = Vector.addVectors(vector, vector2);
System.out.println("Sum of vectors: ");
for (int i = 0; i < sum.getLength(); i++) {
System.out.println(sum.getElement(i));
}

double scalarProduct = Vector.calculateScalarProduct(vector, vector2);
System.out.println("Scalar product: " + scalarProduct);
}
}

Vector.java
public class Vector {
private double[] elements;

public Vector(int length) {
elements = new double[length];
}

public double getElement(int index) {
return elements[index];
}

public void setElement(int index, double value) {
elements[index] = value;
}

public int getLength() {
return elements.length;
}

public double findMin() {
double min = elements[0];
for (int i = 1; i < elements.length; i++) {
if (elements[i] < min) {
min = elements[i];
}
}
return min;
}

public double findMax() {
double max = elements[0];
for (int i = 1; i < elements.length; i++) {
if (elements[i] > max) {
max = elements[i];
}
}
return max;
}

public void sort() {
for (int i = 0; i < elements.length - 1; i++) {
for (int j = i + 1; j < elements.length; j++) {
if (elements[i] > elements[j]) {
double temp = elements[i];
elements[i] = elements[j];
elements[j] = temp;
}
}
}
}

public double calculateEuclideanNorm() {
double sum = 0;
for (double element : elements) {
sum += Math.pow(element, 2);
}
return Math.sqrt(sum);
}

public void multiplyByScalar(double scalar) {
for (int i = 0; i < elements.length; i++) {
elements[i] *= scalar;
}
}

public static Vector addVectors(Vector vector1, Vector vector2) {
if (vector1.getLength() != vector2.getLength()) {
throw new IllegalArgumentException("Vectors should have the same length");
}
Vector result = new Vector(vector1.getLength());
for (int i = 0; i < vector1.getLength(); i++) {
result.setElement(i, vector1.getElement(i) + vector2.getElement(i));
}
return result;
}

public static double calculateScalarProduct(Vector vector1, Vector vector2) {
if (vector1.getLength() != vector2.getLength()) {
throw new IllegalArgumentException("Vectors should have the same length");
}
double result = 0;
for (int i = 0; i < vector1.getLength(); i++) {
result += vector1.getElement(i) * vector2.getElement(i);
}
return result;
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.