Assignment 5.1: Input Validation and Multiple Files

How to use multiple files and validate input

Goals

The goals of this assignment are to learn the following:

  • How to validate user input
  • How to create class methods
  • How to place source code in multiple files

Computer Science Principles Curriculum

  • Big Idea: Programming: EK 5.1.2B, EK 5.1.2E, EK 5.2.1F, EK 5.1.2J, EK 5.2.1C, EK 5.3.1D, EK 5.3.1I, EK 5.4.1

Common Core Standards

  • English Language Arts Standards ยป Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.6, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3
  • Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8

Overview

In this assignment you will be creating a program that mimics a radio. The user will be able to select from 1 of 4 stations and each station will play a different tune. The user will also be able to set how long the tune plays for.

The duration and station input from the user must be checked for correctness. These values will be verified in the Main class, and then passed to the Station class. The Station class will be responsible for playing the music after receiving the station and duration input. The Main class will be responsible for asking the user for these values. You will learn about how to use separate files, and how to utilize the classes you define in them. Create a new assignment and label it Assignment 5_1.

Class Main

Class Main should have the following actions:

  • action StationCheck(integer station) returns boolean: StationCheck should check to make sure the integer value passed in is between 1 and 4, inclusively. This means the value can be either 1, 2, 3, or 4. If the value passed in is valid, then this action should return true. If the value is not valid, it should output an error message and then return false.
  • action GetStation returns integer: GetStation should ask the user to input a station (1-4) and then return the integer they enter. It should continue to ask for a station until the user enters a valid station number.
  • action TimeCheck(integer time) returns boolean: TimeCheck will do the same things that StationCheck does: it should check if the integer passed in is between 1 and 20, inclusively. If it is, then it should return true. If not, output an error message and return false.
  • action GetTime() returns integer: GetTime should ask the user to input an integer between 1 and 20 inclusively, and then return that integer value. It should continue to ask the user for a value until a valid one is given.

Class Station

Class Station should have the following actions:

  • private action Play(integer time, integer note) Play should use the Play action from the Music library to play the note passed for the duration of time(the first integer passed in).
  • action Station(integer note, integer time): Station should use action Play from above using the parameters passed in to Station as the parameters needed for Play.

Using Separate Files

  • Make sure to include "package Music.Players" at the top of the Station.quorum file. package Music.Players
    package Music.Players
  • Include the following use statement: "use Music.Players.Station" at the top of the main.quorum file.
    use Music.Players.Station
  • The first statement is saving the class Station into the Music.Players library.
  • The second statement is responsible for making that specific class available to the main.quorum file. This way the user can call the class by simply typing the class name, then a variable name: 'Station radio'
    Station radio
  • Now any public methods of the Station class can be called by typing the variable of the Station class, in this case radio, like so: radio:(parameter(s))
    radio:(parameter(s))

Other Information

  • Before you begin, think about how you might change the note that's played based only on the user's input
  • If you use a repeat statement, and don't change the variable you're using for the note, it will play the same sound for as long as the user specified
  • Within the repeat structure you're able to change the variable, so consider how you might change it to make it a more complex sound, rather than playing one note over and over again
  • Another consideration is that you want to make unique sounds for each station selected, but the human ear has a limited range of pitches that it is able to hear. It might take some tinkering, but try to make it so that every station played produces an audible sound.
  • Reminder: when you want to speak a word in the Sodbeans environment, you can use the say statement. However, if you want to output a word to the Sodbeans output window, you must use an output statement. Be aware of these differences when completing this assignment.

Sample Output

Entering an invalid time

"Please select your station (1-4):"
"3"
"How long do you want to play? (1-20):"
"0"
"Incorrect input. Please try again."
"How long do you want to play? (1-20):""
"6"

Entering an invalid station

"Please select your station (1-4):"
"5"
"Incorrect input. Please try again."
"Please select your station (1-4):"
"4"
"How long do you want to play? (1-20):"
"10"

Entering valid information

"Please select your station (1-4):"
"2"
"How long do you want to play? (1-20):"
"19"

Next Tutorial

In the next tutorial, we will discuss Assignment 5.2, which describes an introduction to recursion..