Download presentation
Presentation is loading. Please wait.
1
A simple program: Area of a circle Problem statement: Given the radius of a circle, display its area Algorithm: 4. Display area To store values, we need to learn about variables 1. Store the radius value 2. Compute area using formula: Area = radius x radius x Pi 3. Store the Area value
2
Variables Variables store data…input, output or inBetweeny stuff Variables are like shaped boxes…. 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). 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)
3
Declaring variables……. ; 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 Int, double, float, char all begin with a lower case letter. String begins with a capital letter. String myName; // myName holds a string of characters
4
Identifiers Identifiers name things: variables, 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 Rules for identifiers (variable names, and names for methods, classes)
5
Identifiers…… Good or bad? area radius403 3rdRadius d+4 thisIsAVeryLongIdentifierIndeed i double ISTHISOK
6
Assigning to variables = ; x = 1; radius = 1.0; ch = 'a'; x = 1 + (2 * 3) + (3 * 4); = means put something into a variable.
7
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)
8
Program: ComputeArea Public class ComputeArea { public static void main(String[] args) { double radius; double area; // step 1: store radius radius = 1.23; // step 2: compute, store area area = radius * radius * 3.1416; // 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); } ‘Declare’ variables radius and area (boxes to hold radius and area values) Store radius value in the variable Compute area and store in area variable
9
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.
10
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
11
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; Variables must be declared before usage! myName = “Fintan”; String myName; // NOPE! String myName = “Fintan”;
12
Homework Read Liang Ch.2 pp. 25 – 32.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.