Friday, September 6, 2013

JAVA Case Study #2: Divide Items Equally

CASE STUDY #2: Divide Items Equally

Next let's look at this strange code for a program that will divide the total number of gumballs equally among a varied number of children:
import java.util.Scanner;

class EquallyDivide
{

    public static void main(String args[]) {
        Scanner myScanner = new Scanner(System.in);
        int gumballs;
        int kids;
        int gumballsPerKid;

        System.out.print("How many gumballs? ");

        gumballs = myScanner.nextInt();
        System.out.print("How many kids? ");

        kids = myScanner.nextInt();
        gumballsPerKid = gumballs / kids;

        System.out.print("Each kid gets ");
        System.out.print(gumballsPerKid);
        System.out.println(" gumballs.");
    }
}
 
So let's work with this code:
  • Add some comments / labels.
  • What is the difference between:
    •  System.out.println
    •  System.out.print
  • With variable names like "gumballs" or "kids" or "gumballsPerKid", this program is pretty specific.  What sorts of names could we use for variables to make this program usable for other purposes?  The "gumballs" variable might be more useful as "items", for example.  Remember, however, that you can change all instances of "gumballs" but "gumballsPerKid" is a completely different variable name.
  • What code could we add to figure out how many gumballs are left over after they have been divided equally?

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.

Wednesday, September 4, 2013

About Programming

Why Is Computer Programming Important:


The Early Days of Programming

During the early 1980s, Chris worked for a computer software firm. The firm wrote code for word processing machines. (At the time, if you wanted to compose documents without a typewriter, you bought a “computer” that did nothing but word processing.) Chris complained about being asked to write the same old code over and over again. “First, I write a search-and-replace program. Then I write a spell checker. Then I write another search-and-replace program. Then, a different kind of spell checker. And then, a better search-and-replace.”

How did Chris manage to stay interested in his work? And how did Chris’s employer manage to stay in business? Every few months, Chris had to reinvent the wheel. Toss out the old search-and-replace program, and write a new program from scratch. That’s inefficient. What’s worse, it’s boring.

For years, computer professionals were seeking the Holy Grail — a way to write software so that it’s easy to reuse. Don’t write and rewrite your search-and replace code. Just break the task into tiny pieces. One piece searches for a single character, another piece looks for blank spaces, a third piece substitutes one letter for another. When you have all the pieces, just assemble these pieces to form a search-and-replace program. Later on, when you think of a new feature for your word processing software, you reassemble the pieces in a slightly different way. It’s sensible, it’s cost efficient, and it’s much more fun.

The late 1980s saw several advances in software development, and by the early 1990s, many large programming projects were being written from prefab components. Java came along in 1995, so it was natural for the language’s founders to create a library of reusable code. The library included about 250 programs, including code for dealing with disk files, code for creating windows, and code for passing information over the Internet. Since 1995, this library has grown to include more than 3,000 programs. This library is called the API — the Application Programming Interface.

Every Java program, even the simplest one, calls on code in the Java API.  This Java API is both useful and formidable. It’s useful because of all the things you can do with the API’s programs. It’s formidable because the API is so extensive. No one memorizes all the features made available by the Java API. Programmers remember the features that they use often, and look up the features that they need in a pinch. They look up these features in an online document called the API Specification (known affectionately to most Java programmers as the API documentation, or the Javadocs).

The API documentation describes the thousands of features in the Java API.  As a Java programmer, you consult this API documentation on a daily basis. You can bookmark the documentation at the Sun Microsystems Web site and revisit the site whenever you need to look up something. But in the long run (and in the not-so-long run), you can save time by downloading your own copy of the API docs.

Flow Charts

The first example is lengthy, but the first 6+ minutes are the part I want you to focus on -- unless of course you want to learn C++ programming.  The professor does a great job of demonstrating a basic problem that we need to solve, lays it out in steps, and then applies to steps to a flow chart.





This is a flow chart of making decisions in your life:
This is a useful tool for figuring out what to do:

Here's a funny flow chart in action:  http://www.youtube.com/watch?v=k0xgjUhEG3U
And lastly... this little gem. :)

Tuesday, September 3, 2013

AP Computer Science

CollegeBoard's Course Description for AP Computer Science A

The AP Computer Science A course is an introductory course in computer science. Because the design and implementation of computer programs to solve problems involve skills that are fundamental to the study of computer science, a large part of the course is built around the development of computer programs that correctly solve a given problem. These programs should be understandable, adaptable, and, when appropriate, reusable. At the same time, the design and implementation of computer programs is used as a context for introducing other important aspects of computer science, including the development and analysis of algorithms, the development and use of fundamental data structures, the study of standard algorithms and typical applications, and the use of logic and formal methods. In addition, the responsible use of these systems is an integral part of the course.

Because this is a brand new course, we don't have a full year outline yet.  However, you should be receiving a syllabus and general (tentative) outline sometime this week.
Welcome to Week One of "AP Computer Science"! Although this course is called "Introductory Computer Science", it is actually fairly complex. We will be building up to the core of an AP Computer Science course.
Your homework is to install the JAVA software [on your home computer] as follows:
  1. Install JDK on your home computer and/or laptop (wherever you plan to work on Java programming)
  2. Install the Java API (Docs)
  3. Install BlueJ on your computer/device.
  4. Install GreenFoot on your computer/device.
And now for a brief intro about the importance of updating your Java.
And demonstrating some of the JAVA epicness... hardcore.