Section5.49 - COMPLETE

This commit is contained in:
2023-10-03 14:17:13 +02:00
parent 053656ce3f
commit 3695507896
5 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
/*
Coding Exercise #1
Using the var keyword, declare 4 variables called a, b, c, d of type int, float64, bool and string.
Using short declaration syntax declare and assign these values to variables x, y and z:
- 20
- 15.5
- "Gopher!"
Using fmt.Println() print out the values of a, b, c, d, x, y and z.
Try to run the program without error.
package main
import "fmt"
func main() {
var a int
var b float64
var c bool
var d string
x := 20
y := 15.5
z := "Gopher!"
fmt.Println(a, b, c, d)
fmt.Println("X is", x, "\nY is", y, "\nZ is", z)
}
*/
/*
Coding Exercise #2
Change the code from the previous exercise in the following way:
1. Declare a, b, c, d using a single var keyword (multiple variable declaration) for better readability.
2. Declare x, y and z on a single line -> multiple short declarations
3. Remove the statement that prints out the variables. See the error!
4. Change the program to run without error using the blank identifier (_)
Are you stuck? Do you want to see the solution for this exercise? Click here.
package main
func main() {
var (
a int
b float64
c bool
d string
)
x, y, z := 20, 15.5, "Gopher!"
_, _, _, _, _, _, _ = a, b, c, d, x, y, z
}
*/
/*
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
func main() {
var a float64 = 7.1
x, y := true, 3.7
a, x := 5.5, false
_, _, _ = a, x, y
}
package main
func main() {
var a float64 = 7.1
x, y := true, 3.7
a, x = 5.5, false
_, _, _ = a, x, y
}
*/
/*
Coding Exercise #4
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
version := "3.1"
func main() {
name := 'Golang'
fmt.Println(name)
}
*/
package main
import "fmt"
var version = "3.1"
func main() {
name := "Golang"
fmt.Println(name)
}

View File

@@ -0,0 +1,157 @@
/*
Coding Exercise #1
Using the const keyword declare and initialize the following constants:
1. daysWeek with value 7
2. lightSpeed with value 299792458
3. pi with value 3.14159
Run the program without errors.
package main
func main() {
const daysWeek = 7
const lightSpeed = 299792458
const pi = 3.14159
}
*/
/*
Coding Exercise #2
Change the code from the previous exercise and declare all 3 constants as grouped constants.
Make them untyped.
func main() {
const (
daysWeek = 7
lightSpeed = 299792458
pi = 3.14159
)
}
*/
/*
Coding Exercise #3
Calculate how many seconds are in a year.
STEPS:
1. Declare secPerDay constant and initialize it to the number of seconds in a day
2. Declare daysYear constant and initialize it to 365
3. Use fmt.Printf() to print out the total number of seconds in a year.
EXPECTED OUTPUT:
There are 31536000 seconds in a year.
package main
import "fmt"
func main() {
const (
daysYear = 365
hoursDay = 24
secondsHour = 3600
)
var result = daysYear * hoursDay * secondsHour
fmt.Printf("Seconds in a Year %d\n", result)
}
*/
/*
Coding Exercise #4
There are an error in the following Go program. Try to identify the error, change the code and run the program without errors.
package main
func main() {
const x int = 10
// declaring a constant of type slice int ([]int)
const m = []int{1: 3, 4: 5, 6: 8}
_ = m
}
package main
func main() {
const x int = 10
// declaring a constant of type slice int ([]int)
// const m = []int{1: 3, 4: 5, 6: 8}
// You cannot declare a slice constant
// _ = m
}
*/
/*
Coding Exercise #5
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 "math"
func main() {
const a int = 7
const b float64 = 5.6
const c = a * b
x := 8
const xc int = x
const noIPv6 = math.Pow(2, 128)
}
package main
// import "math"
func main() {
const a int = 7
const b float64 = 5.6
const c = float64(a) * b
x := 8
_ = x
// const xc int = x
// const noIPv6 = math.Pow(2, 128)
}
*/
/*
Coding Exercise #6
Using Iota declare the following months of the year: Jun, Jul and Aug
Jun, Jul and Aug are constant and their value is 6, 7 and 8.
*/
package main
import "fmt"
func main() {
const (
Jun = iota + 6
Jul
Aug
)
fmt.Println(Jun, Jul, Aug)
}

View File

@@ -0,0 +1,83 @@
/*
Coding Exercise #1
Consider the following variable declarations:
x, y, z := 10, 15.5, "Gophers"
score := []int{10, 20, 30}
Using fmt.Printf():
Print each variable using a specific verb for its type
Print the string value enclosed by double quotes ("Gophers")
Print each variable using the same verb
Print the type of y and score
package main
import "fmt"
func main() {
x, y, z := 10, 15.5, "Gophers"
score := []int{10, 20, 30}
fmt.Printf("x is %d, y is %f, z is %s\n", x, y, z)
fmt.Printf("score is %#v\n", score)
fmt.Printf("Values are %v,%v,%v,%v \n", x, y, z, score)
fmt.Printf("y is %T\nscore is %T\n", y, score)
}
*/
/*
Coding Exercise #2
Consider the following constant declaration: const x float64 = 1.422349587101
Write a Go program that prints the value of x with 4 decimal points.
package main
import "fmt"
func main() {
const x float64 = 1.422349587101
fmt.Printf("%.4f\n", x)
}
*/
/*
Coding Exercise #3
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"
func main() {
shape := "circle"
radius := 3.2
const pi float64 = 3.14159
circumference := 2 * pi * radius
fmt.Printf("Shape: %q\n")
fmt.Printf("Circle's circumference with radius %d is %b\n", radius, circumference)
_ = shape
}
*/
package main
import "fmt"
func main() {
shape := "circle"
radius := 3.2
const pi float64 = 3.14159
var circumference float64 = float64(2) * pi * radius
fmt.Printf("Shape: %q\n", shape)
fmt.Printf("Circle's circumference with radius %f is %f\n", radius, circumference)
}

View File

@@ -0,0 +1,194 @@
/*
Coding Exercise #1
Consider the following declarations:
var i int = 3
var f float64 = 3.2
Write a Go program that converts i to float64 and f to int.
Print out the type and the value of the variables created after conversion.
package main
import "fmt"
func main() {
var i int = 3
var f float64 = 3.2
fmt.Printf("Type of i is %T and f is %T\n", i, f)
i1 := float64(i)
f1 := int(f)
fmt.Printf("Type of i1 is %T and value is %f\n", i1, i1)
fmt.Printf("Type of f1 is %T and value is %d\n", f1, f1)
}
*/
/*
Coding Exercise #2
Consider the following declarations:
var i = 3
var f = 3.2
var s1, s2 = "3.14", "5"
Write a Go program that converts:
1. i to string (int to string)
2. s2 to int (string to int)
3. f to string (float64 to string)
4. s1 to float64 (string to float64)
5. Print the value and the type for each variable created after conversion.
package main
import (
"fmt"
"strconv"
)
func main() {
var i = 3
var f = 3.2
var s1, s2 = "3.14", "5"
// 1. int to string
s := strconv.Itoa(i)
fmt.Printf("s Type is %T, s value is %q\n", s, s)
// 2. string to int
is, err := strconv.Atoi(s2)
if err == nil {
fmt.Printf("i type is %T, i value is %v\n", is, is)
} else {
fmt.Println("Can not convert string to int.")
}
// 3. float64 to string
ss1 := fmt.Sprintf("%f", f)
fmt.Printf("ss1's type: %T, ss1's value: %s\n", ss1, ss1)
// 4. string to float64
f1, err1 := strconv.ParseFloat(s1, 64)
if err1 == nil {
fmt.Printf("f1's type: %T, f1's value: %v\n", f1, f1)
} else {
fmt.Println("Cannot convert string to float64.")
}
}
*/
/*
Coding Exercise #3
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"
func main() {
x, y := 4, 5.1
z := x * y
fmt.Println(z)
a := 5
b := 6.2 * a
fmt.Println(b)
}
package main
import "fmt"
func main() {
x, y := 4, 5.1
z := float64(x) * y
fmt.Println(z)
a := 5
b := 6.2 * float64(a)
fmt.Println(b)
}
*/
/*
Coding Exercise #4
Create a Go program that computes how long does it take for the Sunlight to reach the Earth if we know that the distance from the Sun to Earth is 149.6 million km and the speed of light is 299,792,458 m / s
The formula used is: time = distance / speed
package main
import "fmt"
func main() {
d := 149600000 * 1000
s := 299792458
fmt.Printf("Speed of light is %d\n", s)
fmt.Printf("Distance from Sun to Earth is %d\n", d)
t := d / s
fmt.Printf("Sunlight reaches Earth in %v seconds\n", t)
}
*/
/*
Coding Exercise #5
Write the correct logical operator (&&, || , !) inside the expression so that result1 will be false and result2 will be true.
Program source code: https://play.golang.org/p/74SCleChC20
package main
import "fmt"
func main() {
x, y := 0.1, 5
var z float64
// Write the correct logical operator (&&, || , !)
// inside the expression so that result1 will be false and result2 will be true.
result1 := x < z int(x) != int(z)
fmt.Println(result1)
result2 := y == 1 * 5 int(z) == 0
fmt.Println(result2)
}
*/
package main
import "fmt"
func main() {
x, y := 0.1, 5
var z float64
// Write the correct logical operator (&&, || , !)
// inside the expression so that result1 will be false and result2 will be true.
result1 := x < z || int(x) != int(z)
fmt.Println(result1)
result2 := y == 1 * 5 || int(z) == 0
fmt.Println(result2)
}

View File

@@ -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)
}