0% Complete
Minds on

MINDS ON

Artificial Intelligence (AI) is a branch of computer science dealing with the simulation of intelligent behavior in computers, or the capability of a machine to imitate intelligent human behavior.

~ Merriam-Webster Dictionary

Artifical Intelligence (AI) is a topic that is being talked about a lot lately, because people feel that it really will change every aspect of our lives - from health care, to traffic lights, to teaching assistants. Isn’t that cool? Computer science is changing every aspect of our lives!

This is an image of a brain with electronic circuitry on top.

When you consider the programs you’ve written so far (programs that have calculated areas, volumes and taxes) you might ask yourself: Are these examples of AI?

The answer is: sort of.

While not everyone agrees on what exactly constitutes AI, in it’s simplest form, a machine performing calculations is AI. What everyone does agree on is that there are “levels” of AI, or an AI continuum, where some technologies are more “AI-ish” than others, or where some technologies include more sophisticated, complex and advanced AI systems.

Did You Know?

Did you know that the ghosts in the famous game Pac-Man all have their own “personalities?” Not only do they pursue Pac-Man throughout the game, but they were each programmed to chase him using different patterns of behaviour.

Toru Iwatani, Pac-Man creator, said that this "is the heart of the game. I wanted each ghostly enemy to have a specific character and its own particular movements, so they weren’t all just chasing after Pac Man in single file, which would have been tiresome and flat."

You can read more about the Pac-Man ghost personalities, and about other components of the game’s AI.

 

In this activity, you are going to add a bit more AI to your programs by learning concepts that will allow your programs to “make decisions.”

Machine Learning versus Artifical Intelligence

Machine learning is considered a little more complex than AI.

While AI includes a lot of behaviour that has been programmed by the programmer, machine learning involves the program eventually deciding upon it's own behaviour.

“Machine learning is an application of artificial intelligence (AI) that provides systems the ability to automatically learn and improve from experience without being explicitly programmed. Machine learning focuses on the development of computer programs that can access data and use it to learn for themselves.”

~ ExpertSystems.com

Learning After High School

Many leading computer science departments have researchers dedicated specifically to the area of AI:

MIT even offers a free “Introduction to AI” course that anyone can take. You don’t even have to be registered at MIT or at any other University. The course includes lectures, assignments, quizzes and exams. Here is an example of an assignment.

This is the discussion icon. AI All Around

Your task is to consider your experience with AI and to share with your friends and family.

Consider at least three of the following questions:

  • What is an example of AI that you encounter every day, and why do you believe that this could qualify as AI?
  • What have you seen on TV or in the movies that you believe to be AI? It might be science fiction movies or television, or perhaps in another genre. 
  • What have you read about online, or in a book, magazine or newspaper, that is related to AI?
  • Have you read, seen or heard anything about AI possibly being a danger to human beings? Who thinks this might be the case? How could AI be dangerous?

 
Action.

ACTION

Selection: Programs that Make Decisions

Take a look at the following program. Can you figure out

  • what the program does overall?
  • what each line of code does?

After considering for a few minutes, rollover the indicated spots to find out.

ifElse

Long Description

 

The program above uses what is called an “If… then.. else…” statement in order to make a decision. The decision is made based on the value of the variable age.

Line 19 will be executed if age is greater than or equal to 16.

Line 23 will be executed if age is not greater than or equal to 16.

In order to make this decision a comparison was made. The value of the variable age was compared to the number 16. This was done using what’s called a comparison operator.

Comparison Operators

Comparison Operators allow the program to compare two or more pieces of information. The following are the main comparison operators used in computer programming:

Java Comparison Operator
What is means…
>
Greater than
<
Less than
==
Equal to
>=
Greater than or equal to
<=
Less than or equal to
!=
Not equal to

Reading Selection Code

Before you start to write code that includes selection, let’s make sure you can read through some programs that include it, and see if you can make predictions about what might happen.

trueOrFalse

Long Description

 

else if Statements

In the driver’s license example above, there was really only one decision, and therefore only two potential actions, involved in the program.

The person’s age was either >= 16, or it wasn’t. That’s why the program used an “if…. then… else…” statement.

In some programs, however, you might want to have more than one decision and more than one action involved. If this is the case, then it’s smart to use what is called an “if… then… else if…” statement.

Take a look at the program below. Can you figure out the purpose of each line of code?

This is an image of source code that prompts the user for their mark as a double value and then determines the letter grade associated with this value by implementing if else statements.

 

In the marks program above, there is only one selection statement. Although the statement contains several else ifs, there is still only one of these that will be true. This means that the programmer has written five possible output statements, but only one will be executed.

Also, it’s important to note that as soon as one of the statements evaluates to true, the program then jumps to the end of the overall selection statement (line 35). Therefore, if more than one of these conditions evaluates to true, it will only execute the first one that evaluates to true, and then skip the rest.

The order of the statements is also very important. If the programmer had started with if (mark >= 50)

        {
            System.out.println("You received a D.");       
        }

Then even a mark of 87.4% would have resulted in this being true and the user would receive the message “You received a D.”:

This is an image of a programming code that has the if selection statements ordered in such a way that the program will not generate the correct output.

This is the dropbox icon. First Selection Program

A store is offering a certain percentage off all purchases, depending on the amount spent:

  • A customer spending $0.01-$40.00 will receive 10% off.
  • A customer spending $40.01 - $80.00 will receive 20% off.
  • A customer spending $80.01 - $120.00 will receive 30% off.
  • A customer spending over $120 will receive 40% off.

Create a program that asks the user to enter the amount that was spent, and then output

  • the percentage of savings.
  • the amount of savings in dollars and cents.
  • the resulting price.

You can create this program simply using the console for input and output.

The following are screenshots showing the output based on a variety of amounts spent:

This is an image showing the output from four runs of the program.

Your program should include the following:

  • Appropriate variable names and types
  • Commenting and appropriate spacing
  • Constants where appropriate
  • Values that are rounded and formatted to two decimal places when appropriate

Note: For now you can output $11.80 as $11.8. Later you will learn how to force Java to output this number to 2 decimal places.

 

Boolean Logical Operators

Boolean operators are used when programming slightly more complicated decisions that involve more than one thing being true or false at a time.

The two main Boolean logical operators that you will use are OR and AND.

Let’s take a look at some examples:

Imagine that in order to drive a person had to be at least 16 years of age and own a dog.  This would be a ridiculous idea, but let’s go with it…

You could write a Java if statement that uses the AND Boolean logical operator. It would resemble the following:

if ((age >=16) && (ownsDog == true))

   {
      System.out.println (“You can obtain your license!”);
   }

Notice that in the above code, age >= 16 AND ownsDog == true both have to be true in order for the statement to be output.

Imagine that in order to pass this course you could either obtain a mark of at least 50% or buy your teacher an awesome birthday present.  (Note that this would be highly unethical on both your parts, by the way.)

You could write a Java if statement that uses the OR Boolean logical operator. It would resemble the following:

if (mark >= 50) || (bdayPresent == true))

   {
     System.out.println (“Yes, you pass!”);
   }

Notice that in the above code, only one of marks >= 50 OR bdayPresent == true has to be true in order for the statement to be output to the screen.

Here is a slightly more complex (but more realistic) example that highlights how Boolean logical operators can be used:

In basketball, it is said that a player has obtained a “triple double” if he or she has achieved double digits in three statistical categories: points, assists and rebounds (it is possible to obtain a triple double by obtaining double digits in another category, like steals or blocks, but this is very rare).

A player can obtain a double-double by achieving double digits in two statistical categories.

The program below prompts the user for the points, rebounds and assists obtained in a game. It then uses Boolean and comparison operators to generate an accurate output statement based on the statistics.

Take a few minutes to read through the code. Pay close attention to the logic involved, and notice how Boolean operators, comparison operators, and brackets were used to ensure that the program made the correct decisions.

This is an image of a program that prompts users to enter their statistics from a basketball game to determine whether or not they achieved a triple double.

 

Flowcharts

As your programs begin to get more and more complex, it’s important for you to spend time planning and organizing programs. If you don’t do this, then programming will take a lot longer, it will be a lot more difficult, and you may end up with very disorganized programs. It will also be very hard to explain your ideas and programs to other people.

One way programmers design and organize programs is by creating a flowchart.

A flowchart is a visual representation of the sequence of steps and decisions needed to perform a process. Each step in the sequence is noted within a diagram shape. Steps are linked by connecting lines and directional arrows. This allows anyone to view the flowchart and logically follow the process from beginning to end…. With proper design and construction, it communicates the steps in a process very effectively and efficiently.

~ Smartdraw

This is a flowchart indicating the decisions to be made in the driver's license program.

Notice the different shapes in the above flowchart. These are important. Each shape represents a different component of the program.

Now let’s look at the marks program again and the flowchart following it:

This is an image of the code for the marks program.
This is a handdrawn image of the flowchart for a marks program.

Creating Flowcharts

For this course, you have a number of options for how you would like to create your flowcharts. You can draw them by hand, take a picture, and then submit them that way. Or, you can use digital tools to create your flowchart. Many word processing software packages allow you to create flowcharts quite easily by inserting the symbols to a document. You can also try flowchart creation software that is available online, such as:

Consolidation

CONSOLIDATION

This is the dropbox icon. Making a Pizza!

Putrid Pizza is a pizzeria that only makes one type of pizza with the following toppings: extra, extra anchovies and extra, extra olives.

Customers aren’t allowed to order any other type of pizza, but they are allowed to select the size (by indicating the diameter).

Your task is to prompt the user for the diameter of pizza he or she would like to purchase, and then calculate the subtotal, the taxes, and the grand total.

Your program should also output a message based on the following:

  • If the diameter of the pizza is from 1cm to 15 centimeters, then output a message that reads: “We are going to make you a cute little pizza!”
  • If the diameter of the pizza is greater than 20 cm but less than 40 cm, then output a message that reads: “This will be delicious!”
  • If the diameter of the pizza is greater than 40 cm, then output a message that reads: “Whoa, big pizza! You might need a truck to get this home!”

The following lists the charges for a pizza at Putrid Pizza:

  • $0.75 per pizza (for labour)
  • $0.99 per pizza for rent (for the pizza shop)
  • $0.50 per diameter centimeter of pizza (for ingredients)

You will submit your completed program, as well as a flowchart that outlines the start, end, processes, input/output and decisions involved in the program.

Your program should include

  • a well-designed GUI that uses a form, textboxes, labels and a button.
  • appropriately named components.
  • commenting and appropriate spacing.
  • values that are rounded and formatted to two decimal places.
  • constants for all variables that will not change as the program is run.

The following is a screen capture of a sample GUI created for this program:

This is an image of the Putrid Pizza program form with a text box to enter the diameter of the pizza.

 
test text.