Thursday, September 5, 2013

Looking at Source Code & Flow Charts

Today we will be playing with some existing source code.  We will discuss what the programs do, how they are outlined, and what we can do to improve them.  Then we will attempt to expand on the programs

Below is a snippet of code that we will be entering in:

public class HelloWorld
{
   public static void main(String[] args)
   {
      System.out.println("Hello World!");
   }
}
 

Programming is about solving problems.  The entire purpose of writing a program is to solve a problem which, in general, consists of multiple steps:
  1. Understanding the problem.
  2. Breaking the problem into manageable pieces.
  3. Designing a solution.
  4. Considering alternatives to the solution and refining the solution.
  5. Implementing the solution.
  6. Testing the solution and fixing any problems.
After we understand a given problem, we can break the problem into manageable pieces and design a solution.  For example, if we wanted a program to figure out the least possible number of coins to give a customer as change, we might want to first subtract the amount owed from the amount paid, then figure out how many quarters would go into that, take the remainder and figure out how many dimes would go into that, and then move on to nickels and then pennies.  For more complex problems, making a flow chart can help to organize your thoughts and identify potential issues before you begin programming.

Below are some potential "problems" for you to solve using flow charts.

Flow Chart Practice: 
  1. A movie theater wants a program that will show admission price based on age.  They want to charge children (under 12) and seniors (65 and older) $5.25.  Everybody else will pay $9.25.  They want to ask "How old are you?", let the person put in their age (in years), tell them the price, then tell them to "Enjoy the show!".
  2. A daycare provider wants to give gumballs to their kids, but they want a program that will ask the worker, "How many gumballs?" and then "How many kids?".  Then it should divide the gumballs per kid (gumballs/kids).  Finally it should say, "Each kid gets X gumballs."
  3. You decide to write an app for a SmartPhone.  You want to do a Magic 8 Ball app where you ask a question and it gives you a "Yes" or "No" answer.
  4. You want to write a program that will pick a random number between 1 and 1000.  Ask the player to guess a number and tell them if they are too high, too low, or if they get the number correct.  The program should keep running until they guess the number correctly.

No comments:

Post a Comment