Repetition Präsentation

This commit is contained in:
Sage The DM 2024-10-23 15:27:03 +02:00
parent a87e5186f6
commit f37709767f
13 changed files with 143 additions and 0 deletions

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,33 @@
// DateOfBirth class to manage birth date and age calculation
class DateOfBirth {
private int birthYear;
private int birthMonth;
private int birthDay;
// Constructor
public DateOfBirth(int birthYear, int birthMonth, int birthDay) {
this.birthYear = birthYear;
this.birthMonth = birthMonth;
this.birthDay = birthDay;
}
// Method to calculate the age based on the hardcoded current date (23.10.2024)
public int calculateAge() {
int currentYear = 2024;
int currentMonth = 10;
int currentDay = 23;
int age = currentYear - birthYear;
// Adjust the age if the birthday hasn't happened yet this year
if (birthMonth > currentMonth || (birthMonth == currentMonth && birthDay > currentDay)) {
age--;
}
return age;
}
// Method to return the birth date as a string
public String getBirthDate() {
return birthDay + "." + birthMonth + "." + birthYear;
}
}

Binary file not shown.

View file

@ -0,0 +1,38 @@
// Main class with methods to create, modify, and display a person
public class Main {
public static void main(String[] args) {
// Create a Person object with all parameters
Person person = createPerson();
// Print the introduction and list of information
printPersonDetails(person);
// Modify the person and return a new object
Person updatedPerson = modifyPerson(person, "Bob", "789 Oak Street");
// Print the updated person information
System.out.println("\nAfter modification:");
printPersonDetails(updatedPerson);
}
// Method to create and return a Person object
public static Person createPerson() {
DateOfBirth dob = new DateOfBirth(1994, 6, 15); // Example birth date
return new Person("Alice", dob, "1234 Elm Street");
}
// Method to print a person's details
public static void printPersonDetails(Person person) {
person.introduce(); // Print the introduction
person.listInformation(); // Print the full list of details
}
// Method to modify a person object and return the modified person (custom
// object as return type)
public static Person modifyPerson(Person originalPerson, String newName, String newAddress) {
// Modify the person's details and return the modified object
originalPerson.setName(newName); // Update the name
originalPerson.setAddress(newAddress); // Update the address
return originalPerson; // Return the modified object
}
}

Binary file not shown.

View file

@ -0,0 +1,47 @@
// Person class that uses the DateOfBirth class
class Person {
private String name;
private DateOfBirth dateOfBirth; // Using DateOfBirth for birth date management
private String address;
// Constructor
public Person(String name, DateOfBirth dateOfBirth, String address) {
this.name = name;
this.dateOfBirth = dateOfBirth;
this.address = address;
}
// Method to introduce the person
public void introduce() {
System.out.println("Hi, I'm " + name + ". I was born on " + dateOfBirth.getBirthDate() + " and I am "
+ dateOfBirth.calculateAge() + " years old.");
}
// Method to list the person's details
public void listInformation() {
System.out.println("Name: " + name);
System.out.println("Birth Date: " + dateOfBirth.getBirthDate());
System.out.println("Age: " + dateOfBirth.calculateAge());
System.out.println("Address: " + address);
}
// Getter for name
public String getName() {
return name;
}
// Setter for name
public void setName(String name) {
this.name = name;
}
// Getter for address
public String getAddress() {
return address;
}
// Setter for address
public void setAddress(String address) {
this.address = address;
}
}

Binary file not shown.