diff --git a/go-1-ex-1/main.go b/go-1-ex-1/main.go index 7b86720..7b37413 100644 --- a/go-1-ex-1/main.go +++ b/go-1-ex-1/main.go @@ -4,6 +4,18 @@ import "fmt" func main() { // TODO: Declare and initialize the variables being used in the output! + + // Aufgabe + var firstName string = "Selina" + var lastName string = "Frey" + var dayOfBirth int = 17 + var monthOfBirth int = 07 + var yearOfBirth int = 2007 + var numberOfSiblings int = 3 + heightInMeters := 1.55 + var zodiacSign = 'Cancer' + // Aufgabe + fmt.Printf("Vor- und Nachname: %s %s\n", firstName, lastName) fmt.Printf("Geburtsdatum: %d.%d.%d\n", dayOfBirth, monthOfBirth, yearOfBirth) fmt.Printf("Anzahl Geschwister: %d\n", numberOfSiblings) diff --git a/go-1-ex-2/main.go b/go-1-ex-2/main.go index edbce3c..2897855 100644 --- a/go-1-ex-2/main.go +++ b/go-1-ex-2/main.go @@ -1,3 +1,4 @@ + package main import "fmt" @@ -13,10 +14,10 @@ func main() { fmt.Printf("%.2f°F = %.2f°C\n", fahrenheit, celsius) const marathonInKM = 42.195 - var marathonInMiles = 0.0 // TODO: calculate using mileInKM! + var marathonInMiles = marathonInKM / mileInKM // TODO: calculate using mileInKM! fmt.Printf("a marathon is %.2f kilometres = %.2f miles long\n", marathonInKM, marathonInMiles) var boilingWaterCelsius = 100.0 - var boilingWaterFahrenheit = 0.0 // TODO: calculate using formula above! + var boilingWaterFahrenheit = boilingWaterCelsius*9/5 + 32 // TODO: calculate using formula above! fmt.Printf("water boils at %.2f°C = %.2f°F\n", boilingWaterCelsius, boilingWaterFahrenheit) } diff --git a/go-1-ex-3/main.go b/go-1-ex-3/main.go index e98ac5c..8e1f601 100644 --- a/go-1-ex-3/main.go +++ b/go-1-ex-3/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "math/rand" + "os" "time" ) @@ -11,10 +12,12 @@ func main() { var when = time.Now() // TODO: use fmt.Fprintln instead! - fmt.Println("the dice shows", eyes, "eyes") + // fmt.Println("the dice shows", eyes, "eyes") + fmt.Fprintln(os.Stdout, "the dice shows", eyes, "eyes") // TODO: use fmt.Fprintln instead! - fmt.Println("the dice was rolled at", when) + // fmt.Println("the dice was rolled at", when) + fmt.Fprintln(os.Stdout, "the dice was rolled at", when) // TODO: how to write the output into eyes.txt and dice.log? // go run ex3/main.go TODO diff --git a/go-2-ex-1/main.go b/go-2-ex-1/main.go index aab7a0b..dbcd00b 100644 --- a/go-2-ex-1/main.go +++ b/go-2-ex-1/main.go @@ -3,13 +3,22 @@ package main import "fmt" type FullName struct { - // TODO: add fields + // TODO: add fields$ + FirstName string + LastName string } // TODO: declare a structure for birth date - +type BirthDate struct { + DayOfBirth byte + MonthOfBirth byte + YearOfBirth int16 +} + type Profile struct { // TODO: embed full name and birth date information + FullName + BirthDate NumberOfSiblings byte ZodiacSign rune } @@ -17,12 +26,24 @@ type Profile struct { func main() { var me = Profile{ // TODO: set name and birth date information - NumberOfSiblings: 0, // TODO: adjust - ZodiacSign: ' ', // TODO: adjust + FullName: FullName{ + FirstName:"Selina", + LastName:"Frey", + }, + BirthDate: BirthDate{ + DayOfBirth: 17, + MonthOfBirth:7, + YearOfBirth: 2007, + }, + NumberOfSiblings: 3, // TODO: adjust + ZodiacSign: '\u264B', + } fmt.Println(me) fmt.Println("Siblings Before:", me.NumberOfSiblings) // TODO: imagine, you get a little brother or sister + me.NumberOfSiblings = me.NumberOfSiblings + 1 + fmt.Println("Siblings After:", me.NumberOfSiblings) } diff --git a/go-2-ex-2/main.go b/go-2-ex-2/main.go index 1c2a2c1..476ae35 100644 --- a/go-2-ex-2/main.go +++ b/go-2-ex-2/main.go @@ -6,10 +6,16 @@ func main() { var fibs = []int{1, 1, 0, 0, 0} fibs[2] = fibs[0] + fibs[1] - // TODO: correct up to index 4 using direct element access + // TODO: correct up to index 4 using direct element access + fibs[3] = fibs[1] + fibs[2] + fibs[4] = fibs[2] + fibs[3] - fibs = append(fibs, 0) // TODO: replace 0 with the next Fibonacci number - // TODO: compute three more Fibonacci numbers and append them + + fibs = append(fibs, fibs[3] + fibs[4]) // TODO: replace 0 with the next Fibonacci number + // TODO: compute three more Fibonacci numbers and append them + fibs = append(fibs, fibs[4] + fibs[5]) + fibs = append(fibs, fibs[5] + fibs[6]) + fibs = append(fibs, fibs[6] + fibs[7]) fmt.Println(fibs) // expected output: [1 1 2 3 5 8 13 21 34] } diff --git a/go-2-ex-3/main.go b/go-2-ex-3/main.go index 0579766..a8289f1 100644 --- a/go-2-ex-3/main.go +++ b/go-2-ex-3/main.go @@ -4,13 +4,24 @@ import "fmt" func main() { // TODO: create a map called "modules" + modules := map[int]string{ + 104: "Informatik und Gesellschaft", + 117: "Datenbanken", + 346: "Go Programmierung", + } fmt.Println("Modul 104:", modules[104]) fmt.Println("Modul 117:", modules[117]) fmt.Println("Modul 346:", modules[346]) // TODO: delete one + delete(modules, 117) + // TODO: add one + modules[320] = "Netzwerke" + // TODO: replace one + modules[104] = "IT und Gesellschaft" + fmt.Println(modules) } diff --git a/go-2-ex-4/main.go b/go-2-ex-4/main.go index b69d3d1..70746ca 100644 --- a/go-2-ex-4/main.go +++ b/go-2-ex-4/main.go @@ -1,8 +1,37 @@ package main +import "fmt" + func main() { // TODO: declare a type for Student (with first and last name) + type Student struct { + First string + Last string + } + // TODO: declare a type for Class (consisting of multiple students) + type Class []Student + // TODO: declare a map of modules being attended by multiple classes + //this was solved with the help of chatgpt + modules := map[string][]Class{ + "Modul 104": { + { + {First: "Anna", Last: "Muster"}, + {First: "Ben", Last: "Beispiel"}, + }, + { + {First: "Clara", Last: "Test"}, + {First: "David", Last: "Demo"}, + }, + }, + "Modul 346": { + { + {First: "Eva", Last: "Practice"}, + }, + }, + } + // TODO: output everything using fmt.Println() + fmt.Println(modules) } diff --git a/go-3-ex-1/main.go b/go-3-ex-1/main.go index 41943e3..aedf323 100644 --- a/go-3-ex-1/main.go +++ b/go-3-ex-1/main.go @@ -22,6 +22,34 @@ func outputWithZodiacSign(p Person) { // TODO: Assign proper value to zodiacSign using if/else branching. // NOTE: The runes are defined above as constants. + day := p.Day + month := p.Month + + if (month == 3 && day >= 21) || (month == 4 && day <= 20) { + zodiacSign = Aries + } else if (month == 4 && day >= 21) || (month == 5 && day <= 20) { + zodiacSign = Taurus + } else if (month == 5 && day >= 21) || (month == 6 && day <= 21) { + zodiacSign = Gemini + } else if (month == 6 && day >= 22) || (month == 7 && day <= 22) { + zodiacSign = Cancer + } else if (month == 7 && day >= 23) || (month == 8 && day <= 23) { + zodiacSign = Leo + } else if (month == 8 && day >= 24) || (month == 9 && day <= 23) { + zodiacSign = Virgo + } else if (month == 9 && day >= 24) || (month == 10 && day <= 23) { + zodiacSign = Libra + } else if (month == 10 && day >= 24) || (month == 11 && day <= 22) { + zodiacSign = Scorpius + } else if (month == 11 && day >= 23) || (month == 12 && day <= 21) { + zodiacSign = Sagittarius + } else if (month == 12 && day >= 22) || (month == 1 && day <= 20) { + zodiacSign = Capricornus + } else if (month == 1 && day >= 21) || (month == 2 && day <= 19) { + zodiacSign = Aquarius + } else if (month == 2 && day >= 20) || (month == 3 && day <= 20) { + zodiacSign = Pisces + } fmt.Printf("%s %s, born on %02d.%02d.%04d, has the zodiac sign %c.\n", p.FirstName, p.LastName, p.Day, p.Month, p.Year, zodiacSign) diff --git a/go-3-ex-2/main.go b/go-3-ex-2/main.go index 187a2c9..b593662 100644 --- a/go-3-ex-2/main.go +++ b/go-3-ex-2/main.go @@ -20,15 +20,35 @@ const ( func outputDateRange(zodiacSign rune) { fmt.Printf("%c: ", zodiacSign) // TODO: Replace if, else if branching with switch/case. - // TODO: Define all 12 cases... - if zodiacSign == Aries { + switch zodiacSign { + case Aries: fmt.Println("21.03. - 20.04") - } else if zodiacSign == Taurus { + case Taurus: fmt.Println("21.04. - 21.05") - } else { - fmt.Println("") + case Gemini: + fmt.Println("22.05. - 21.06") + case Cancer: + fmt.Println("22.06. - 22.07") + case Leo: + fmt.Println("23.07. - 23.08") + case Virgo: + fmt.Println("24.08. - 23.09") + case Libra: + fmt.Println("24.09. - 23.10") + case Scorpius: + fmt.Println("24.10. - 22.11") + case Sagittarius: + fmt.Println("23.11. - 21.12") + case Capricornus: + fmt.Println("22.12. - 20.01") + case Aquarius: + fmt.Println("21.01. - 19.02") + case Pisces: + fmt.Println("20.02. - 20.03") + default: + // TODO: ...and consider a default case. + fmt.Println("Unknown zodiac sign") } - // TODO: ...and consider a default case. } func main() { diff --git a/go-3-ex-3/main.go b/go-3-ex-3/main.go index 5ebaafe..6587279 100644 --- a/go-3-ex-3/main.go +++ b/go-3-ex-3/main.go @@ -1,5 +1,7 @@ package main +import "fmt" + const ( Lower = 1 Upper = 30 @@ -7,4 +9,17 @@ const ( func main() { // TODO: Implement FizzBuzz using a for loop from Lower to Upper. + for i := Lower; i <= Upper; i++ { + switch { + case i%15 == 0: + fmt.Println("FizzBuzz") + case i%3 == 0: + fmt.Println("Fizz") + case i%5 == 0: + fmt.Println("Buzz") + default: + fmt.Println(i) + } + } } +s \ No newline at end of file diff --git a/go-3-ex-4/main.go b/go-3-ex-4/main.go index 9eb5868..3926ad8 100644 --- a/go-3-ex-4/main.go +++ b/go-3-ex-4/main.go @@ -19,12 +19,19 @@ const ( Ace = 'A' ) + func main() { suits := []rune{Diamonds, Spades, Clubs, Hearts} ranks := []rune{Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace} // TODO: Loop over suits and ranks to output all combinations. + for _, s := range suits { + for _, r := range ranks { + fmt.Printf("%c%c\n", s, r) + } + } // TODO: delete this line afterwards fmt.Println(suits, ranks) } +