class PhysicsVector def initialize(name,x,y,z) @name = name @x = x.to_f @y = y.to_f @z = z.to_f end def x x = @x end def y y = @y end def z z = @z end def add(n,a) return PhysicsVector.new(n,@x + a.x,@y + a.y,@z + a.z) end def subtract(n,a) return PhysicsVector.new(n,@x - a.x,@y - a.y,@z - a.z) end def print() puts @name + " = " + @x.to_s + "i + " + @y.to_s + "j + " + @z.to_s + "k" end def magnitude() return Math.sqrt((@x * @x) + (@y * @y) + (@z * @z)) end def print_magnitude() puts "magnitude of " + @name + " = " + self.magnitude().to_s end def direction_r() return Math.atan2(@y, @x) end def direction_d() return Math.atan2(@y, @x) * 90.0 / Math.asin(1.0) end def print_direction_r() puts "direction of " + @name + " in radians = " + self.direction_r().to_s end def print_direction_d() puts "direction of " + @name + " in degrees = " + self.direction_d().to_s end end # define maim function def main() puts "Ruby output" puts "Chapter 2" puts "Problem 5" a = PhysicsVector.new("a",4.0,-3.0,0.0) b = PhysicsVector.new("b",6.0,8.0,0.0) c = a.add("c",b) d = b.subtract("d",a) e = a.subtract("e",b) a.print() b.print() c.print() d.print() e.print() a.print_magnitude() b.print_magnitude() c.print_magnitude() d.print_magnitude() e.print_magnitude() a.print_direction_r() b.print_direction_r() c.print_direction_r() d.print_direction_r() e.print_direction_r() a.print_direction_d() b.print_direction_d() c.print_direction_d() d.print_direction_d() e.print_direction_d() end main()