Compare commits
3 Commits
2f0de3f778
...
9506ee39ed
Author | SHA1 | Date | |
---|---|---|---|
9506ee39ed | |||
ee2b5d5e1c | |||
203e18ca10 |
@ -6,5 +6,6 @@ Lösungen zu den Aufgaben der Vorlesung "Programmieren" des ersten und zweiten S
|
||||
- **Editor**: [Visual Studio Code](https://code.visualstudio.com/)
|
||||
- [Extension Pack for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack)
|
||||
- [EditorConfig for VS Code](https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig)
|
||||
- [Rainbow Brackets](https://marketplace.visualstudio.com/items?itemName=tal7aouy.rainbow-bracket)
|
||||
- **Java**: [Java Development Kit 21 (LTS)](https://www.oracle.com/de/java/technologies/downloads/#java21)
|
||||
- **Style Guide**: [Google Java Style Guide](https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml)
|
38
VL08/Aufgabe03/Aufgabe03.java
Normal file
38
VL08/Aufgabe03/Aufgabe03.java
Normal file
@ -0,0 +1,38 @@
|
||||
package VL08.Aufgabe03;
|
||||
|
||||
/**
|
||||
* Vorlesung 8 / Aufgabe 3
|
||||
*
|
||||
* @author Sebastian Brosch
|
||||
*/
|
||||
public class Aufgabe03 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Matrix matrixA = new Matrix(2, 3);
|
||||
matrixA.setValue(0, 0, 3);
|
||||
matrixA.setValue(0, 1, 2);
|
||||
matrixA.setValue(0, 2, 1);
|
||||
matrixA.setValue(1, 0, 1);
|
||||
matrixA.setValue(1, 1, 0);
|
||||
matrixA.setValue(1, 2, 2);
|
||||
|
||||
Matrix matrixB = new Matrix(3, 2);
|
||||
matrixB.setValue(0, 0, 1);
|
||||
matrixB.setValue(0, 1, 2);
|
||||
matrixB.setValue(1, 0, 0);
|
||||
matrixB.setValue(1, 1, 1);
|
||||
matrixB.setValue(2, 0, 4);
|
||||
matrixB.setValue(2, 1, 0);
|
||||
|
||||
System.out.println("------------------");
|
||||
matrixA.print();
|
||||
System.out.println("------------------");
|
||||
matrixB.print();
|
||||
System.out.println("------------------");
|
||||
|
||||
matrixA.multiply(matrixB);
|
||||
|
||||
matrixA.print();
|
||||
System.out.println("------------------");
|
||||
}
|
||||
}
|
154
VL08/Aufgabe03/Matrix.java
Normal file
154
VL08/Aufgabe03/Matrix.java
Normal file
@ -0,0 +1,154 @@
|
||||
package VL08.Aufgabe03;
|
||||
|
||||
/**
|
||||
* A class to represent a Matrix.
|
||||
*/
|
||||
public class Matrix {
|
||||
private int rows = 0;
|
||||
private int columns = 0;
|
||||
private int[][] matrix;
|
||||
|
||||
Matrix(int rows, int columns) {
|
||||
this.rows = rows;
|
||||
this.columns = columns;
|
||||
this.matrix = new int[this.rows][this.columns];
|
||||
|
||||
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
|
||||
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
|
||||
this.matrix[rowIndex][columnIndex] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the number of columns.
|
||||
*
|
||||
* @return The number of columns.
|
||||
*/
|
||||
int getColumns() {
|
||||
return this.columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the number of rows.
|
||||
*
|
||||
* @return The number of rows.
|
||||
*/
|
||||
int getRows() {
|
||||
return this.rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the value of a specific field.
|
||||
*
|
||||
* @param row The row of the value.
|
||||
* @param column The column of the value.
|
||||
* @return The value of the given row and column.
|
||||
*/
|
||||
int getValue(int row, int column) {
|
||||
return this.matrix[row][column];
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to set the value of a specific field.
|
||||
*
|
||||
* @param row The row to set the value.
|
||||
* @param column The column to set the value.
|
||||
* @param value The value to be set in the field.
|
||||
*/
|
||||
void setValue(int row, int column, int value) {
|
||||
this.matrix[row][column] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Methode to get the state whether the given Matrix is same size.
|
||||
*
|
||||
* @param matrix The Matrix to check for same size.
|
||||
* @return The state whether the given Matrix is same size.
|
||||
*/
|
||||
boolean isSameSize(Matrix matrix) {
|
||||
return (this.rows == matrix.getRows()) && (this.columns == matrix.getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to add a Matrix to the current Matrix.
|
||||
*
|
||||
* @param matrix The Matrix to add to the current Matrix.
|
||||
*/
|
||||
void add(Matrix matrix) {
|
||||
if (!this.isSameSize(matrix)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
|
||||
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
|
||||
this.matrix[rowIndex][columnIndex] += matrix.getValue(rowIndex, columnIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to multiply a scalar value to the Matrix.
|
||||
*
|
||||
* @param scalar The scalar value to multiply the Matrix.
|
||||
*/
|
||||
void multiply(int scalar) {
|
||||
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
|
||||
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
|
||||
this.matrix[rowIndex][columnIndex] *= scalar;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to multiply a Matrix to the Matrix.
|
||||
*
|
||||
* @param matrix The Matrix to multiply with this Matrix.
|
||||
* @return State whether the multiplication of the Matrix was successful.
|
||||
*/
|
||||
boolean multiply(Matrix matrix) {
|
||||
|
||||
// multiplication is not possible if there are no rows or no columns.
|
||||
if (this.getColumns() == 0 || this.getRows() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// multiplication is only possible if the number of rows of Matrix A
|
||||
// is equals to the number of columns of Matrix B.
|
||||
if (this.getColumns() != matrix.getRows()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int[][] tempMatrix = new int[this.matrix.length][this.matrix.length];
|
||||
|
||||
for (int rowIndex = 0; rowIndex < this.getRows(); rowIndex++) {
|
||||
for (int columnIndex = 0; columnIndex < matrix.getColumns(); columnIndex++) {
|
||||
tempMatrix[rowIndex][columnIndex] = 0;
|
||||
|
||||
for (int fieldIndex = 0; fieldIndex < matrix.getRows(); fieldIndex++) {
|
||||
tempMatrix[rowIndex][columnIndex] += this.getValue(rowIndex, fieldIndex)
|
||||
* matrix.getValue(fieldIndex, columnIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.matrix = tempMatrix;
|
||||
this.columns = tempMatrix.length;
|
||||
this.rows = tempMatrix.length;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to print the Matrix.
|
||||
*/
|
||||
void print() {
|
||||
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
|
||||
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
|
||||
System.out.printf("%5d", this.getValue(rowIndex, columnIndex));
|
||||
}
|
||||
|
||||
System.out.print("\n");
|
||||
}
|
||||
}
|
||||
}
|
30
VL08/Aufgabe04/Aufgabe04.java
Normal file
30
VL08/Aufgabe04/Aufgabe04.java
Normal file
@ -0,0 +1,30 @@
|
||||
package VL08.Aufgabe04;
|
||||
|
||||
import VL08.Aufgabe04.PersonenDH.Person;
|
||||
import VL08.Aufgabe04.PersonenDH.Student;
|
||||
import VL08.Aufgabe04.PersonenDH.Studiengangsleiter;
|
||||
import VL08.Aufgabe04.PersonenDH.Dozent;
|
||||
import VL08.Aufgabe04.PersonenDH.Mitarbeiter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Vorlesung 8 / Aufgabe 4
|
||||
*
|
||||
* @author Sebastian Brosch
|
||||
*/
|
||||
public class Aufgabe04 {
|
||||
public static void main(String[] args) {
|
||||
ArrayList<Person> personen = new ArrayList<Person>();
|
||||
personen.add(new Person("Petra Mustermann", 37));
|
||||
personen.add(new Student("Max Mustermann", 30, "1234567"));
|
||||
personen.add(new Dozent("Eva Mustermann", 50, "Informatik"));
|
||||
personen.add(new Mitarbeiter("Tim Mustermann", 35, "Programmieren"));
|
||||
personen.add(new Studiengangsleiter("Kevin Mustermann", 40, "BWL", "BWL 1"));
|
||||
|
||||
for (Person person : personen) {
|
||||
person.print();
|
||||
System.out.println("-------------------------");
|
||||
}
|
||||
}
|
||||
}
|
15
VL08/Aufgabe04/PersonenDH/Dozent.java
Normal file
15
VL08/Aufgabe04/PersonenDH/Dozent.java
Normal file
@ -0,0 +1,15 @@
|
||||
package VL08.Aufgabe04.PersonenDH;
|
||||
|
||||
public class Dozent extends Person {
|
||||
private String specialization;
|
||||
|
||||
public Dozent(String name, int age, String specialization) {
|
||||
super(name, age);
|
||||
this.specialization = specialization;
|
||||
}
|
||||
|
||||
public void print() {
|
||||
super.print();
|
||||
System.out.printf("Fachrichtung: %s\n", specialization);
|
||||
}
|
||||
}
|
15
VL08/Aufgabe04/PersonenDH/Mitarbeiter.java
Normal file
15
VL08/Aufgabe04/PersonenDH/Mitarbeiter.java
Normal file
@ -0,0 +1,15 @@
|
||||
package VL08.Aufgabe04.PersonenDH;
|
||||
|
||||
public class Mitarbeiter extends Person {
|
||||
private String activity;
|
||||
|
||||
public Mitarbeiter(String name, int age, String activity) {
|
||||
super(name, age);
|
||||
this.activity = activity;
|
||||
}
|
||||
|
||||
public void print() {
|
||||
super.print();
|
||||
System.out.printf("Tätigkeit: %s\n", this.activity);
|
||||
}
|
||||
}
|
16
VL08/Aufgabe04/PersonenDH/Person.java
Normal file
16
VL08/Aufgabe04/PersonenDH/Person.java
Normal file
@ -0,0 +1,16 @@
|
||||
package VL08.Aufgabe04.PersonenDH;
|
||||
|
||||
public class Person {
|
||||
String name;
|
||||
int age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public void print() {
|
||||
System.out.printf("Name: %s\n", this.name);
|
||||
System.out.printf("Alter: %d\n", this.age);
|
||||
}
|
||||
}
|
15
VL08/Aufgabe04/PersonenDH/Student.java
Normal file
15
VL08/Aufgabe04/PersonenDH/Student.java
Normal file
@ -0,0 +1,15 @@
|
||||
package VL08.Aufgabe04.PersonenDH;
|
||||
|
||||
public class Student extends Person {
|
||||
private String studentNumber;
|
||||
|
||||
public Student(String name, int age, String studentNumber) {
|
||||
super(name, age);
|
||||
this.studentNumber = studentNumber;
|
||||
}
|
||||
|
||||
public void print() {
|
||||
super.print();
|
||||
System.out.printf("Matrikel-Nummer: %s\n", this.studentNumber);
|
||||
}
|
||||
}
|
15
VL08/Aufgabe04/PersonenDH/Studiengangsleiter.java
Normal file
15
VL08/Aufgabe04/PersonenDH/Studiengangsleiter.java
Normal file
@ -0,0 +1,15 @@
|
||||
package VL08.Aufgabe04.PersonenDH;
|
||||
|
||||
final public class Studiengangsleiter extends Dozent {
|
||||
private String course;
|
||||
|
||||
public Studiengangsleiter(String name, int age, String specialization, String course) {
|
||||
super(name, age, specialization);
|
||||
this.course = course;
|
||||
}
|
||||
|
||||
public void print() {
|
||||
super.print();
|
||||
System.out.printf("Kurs: %s\n", this.course);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user