5.2T.pdf - Swinburne University of Technology 2020-HS1-COS10009-Introduction to Programming Doubtfire Submission Track File Handling Submitted By Mukil

5.2T.pdf - Swinburne University of Technology...

This preview shows page 1 out of 4 pages.

Unformatted text preview: Swinburne University of Technology 2020-HS1-COS10009-Introduction to Programming Doubtfire Submission Track File Handling Submitted By: Mukil Chittybabu 103060832 2020/04/16 17:35 Tutor: Muneeb Ul Hassan April 16, 2020 Produced by Doubtfire File 1 of 2 1 2 Track File Handling class Track attr_accessor :name, :location 3 4 5 6 7 8 end def initialize (name, location) @name = name @location = location end 9 10 11 12 13 14 15 16 17 18 19 20 21 22 # Returns an array of tracks read from the given file def read_tracks(music_file) count = music_file.gets.to_i() tracks = Array.new() i = 0 while i <= count track = read_track(music_file) tracks << track # Put a while loop here which increments an index to read the tracks i += 1 end tracks end 23 24 25 26 27 28 29 30 31 # reads in a single track from the given file. def read_track(a_file) title = a_file.gets location = a_file.gets Track.new(title, location) # complete this function # you need to create a Track here - see 5.1 T, Music Record for this too. end 32 33 34 35 36 37 38 39 40 41 # Takes an array of tracks and prints them to the terminal def print_tracks(tracks) index = 0 time = tracks.length while index < time puts tracks[index].name puts tracks[index].location index += 1 42 43 44 45 46 end # Use a while loop with a control variable index # to print each track. Use tracks.length to determine how # many times to loop. 47 48 49 # Print each track use: tracks[index] to get each track record end 50 51 52 53 # Open the file and read in the tracks then print them def main() a_file = File.new("input.txt", "r") # open for reading Page 1 of 3 File 1 of 2 54 55 56 57 Track File Handling tracks = read_tracks(a_file) # Print all the tracks print_tracks(tracks) end 58 59 main() Page 2 of 3 File 2 of 2 Screen shot Page 3 of 3 ...
View Full Document

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture