A simple program: Radius - University College Dublin

Download Report

Transcript A simple program: Radius - University College Dublin

Methods: a first introduction
Two ways to make tea:
1: boil water;
put teabag into cup;
... etc......
2: tell your younger brother:
makeTea(1 milk, 0 sugar);
A method is a short way of calling a bunch of other statements
Built-in classes and methods
Printing things on the screen is actually quite complicated
(we’d need to know how to draw the characters, where to
put them, how to move to a new line, and so on.)
Java has built-in System.out classes that other
programmers have written to handle all these complications.
These classes contain methods like println or print
that we can call to execute the series of statements needed
to print something on the screen.
There are a lot of such built-in classes and methods in Java
that do all sorts of useful things. We will learn many of them
More on methods
We need to know 1) the method we want to execute; 2) the
class that can carry out that method; 3) the arguments the
method takes.
System.out.println("Hi there");
Method:
println
Method: makeTea
Carried out by: System.out Carried out by: brother
Arguments:
"Hi there" Arguments: 1 milk,0
sugar
Programs and classes
Each class describes a thing that does certain actions. Large
programs can contain multiple classes describing different things
Smallest possible Java program:
1 class containing
1 (main) method & 0 statements
public class Foo
{
public static void main(String[] args)
{
// your code in here
}
}
This would be placed in a file called Foo.java. A java file
always has the exact same name as the class containing main.
How do we develop programs?
understand the problem
design an algorithm (semi-formal)
implement the algorithm (write Java)
compile and test
Debug (fix mistakes)
Edit... Compile... Debug cycle
Write/edit Java code
Compile and run
Debug
(find the mistakes)
A simple program: Area of a circle
Problem statement:
Given the radius of a circle, display its area
Algorithm:
1. Store the radius value
2. Compute area using formula:
Area = radius x radius x Pi
3.
Store the Area value
4. Display area
To store values, we need to learn about variables
Variables
Variables store data…input, output or inBetweeny stuff
Variables are like shaped boxes….
Integers go in integer-shaped variables (labelled int)
Real numbers go in real-number shaped ones (float, double)
Strings (“hello”) can go in string-shaped variables (String)
Variables must be set up ("declared") before use
Each variable is of a certain datatype (int, float, double,
String) and each variable has a name (an identifier).
Declaring variables…….
<datatype> <variable name>;
int x;
// a box called x that will hold an integer
double radius;
/* a box called radius that will hold
a floating point no.
*/
char ch;
// ch can hold a single character
String myName;
// myName holds a string of characters
Int, double, float, char all begin with a
lower case letter. String begins with a capital letter.
Identifiers
Identifiers name things: variables, methods, classes…..
Rules for identifiers (variable names, and names for methods, classes)
Must start with a letter (or _ or $)
Cannot contain an operator, e.g. +, Cannot be a reserved word, e.g. public, static, double, int
Can be as long as you like………
Should be descriptive
Identifiers……
Good or bad?
i
area
double
radius403
ISTHISOK
3rdRadius
d+4
thisIsAVeryLongIdentifierIndeed
Assigning to variables
<variable> = <value>;
<variable> = <expression>;
x = 1;
radius = 1.0;
ch = 'a';
x = 1 + (2 * 3) + (3 * 4);
= means put something into a variable.
Program: ComputeArea
Public class ComputeArea {
public static void main(String[] args) {
double radius;
double area;
// step 1: store radius
// step 2: compute and store area
// step 3: display the area
}
}
(same as the
Algorithm we
saw earlier)
Program: ComputeArea
Public class ComputeArea {
public static void main(String[] args) {
double
double
// step 1:
radius
radius;
area;
store radius
= 1.23;
‘Declare’ variables radius
and area (boxes to hold
radius and area values)
Store radius value in the
variable
// step 2: compute, store area
Compute area and store
area = radius * radius * 3.1416;
in area variable
// step 3: display the area
System.out.print(“A circle of radius ”);
System.out.print(radius);
System.out.print(“ has an area of ”);
System.out.println(area);
}
}
Things to note about ComputeArea
3.1416 is the value of pi. We could have a variable called
pi if we wanted, and put that value in it. What type?
There is a sequence of print statements, ending with a
println. This means everything will be printed on a
single line.
We can print strings (in quotes) and variables (radius
and area). When we print a variable, its value comes out.
We have to type the value of radius into the program.
It’d be better to read it in: we’ll see how to do that later.
More assignment
Datatype on LHS (Left Hand Side) must be compatible with
datatype on RHS (Right Hand Side):
int x;
x = 3; // ok
x = 3.1; // not ok, as we said x was of type int
LHS: must be a variable
1 = x; // this is meaningless
RHS: expression can include variables…
x = x + 1;
// ok if x already has some legal value
Shortcut: declare and initialize
Initialization: giving a variable a value for the first time
We can declare and initialize in the same line (good practice!)
int x = 20;
double d = 3.14;
String myName = “Fintan”;
Variables must be declared before usage!
myName = “Fintan”;
String myName;
// NOPE!
Homework
Read Liang Ch.2 pp. 25 – 32.
In your labs this week you will be
doing printing and some variable
assignment.