Download File

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Programming using the Finch
Introduction to Programming Using the Finch
The finch is a robot that allows users to create programs to control its basic functionality.
There are multiple programming interfaces that allow you a variety of languages to control
its functions.
These notes are going to use Java and the Eclipse environment.
Set Up
Java SDK
First of all you will need Java. For home use this can be downloaded from Oracle’s site:
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads1880260.html
Download the 32 bit Windows version.
Eclipse
We need to do this in class. Here you will need the Eclipse IDE (Integrated Development
Environment). Download it from:
https://eclipse.org/downloads/packages/eclipse-ide-java-developers/lunasr1a
Download the 32 bit Windows version.
Open Eclipse (double click Eclipse.exe). Then File-> New-> Java Project. Give the Project a
name such as MyFirstProject {note – no spaces and in Pascal case}. Use the default location
unless you want to save the project somewhere else and press Finish.
The Package Explorer window should be on the left with the name of your project. Double
click your project icon to reveal an icon named src. Right click this and pick New -> Class. A
Java Class window will appear and the Source Folder will show your current Project. In the
Name dialog box enter the name of your class, say, MyFirstClass. Under ‘Which method
stubs would you like to create?’ select the public static void main(String[] args). Keep the
other box ticked.
West College Scotland
1
Introduction to Programming using the Finch
You will then have a template for your Java code created called MyFirstClass.java. This will
contain the following code:public class MyFirstClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Change this so the braces are lined up, and remove the ‘param args’ comments and the line
comments inside the main program:public class MyFirstClass
{
public static void main(String[] args)
{
}
}
Voila, you now have the skeletal code where you can now enter your code:
public class MyFirstClass
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
Write the above program and run it using the little green arrow on the menu – the result
will appear on the console manu at the bottom of the screen. Before you see the result of
running the program you will have to save it with the exact same name as the class followed
by .java
The word public means that code we write elsewhere in our project can see it. If we didn’t
want this visibility we would make it private instead.
When you create a new class within a Java project it adds a main method
public static void main(String[] args)
West College Scotland
2
Introduction to Programming using the Finch
which will attempt to run all the code between the curly brackets {} (also called braces)
Note: all braces must have an open and closing brace. If not you will get an error. You will
get this error a lot to start with.
In the example above the class has curly brackets to show where it starts and ends, and the
method has the same.
Within our main method we only have one line which will run
System.out.println("Hello World");
This lines simply puts the text (called a string in most programming languages) “Hello
World” on your screen.
Java Introduction
As stated earlier the finch can be programmed with a huge amount of languages, but these
notes are going to focus on Java.
Java can be written in a variety of tools, from a simple text editor to a full blown
development environment such as Eclipse. There are guides on how to setup your chosen
environment on the finch web page.
Java is case sensitive so the commands move() and Move() are different, as they don’t have
identical case (one starts with a lowercase and the other an uppercase M).
You will make lots of mistakes in using the incorrect case. If your program says there is an
error check case first!
A basic java program must have a class and a main method that kicks off your program.
Note: a class is just a thing that represents something we want to create. So we could
create a class called Human. A method is something that acts upon the class or returns
information about it. So we could have a method called getAge(); in our Human class.
Classes should be named with an initial capital.
At first the code can look complicated, but because it’s the same code other than name
changes in every program then it soon becomes familiar.
Task 1 - Create a program that displays a ticket something like
++++++++++
++ Tesco Receipt ++
++++++++++
West College Scotland
3
Introduction to Programming using the Finch
To do this first create a new class inside your project named Tesco
Task 2 - Modify the above ticket to display more information such as the product and price.
Task 3 - Create a program (again create a new class named House) to display as a house like this. Be
creative (windows, door etc.)!
Note: to display the “\” character in Java you must do two backslashes such as \\. This is because
Java uses this to display special characters. More on this soon.
__
______| |____
/
\
|
|
|
_
|
|
| |
|
|____| |_____ |
Add the following code under the display of the house –
System.out.println("\n\nHooray!\nMy Super\nHouse\n");
Describe what it does.
Primitives
Primitives in Java are basic data types that allow us to store values. Java has a number of
these
Type
Meaning
Storage
Min Value
Max Value
int
Integer
32 bits
-2,147,483,648 2,147,483,647
short
Short int
16 bits
-32,767
32,767
float
Floating point
number
32 bits
-3.4E+38
3.4E+38
double
Large floating
point number
64 bits
-1.7E+308
1.7E+308
byte
Byte
8 bits
-128
127
boolean
Boolean
true
False
Numeric Values
Alphanumeric
char
Character
String
String
West College Scotland
16 bits
4
Introduction to Programming using the Finch
It’s not important to remember all of these, but we will be using the String, boolean and int
types quite a lot.
Variables are temporary stores that allow us to store and retrieve values and must be
‘declared’ to be of a specific type, generally at the top of the program (below the main title).
The programmer names the variables (camel case) with meaningful names.
e.g.
int numberOfElephantsInBorneoJungle;
we can then assign a value to this variable of the same type in which it has been declared
e.g.
numberOfElephantsInBorneoJungle = 786;
If you declare a variable to be of type ‘int’, say, you cannot assign it to a String type.
For instance, say we have a little program that assigns values to three variables, age, name,
and isAgeOlderThanForty. The age will be a whole number so it will be ‘declared’ as an ‘int’
type, the name will be declared as a String type, and the age status as a boolean type.
Try creating the following class BasicVariables and inserting the code:
public class BasicVariables
{
public static void main(String[] args)
{
// declaration area
int age;
String name;
boolean isAgeOlderThanForty;
age = 62;
name = "William Butler Yeats";
isAgeOlderThanForty = true;
System.out.println(name + " is " + age + " years old");
System.out.print("and it is " + isAgeOlderThanForty + " that " + name);
System.out.println("is older than forty");
}
}
All variables that are printed out must have a value. To comment code to help the
programmer understand the program, use // before the comment. This code will not be
compiled.
West College Scotland
5
Introduction to Programming using the Finch
We can also assign values to variables when we declare them. This is known as initialisation.
Comment out the code you have already written in the class BasicVariables (note: to
comment out a block of code you use /* at the beginning of the block and */ at the end.
Now type the following and save and run it.
public class BasicVariables
{
public static void main(String[] args)
{
int age = 62;
String name = "William Butler Yeats";
Boolean isAgeOlderThanForty = true;
System.out.println(name + " is " + age + " years old");
System.out.print("And it is " + isAgeOlderThanForty + " that " + name);
System.out.println(" is older than forty");
}
}
User Input
So far we have assigned values to variables in the program. This means that the program will
always show the same results. However we could also ask the user to input the values at run
time and this makes our code interactive and much more powerful.
In Java there are a number of ways to obtain input from the user. We are going to learn a
little about the Scanner class. The Scanner class, like the Finch class, must be imported so
we must add the line at the top of our code.
import java.util.Scanner;
We must actually create an instance of the scanner class.
Scanner scan = new Scanner(System.in)
The Scanner class has many methods.
e.g.
scan.nextLine() would take all the text you have typed up until you hit return
scan.nextInt() would get the next whole number
West College Scotland
6
Introduction to Programming using the Finch
Before we go any further let’s look at how we can store certain values input by the user. e.g.
say we wanted Java to ask and store someone’s name and age. We could do the following:
First create a new class named UserInput.
Now on the top line of the code window insert the code to import the Scanner class
Scanner scan = new Scanner(System.in);
String age;
String name;
System.out.println("Please insert your name: “);
name = scan.next();
System.out.println("Please insert your age: “);
age = scan.next();
System.out.println("Your name is: “ + name + ” and your age is “ + age);
This would produce the output
Please insert your name:
Alan
Please insert your age:
23
Your name is: Alan and your age is 23
We don’t just use variables for storing input. There are plenty of times when we wish to
stores values other than just for input.
West College Scotland
7
Introduction to Programming using the Finch
Say we wanted to write a program to calculate the area of a circle. The formula is
A=
π × r2
Where A = area and r = radius. The π is pi which is a constant value of 3.1415926535…
Rather than ask this value from the user each time it runs we can get the program to store
it.
So we could write
double pi = 3.1415926535;
We don’t use the type’ int’ here, because it is not a whole number.
For the full program we could write:
import java.util.Scanner;
public class AreaOfCircle
{
public static void main(String[] args)
{
double pi = 3.1415926535;
double radius, area;
Scanner scan = new Scanner(System.in);
System.out.println("Please Input the radius: ");
radius = scan.nextDouble();
area = pi * radius * radius;
System.out.println("The area of the circle is: " + area);
}
}
Create the above program and test to make sure it is working.
Task 4 – create a program similar to above that converts kilometres into miles. The formula
is (Kilometres /8)*5;
Task 5 - create another conversion program that converts something of your choice.
Task 6 create a program with a class called FootballGroundArea that asks the user for the
football ground they are calculating, the length and the width of the ground and then
responds with a statement saying – The ??? football ground has an area of ?? yards.
West College Scotland
8