Hackety Hacking Problem Spring Oscillation Doend: Difference between revisions

From OLPC
Jump to navigation Jump to search
(New page: == Problem Statement == Your physics teacher is teaching you the principles of Oscillations. In a demonstration, She shows you how a spring oscillates. After compressing it to 1cm, accor...)
 
(cat)
 
(2 intermediate revisions by one other user not shown)
Line 26: Line 26:


<pre>
<pre>
#this block takes input from the user
# This block takes input from the user
puts("Please enter the Spring Constant of spring and press ENTER:")
puts("Please enter the Spring Constant of spring and press ENTER:")
k=gets.to_i
k=gets.to_i
puts("\nThe Force at Compressed State = "+(-1*k).to_s)
puts("\nThe Force at relaxed state = 0 N")


#l is the string that accumulates the whole data to be printed
l="The simulation of Spring motion is as :"
#count is the integer variable whose value is being incremented
#count is the integer variable whose value is being incremented
count=0
count=0

#the first loop is the loop used for incrementing the value of count from 0 10
# Display the Forces at Extreme Positions
puts("\nThe Force at Compressed State = "+(-1*k).to_s)
puts("\nThe Force at relaxed state = 0 N")

# L is the string that accumulates the whole data to be printed
L="The simulation of Spring motion is as :"

# The first loop is the loop used for incrementing the value of count from 0 10
k.times do
k.times do
l+=count.to_s+" ,"
L+=count.to_s+" ,"
count+=1
count+=1
end
end
#the second loop this is used to vary the value of coutn from 10 to 0
# The second loop this is used to vary the value of counting from 10 to 0
k.times do
k.times do
l+=count.to_s+" ,"
L+=count.to_s+" ,"
count-=1
count-=1
end
end
l+="0"
L+="0"

#Place the output on the screen
#Place the output on the screen
puts(l)
puts(L)
</pre>
</pre>
[[Category: Hackety Lesson Plan]]

Latest revision as of 20:08, 6 April 2008

Problem Statement

Your physics teacher is teaching you the principles of Oscillations. In a demonstration, She shows you how a spring oscillates. After compressing it to 1cm, according to Hooke's Law, which is formulated as F = -K*x , the spring starts oscillating. Write a program which could plot the variation of force with change in length of the spring?

Sample Output

Please enter the Spring Constant of Spring and press ENTER: 10
The Force at Compressed State = -10 N
The Force at Relaxed State = 0 N 
The simulation of Spring motion is as : 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 9 , 8 , 7 ,6 , 5 , 4 , 3 , 2 , 1 , 0  

Curriculum Connections

To solve this problem, students will:

  • Take input from a user
  • Store input in a variable
  • Understand the usage of Ternary Operator ( and if-else logic)
  • Output data to the user
  • Demonstrate an understanding of Forces and Laws of Motion

Solution in RUBY

# This block takes input from the user
puts("Please enter the Spring Constant of spring and press ENTER:")
k=gets.to_i

#count is the integer variable whose value is being incremented
count=0

# Display the Forces at Extreme Positions
puts("\nThe Force at Compressed State = "+(-1*k).to_s)
puts("\nThe Force at relaxed state = 0 N")

# L is the string that accumulates the whole data to be printed
L="The simulation of Spring motion is as :"

# The first loop is the loop used for incrementing the value of count from 0 10
k.times do
L+=count.to_s+" ,"
count+=1
end
# The second loop this is used to vary the value of counting from 10 to 0
k.times do
L+=count.to_s+" ,"
count-=1
end
L+="0"

#Place the output on the screen
puts(L)