Question

In the software RUBY Create The first method, unsafe? will take in an argument of a...

In the software RUBY

Create

  • The first method, unsafe? will take in an argument of a speed and return true if the speed is unsafe and false if it is safe.
    • Use an if/else statement pair to build the unsafe? method. It should return true if the speed is either below 40 or above 60. Going 30 mph on the freeway would be unsafe, as would going 95 mph. Going 50 miles per hour, however, would return false as that's within the "safe" range.
  • Build the method not_safe? that is a version of your previous unsafe? method but use the ternary operator (?:) instead of an if/else statement pair.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

RUBY Code:

Using if-else:

def unsafe(speed)
   # If Else
   if speed < 40 || speed > 60
       return true
   elsif
       return false
   end

end
puts unsafe 50
puts unsafe 95

Sample Run:

$ruby main.rb false true 1 - def unsafe( speed) 2 # If Else 3 if speed < 40 || speed > 60 4 return true 5 end 6 return false

_____________________________________________________________________________________________________________________

Using Ternary Operator:

def not_safe(speed)
   # Ternary Operator
   return ((speed < 40 || speed > 60) ? true : false);
end

puts not_safe 50
puts not_safe 95

Sample Run:

$ruby main.ro false 1. def not_safe(speed) 2 # Ternary Operator 3 return ((speed < 40 || speed > 60) ? true : false); 4 end 5

Add a comment
Know the answer?
Add Answer to:
In the software RUBY Create The first method, unsafe? will take in an argument of a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • QUESTION 1 Which statement results in the value false? The value of count is 0; limit...

    QUESTION 1 Which statement results in the value false? The value of count is 0; limit is 10. (count != 0)&&(limit < 20) (count == 0)&&(limit < 20) (count != 0)||(limit < 20) (count == 0)&&(limit < 20) 10 points    QUESTION 2 If this code fragment were executed in an otherwise correct and complete program, what would the output be? int a = 3, b = 2, c = 5 if (a > b) a = 4; if (...

  • Modify the Auction class according to these exercises: 4.48: close method 4.49: getUnsold method 4.51: Rewrite getLot method 4.52: removeLot method For help watch Textbook Video Note 4.2 which sho...

    Modify the Auction class according to these exercises: 4.48: close method 4.49: getUnsold method 4.51: Rewrite getLot method 4.52: removeLot method For help watch Textbook Video Note 4.2 which shows you how to use and test the auction project, as well as solving the getUnsold exercise. Document with javadoc and use good style (Appendix J) as usual. Test your code thoroughly as you go. Create a jar file of your project. From BlueJ, choose Project->Create Jar File... Check the "Include...

  • java code please 14. Next we'll complete the setValues() method. Instead of giving you exact code...

    java code please 14. Next we'll complete the setValues() method. Instead of giving you exact code I'll tell you what to do. Use other code as a guide.  Start with a for loop with a start of int i = 0  Set the ending point of when i is less than the length of the arr array.  Set the incrementation to increase i by 1 using the unary operator ++.  Within the loop set the arr...

  • Matching: What are 4 fundamental activities in software processes? definition of the software function and operational...

    Matching: What are 4 fundamental activities in software processes? definition of the software function and operational constraints Answer 1Choose...software developmentsoftware evolutionsoftware specificationsoftware validation software is modified to reflect changing customer / market needs Answer 2Choose...software developmentsoftware evolutionsoftware specificationsoftware validation design and implementation of the software code Answer 3Choose...software developmentsoftware evolutionsoftware specificationsoftware validation process of ensuring the software meets the customer's requirements Answer 4Choose...software developmentsoftware evolutionsoftware specificationsoftware validation Please answer all parts of the question. Question 2 Answer saved Marked out...

  • PLEASE HURRY Software Testing Don't make any changes to the provided code. Please write a test...

    PLEASE HURRY Software Testing Don't make any changes to the provided code. Please write a test case for the below prompt using the provided java programs Use the setString() function in MyCustomStringInterface to set the value to “Peter Piper picked a peck of pickled peppers.”. Then test to see if reverseNCharacters() function returns the reversed string when the characters are reversed in groups of 4 and padding is disabled (in this case, “etePiP r repkcipa decep fo kcip delkpep srep.”)....

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • Your goal is to create an ‘Array’ class that is able to hold multiple integer values....

    Your goal is to create an ‘Array’ class that is able to hold multiple integer values. The ‘Array’ class will be given functionality through the use of various overloaded operators You will be given the main() function for your program and must add code in order to achieve the desired result. Do not change any code in main(). If something is not working, you must change your own code, not the code in main(). Assignment 5: overloading member functions. Overview:...

  • Could someone help me out with the following python question? Preferably using the "return a <=...

    Could someone help me out with the following python question? Preferably using the "return a <= b < c" method instead of a standard if else statement. I would appreciate a step by step explanation if possible. Thank you! Write a class called Appointment with methods as described below: __init__() method Define an __init__() method with four parameters: self name, a string indicating the name of the appointment (for example, "Sales brunch"). start, a tuple consisting of two integers representing...

  • SELF-CHECK Why did the first version of method search that passed the first test itemNotFirstElementInSingleElementArray contain...

    SELF-CHECK Why did the first version of method search that passed the first test itemNotFirstElementInSingleElementArray contain only the statement return −1? Assume the first JUnit test for the findLargest method described in Self-Check exercise 2 in section 3.4 is a test that determines whether the first item in a one element array is the largest. What would be the minimal code for a method findLargest that passed this test? PROGRAMMING Write the findLargest method described in self-check exercise 2 in...

  • python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a...

    python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a function named newton. This function expects the input number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute square roots of inputs until she presses the enter/return key. 2.Convert Newton’s method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT