Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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;
4
Sample Layout public class FirstProgram { public static void main(String[] args) { …declarations and statements }
5
Declarations Type variable; Type variable=value; Type can be one of the built in types (byte,short,int,long,float,double,char,bool ean) or a class such as String, Random.
6
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.
7
Constants final int MAX_ROW=10; Constants cannot be modified by statements.
8
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.
9
Creating Objects String title, extendedTitle; title = new String(“This is my title”); extendedTitle=title.concat(“ Live with it.”);
10
Statements Assignment statements Invoking class methods System.out.println(title); More complex statements (later);
11
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;
12
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();
13
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;
14
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)
15
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.
16
Simple Statements. Assignments: x = 17; Expressions: x = a + b; Class methods: System.out.println(“Hello”); String title=“Hello”; String moreTitle = title.concat(“ World”);
17
Complex Statements if (condition) statement; if (condition) statement; else statement; while(condition) statement;
18
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”);
19
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)) …
20
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.
21
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) …
22
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.
23
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.
24
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;
25
for for loops exist because many loops follow the format: Initialization statements while(condition) { …. statements to update the condition variable(s) }
26
for(…;…;…) for(initialization;condition;increment) for(int i=0; i < 100; i++) System.out.println(i);
27
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.
28
Applets No main method Run in context of web browser (or appletviewer) Begin by ‘overriding’ Applet methods, typically paint.
29
What to review Self-test exercises – answers in book. Exercises from chap 1, 2, 3 Programs you have done or done in class.
30
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.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.