13.02.2025
This commit is contained in:
parent
70e5974226
commit
82bdd728c3
6 changed files with 223 additions and 0 deletions
61
src/de/dhbwka/java/exercise/classes/Account.java
Normal file
61
src/de/dhbwka/java/exercise/classes/Account.java
Normal file
|
@ -0,0 +1,61 @@
|
|||
package de.dhbwka.java.exercise.classes;
|
||||
|
||||
public class Account {
|
||||
private long AccountNumber;
|
||||
private String AccountHolder;
|
||||
private long AccountBalance;
|
||||
private long OverdraftLimit;
|
||||
|
||||
public Account(long accountNumber, String accountHolder, long accountBalance, long overdraftLimit) {
|
||||
AccountNumber = accountNumber;
|
||||
AccountHolder = accountHolder;
|
||||
AccountBalance = accountBalance;
|
||||
OverdraftLimit = overdraftLimit;
|
||||
}
|
||||
|
||||
public Account(long accountNumber, String accountHolder) {
|
||||
this(accountNumber, accountHolder, 0, 0);
|
||||
}
|
||||
|
||||
public long getAccountBalance() {
|
||||
return AccountBalance;
|
||||
}
|
||||
|
||||
public String getAccountHolder() {
|
||||
return AccountHolder;
|
||||
}
|
||||
|
||||
public long getAccountNumber() {
|
||||
return AccountNumber;
|
||||
}
|
||||
|
||||
public long getOverdraftLimit() {
|
||||
return OverdraftLimit;
|
||||
}
|
||||
|
||||
public void setAccountHolder(String accountHolder) {
|
||||
AccountHolder = accountHolder;
|
||||
}
|
||||
|
||||
public void setOverdraftLimit(long overdraftLimit) {
|
||||
OverdraftLimit = overdraftLimit;
|
||||
}
|
||||
|
||||
public void processDeposit(long amount) {
|
||||
AccountBalance += amount;
|
||||
}
|
||||
|
||||
public void processPayment(long amount) {
|
||||
if (AccountBalance - amount < -OverdraftLimit) {
|
||||
System.out.println("Deckung nicht ausreichend.");
|
||||
} else {
|
||||
AccountBalance -= amount;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Konto Nr. " + AccountNumber + " (" + AccountHolder + ") Stand: " + AccountBalance + "ct Limit: "
|
||||
+ OverdraftLimit + "ct";
|
||||
}
|
||||
}
|
14
src/de/dhbwka/java/exercise/classes/AccountTest.java
Normal file
14
src/de/dhbwka/java/exercise/classes/AccountTest.java
Normal file
|
@ -0,0 +1,14 @@
|
|||
package de.dhbwka.java.exercise.classes;
|
||||
|
||||
public class AccountTest {
|
||||
public static void main(String[] args) {
|
||||
Account account = new Account(4711, "Donald Duck", 500, 1000);
|
||||
System.out.println(account);
|
||||
account.processDeposit(200);
|
||||
System.out.println(account);
|
||||
account.processPayment(400);
|
||||
System.out.println(account);
|
||||
account.processPayment(2000);
|
||||
System.out.println(account);
|
||||
}
|
||||
}
|
56
src/de/dhbwka/java/exercise/classes/Point.java
Normal file
56
src/de/dhbwka/java/exercise/classes/Point.java
Normal file
|
@ -0,0 +1,56 @@
|
|||
package de.dhbwka.java.exercise.classes;
|
||||
|
||||
public class Point {
|
||||
private double x;
|
||||
private double y;
|
||||
|
||||
public Point() {
|
||||
this(0, 0);
|
||||
}
|
||||
|
||||
public Point(double x, double y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(double x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(double y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "(" + x + ", " + y + ")";
|
||||
}
|
||||
|
||||
public double distanceFromOrigin() {
|
||||
return Math.sqrt(x * x + y * y);
|
||||
}
|
||||
|
||||
public Point mirrorX() {
|
||||
return new Point(x, -y);
|
||||
}
|
||||
|
||||
public Point mirrorY() {
|
||||
return new Point(-x, y);
|
||||
}
|
||||
|
||||
public Point mirrorOrigin() {
|
||||
return new Point(-x, -y);
|
||||
}
|
||||
|
||||
public double getDistance(Point p) {
|
||||
return Math.sqrt(Math.pow(p.x - x, 2) + Math.pow(p.y - y, 2));
|
||||
}
|
||||
}
|
16
src/de/dhbwka/java/exercise/classes/PointTest.java
Normal file
16
src/de/dhbwka/java/exercise/classes/PointTest.java
Normal file
|
@ -0,0 +1,16 @@
|
|||
package de.dhbwka.java.exercise.classes;
|
||||
|
||||
public class PointTest {
|
||||
public static void main(String[] args) {
|
||||
Point pointA = new Point(4.0, 2.0);
|
||||
System.out.println("A: " + pointA);
|
||||
Point pointB = new Point(-1.0, -1.0);
|
||||
System.out.println("B: " + pointB);
|
||||
System.out.println("Abstand A-B: "
|
||||
+ pointA.getDistance(pointB));
|
||||
pointA = pointA.mirrorOrigin();
|
||||
System.out.println("A': " + pointA);
|
||||
System.out.println("Abstand A'-B: "
|
||||
+ pointA.getDistance(pointB));
|
||||
}
|
||||
}
|
52
src/de/dhbwka/java/exercise/classes/Radio.java
Normal file
52
src/de/dhbwka/java/exercise/classes/Radio.java
Normal file
|
@ -0,0 +1,52 @@
|
|||
package de.dhbwka.java.exercise.classes;
|
||||
|
||||
public class Radio {
|
||||
private boolean on;
|
||||
private int volume;
|
||||
private double frequency;
|
||||
|
||||
public Radio() {
|
||||
this.on = false;
|
||||
this.volume = 0;
|
||||
this.frequency = 85.0;
|
||||
}
|
||||
|
||||
public Radio(boolean on, int volume, double frequency) {
|
||||
this.on = on;
|
||||
this.volume = Math.max(Math.min(volume, 10), 0);
|
||||
this.frequency = Math.max(Math.min(frequency, 110.0), 85.0);
|
||||
}
|
||||
|
||||
public void turnOn() {
|
||||
this.on = true;
|
||||
}
|
||||
|
||||
public void turnOff() {
|
||||
this.on = false;
|
||||
}
|
||||
|
||||
public void incVolume() {
|
||||
if (this.volume < 10) {
|
||||
this.volume++;
|
||||
}
|
||||
}
|
||||
|
||||
public void decVolume() {
|
||||
if (this.volume > 0) {
|
||||
this.volume--;
|
||||
}
|
||||
}
|
||||
|
||||
public void setFrequency(double frequency) {
|
||||
if (frequency >= 85.0 && frequency <= 110.0) {
|
||||
this.frequency = frequency;
|
||||
} else {
|
||||
this.frequency = 99.9;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Radio " + (this.on ? "an" : "aus") + ": Freq=" + this.frequency + ", Laut=" + this.volume;
|
||||
}
|
||||
}
|
24
src/de/dhbwka/java/exercise/classes/RadioTest.java
Normal file
24
src/de/dhbwka/java/exercise/classes/RadioTest.java
Normal file
|
@ -0,0 +1,24 @@
|
|||
package de.dhbwka.java.exercise.classes;
|
||||
|
||||
public class RadioTest {
|
||||
public static void main(String[] args) {
|
||||
Radio radio = new Radio(false, 7, 93.5);
|
||||
System.out.println(radio);
|
||||
radio.turnOn();
|
||||
System.out.println(radio);
|
||||
radio.incVolume();
|
||||
radio.incVolume();
|
||||
System.out.println(radio);
|
||||
radio.incVolume();
|
||||
radio.incVolume();
|
||||
System.out.println(radio);
|
||||
radio.decVolume();
|
||||
System.out.println(radio);
|
||||
radio.setFrequency(97.8);
|
||||
System.out.println(radio);
|
||||
radio.setFrequency(112.7);
|
||||
System.out.println(radio);
|
||||
radio.turnOff();
|
||||
System.out.println(radio);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue