Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the provided code: hello_user.rb: require 'date' require './input_functions' # Multiply metres by the following to get inches: INCHES = 39.3701 # Insert into

image text in transcribed

Here is the provided code:

hello_user.rb:

require 'date'

require './input_functions'

# Multiply metres by the following to get inches:

INCHES = 39.3701

# Insert into the following your hello_user code

# from task 1.3P and modify it to use the functions

# in input_functions

def main()

hello_user.rb:

# HOW TO USE THE input_functions CODE

# Example of how to read strings:

s = read_string('Enter a String: ')

puts("the string you entered was: " + s)

# Example of how to read integers:

i = read_integer('Enter an Integer: ')

puts("The Integer you entered was: " + i.to_s)

# Example of how to read floats:

f = read_float('Enter a float (eg: 3.142): ')

puts("The Float you entered was: " + f.to_s)

# Get the curent year from the system:

y = Date.today.year

puts("the current year is: " + y.to_s)

# Now if you know how to do all that

# Copy in your code from your completed

# hello_user Task 1.3 P. Then modify it to

# use the code in input_functions.

# use read_string for all strings (this will

# remove all whitespace)

end

main()

input_functions.rb:

# Display the prompt and return the read string

def read_string prompt

puts prompt

value = gets.chomp

end

# Display the prompt and return the read float

def read_float prompt

value = read_string(prompt)

value.to_f

end

# Display the prompt and return the read integer

def read_integer prompt

value = read_string(prompt)

value.to_i

end

# Read an integer between min and max, prompting with the string provided

def read_integer_in_range(prompt, min, max)

value = read_integer(prompt)

while (value max)

puts "Please enter a value between " + min.to_s + " and " + max.to_s + ": "

value = read_integer(prompt);

end

value

end

# Display the prompt and return the read Boolean

def read_boolean prompt

value = read_string(prompt)

case value

when 'y', 'yes', 'Yes', 'YES'

true

else

false

end

end

# Test the functions above

=begin

def main

puts "String entered is: " + read_string("Enter a String: ")

puts "Boolean is: " + read_boolean("Enter yes or no:").to_s

puts "Float is: " + read_float("Enter a floating point number: ").to_s

puts "Integer is: " + read_integer_in_range("Enter an integer between 3 and 6: ", 3, 6).to_s

end

main

=end

In this task, you need to use the functions in the llbrary input_functions. rb to create a simple interactive program. This is a varlation on your prevlous Hello User task. The output should look as follows: The steps needed are: Use the functions in input_functions.rb to do the following: Read the user's name (a String , prompt with 'Please enter your name: ') and store it in the name varlable. Print out the user's name followed by an exclamation mark. Read the user's family name and store it in the varlable. Print out the user's family_name followed by an exclamation mark. Read in the user's year of birth as an integer Calculate the user's age and print it out. Prompt for and read in the user's height in metres as a float Convert the helght to inches and print out the result. Use the constant INCHES at the top of the code to convert metres to inches Call the read_boolean function from input_functions.rb passing in the argument 'Do you want to continue?' If the read_boolean function returns true then display 'Ok, lets continue ', If the function returns talse then output 'ok, goodbye. This should look as follows: \begin{tabular}{l|l} 1 & continue = read_boolean( "Do you want to continue?") \\ 2 & if (continue) \\ 3 & \# ok message here \\ 4 & else \\ 5 & \# good bye message here \\ 6 & end \end{tabular}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions