From 3695507896aa6ef5444748ca4d564528ec04fa1a Mon Sep 17 00:00:00 2001 From: sebastian Date: Tue, 3 Oct 2023 14:17:13 +0200 Subject: [PATCH] Section5.49 - COMPLETE --- .../45.go | 0 .../46.go | 0 .../47.go | 0 .../48.go | 0 Section5 - Exercices S1-2-3-4/49.go | 95 +++++++++++++++++++ 5 files changed, 95 insertions(+) rename {Section5 => Section5 - Exercices S1-2-3-4}/45.go (100%) rename {Section5 => Section5 - Exercices S1-2-3-4}/46.go (100%) rename {Section5 => Section5 - Exercices S1-2-3-4}/47.go (100%) rename {Section5 => Section5 - Exercices S1-2-3-4}/48.go (100%) create mode 100644 Section5 - Exercices S1-2-3-4/49.go diff --git a/Section5/45.go b/Section5 - Exercices S1-2-3-4/45.go similarity index 100% rename from Section5/45.go rename to Section5 - Exercices S1-2-3-4/45.go diff --git a/Section5/46.go b/Section5 - Exercices S1-2-3-4/46.go similarity index 100% rename from Section5/46.go rename to Section5 - Exercices S1-2-3-4/46.go diff --git a/Section5/47.go b/Section5 - Exercices S1-2-3-4/47.go similarity index 100% rename from Section5/47.go rename to Section5 - Exercices S1-2-3-4/47.go diff --git a/Section5/48.go b/Section5 - Exercices S1-2-3-4/48.go similarity index 100% rename from Section5/48.go rename to Section5 - Exercices S1-2-3-4/48.go diff --git a/Section5 - Exercices S1-2-3-4/49.go b/Section5 - Exercices S1-2-3-4/49.go new file mode 100644 index 0000000..81fad00 --- /dev/null +++ b/Section5 - Exercices S1-2-3-4/49.go @@ -0,0 +1,95 @@ +/* +Coding Exercise #1 + + Declare a new type type called duration. Have the underlying type be an int. + + Declare a variable of the new type called hour using the var keyword + + In function main: + + print out the value of the variable hour + + print out the type of the variable hour + + assign 3600 to the variable hour using the = operator + + print out the value of hour + +package main + +import "fmt" + +type duration int + +func main() { + var hour duration + fmt.Printf("hour's type: %T, hour's value: %v\n", hour, hour) + hour = 3600 + fmt.Printf("hour's value %v\n", hour) +} +*/ + +/* +Coding Exercise #2 + +There are some errors in the following Go program. Try to identify the errors, change the code and run the program without errors. + + package main + + import "fmt" + + type duration int + + func main() { + var hour duration = 3600 + minute := 60 + fmt.Println(hour != minute) + } + +package main + +import "fmt" + +type duration int + +func main() { + var hour duration = 3600 + minute := duration(60) + fmt.Println(hour != minute) +} +*/ + +/* +Coding Exercise #3 + + Declare two defined types called mile and kilometer. Have the underlying type be an float64. + + Declare a constant called m2km equals 1.609 ( 1mile=1.609km) + + In function main: + + declare a variable called mileBerlinToParis of type mile with value 655.3 + + declare a variable called kmBerlinToParis of type kilometer + + calculate the distance between Berlin and Paris in km by multiplying mileBerlinToParis and m2km. Assign the result to kmBerlinToParis + + print out the distance in km between Berlin and Paris +*/ +package main + +import "fmt" + +type mile float64 +type kilometer float64 + +const m2km = 1.609 + +func main() { + var mileBerlinToParis mile = 655.3 //distance in miles + var kmBerlinToParis kilometer + + kmBerlinToParis = kilometer(mileBerlinToParis * m2km) + fmt.Printf("Distance in Km from Berlin to Paris is %f\n", kmBerlinToParis) + +}