View
224
Download
0
Embed Size (px)
Citation preview
Where are we?
Programming in Java consists of creating source code in an editor.
Source code is compiled to bytecode by the Java compiler – javac
The bytecode is run by the run-time interpreter – java – or at time in an applet with a wrapper consisting of .html code
Source Code
Must be prepared according to the strict standards of the language and the compiler.
Grouped in files, each file contains a public class with a given name.
Name of the public class must match the part of the filename before the .java exactly, including upper/lower case
Class
Consists of class variables declaration and methods grouped between a starting { and ending }.
Method will have a header followed by a sequence of declarations and statements enclosed in braces.
Each declaration and statement ends with a semi-colon;
Sample Layout
public class FirstProgram
{
public static void main(String[] args)
{
…declarations and statements
}
}
Declarations
Type variable;
Type variable=value;
Type can be one of the built in types (byte,short,int,long,float,double,char,boolean) or a class such as String, Random.
Base Variables
For the base types space is allocated and the variable stores the value of the variable. E.g., int x=9. x ‘stands for’ a location which contains the value 9.
Value can be changed by statements. Variables can be designated as constants with
the keyword “final”. In this case they must be given a value in the declaration.
Constants
final int MAX_ROW=10;
Constants cannot be modified by statements.
Class Variables
String title; Title does not allocate space for a String,
instead it is a class reference. That is, title will be used to store the address of a string.
Objects are created using the new operator and various methods including constructors.
Creating Objects
String title, extendedTitle;
title = new String(“This is my title”);
extendedTitle=title.concat(“ Live with it.”);
Statements
Assignment statements Invoking class methods
System.out.println(title);
More complex statements (later);
Invoking Methods
We can use classes that our program “knows about” either because it is always included (such as String or System.out) as part of the java sdk, or because we have specifically imported it and made it available:
import cs1.Keyboard;
Class Methods
Some classes require an object, and the methods are applied to the object:
String title=new String(“my title”);
int lengthOfMyTitle;
lengthOfMyTitle=title.length();
Static Methods
Other classes consist only of static methods and don’t require an object. Static methods can be invoked using the class name:
double radius=5.0;
double area;
area = Math.PI * radius * radius;
main
Our simple programs consist of a single static method main. Since it is static, it can be invoked without creating a class object.
Since it is named main it will automatically be invoked when the program is run
Since it’s reserved, the format must be followed exactly
public static void main(String[] args)
Main
Actually, the variable args can have any name, the other parts are fixed though.
We are free to create other methods in our class and use them. For the class containing main, these will usually be static methods (since there won’t be an object declared) and can be private or public.
Simple Statements.
Assignments: x = 17; Expressions: x = a + b; Class methods:
System.out.println(“Hello”);
String title=“Hello”;
String moreTitle = title.concat(“ World”);
Complex Statements
if (condition) statement; if (condition)
statement;
else
statement; while(condition)
statement;
Conditions
Expression that results in a boolean value (true or false).
Boolean operators ==, <, >,<=,>=,!
if (a < b)
System.out.println(“a is smaller”);
else
System.out.println(“b is smaller”);
Conditions - more
For comparing objects, we usually cannot use the operators. The class defining the object will usually supply the necessary methods for comparisons
if (title.equals(extendedTitle))
…
How do we find out about classes?
Important ones like string are discussed in the text
Many are included in the SDK, and we find out about them from the documentation with the SDK or from textbooks.
We write our own, and provide the documentation as part of the process.
Using classes
Use the wheel or invent the wheel?• If you are using an existing class, you should
take the time to at least skim-read the documentation. Often you will find that a method exists to do what you want:
if(title.compareToIgnoreCase(extendedTitle) < 0)
…
Flow of execution
Statements are executed sequentially if nothing is done to alter the flow.
Some statements alter the flow. When a method is invoked, flow actually
transfers to the code contained in the method, but conceptually we ignore this and think of it happening immediately.
if and it’s variations
If statements as well as if/else and switch statements in effect cause forward jumps to execute or skip code based on a condition or the value of an expression.
Loops
Loops cause blocks of code to be repeated a number of times based on an expression that is evaluated at the beginning or end of each iteration
while(condition) { …}
do { …} while;
for
for loops exist because many loops follow the format:
Initialization statements
while(condition)
{
….
statements to update the condition variable(s)
}
for(…;…;…)
for(initialization;condition;increment)
for(int i=0; i < 100; i++)
System.out.println(i);
Programs
One or more .java files, compiled to .class files
Begins at main May write other methods in the class
containing main, but they must be static, since no object is created.
May use other classes and methods.
Applets
No main method Run in context of web browser (or
appletviewer) Begin by ‘overriding’ Applet methods,
typically paint.
What to review
Self-test exercises – answers in book. Exercises from chap 1, 2, 3 Programs you have done or done in
class.
What to expect
Short answer such as the exercises. Multiple choice/true false/or short answer
• Terms
• Programming
What’s wrong with this program? At least one short complete program. Other program segments.