Presentation is loading. Please wait.

Presentation is loading. Please wait.

Datalogi A 1: 8/9. Book: Cay Horstmann: Big Java or Java Consepts.

Similar presentations


Presentation on theme: "Datalogi A 1: 8/9. Book: Cay Horstmann: Big Java or Java Consepts."— Presentation transcript:

1 Datalogi A 1: 8/9

2 Book: Cay Horstmann: Big Java or Java Consepts

3 Cay Horstmann: Java Essentials If you have an earlier edition – then use it. buy it from students who have attended the course earlier

4 Aim: Basics of programming –Central constructs in programming languages –variables, expressions, statements –if, while, for,… –subroutines (procedures, functions) –lists, arrays, classes, objects Basics of writing interactive systems –Windows, panels, fields –Events from mouse, keyboard, buttons, etc.

5 Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor, libraries. A bit about software engineering – methods used in constructing programs. A bit about graphics

6 Windows, frames, buttons, menus

7 Computer Science A What you have to do 3 * home work 3 * mini-project 1 * oral test based on mini-projects All information available on http://akira.ruc.dk/~madsr/cs-a.html

8 Java programs public class Hello{ public static void main(String args[]){ System.out.println(”Hello world”); } A program contains classes (here Hello )chapters classes contains methods (here main )paragraphs methods contains statements (here System.out… ) One class has the same name as the file ( Hello.java ) One method in that class is called main

9 Java programs Extra spaces and newlines does not matter, except: No newlines in text strings ( ”… ” ) At least one space between words public static Use indentations to make programs readable. The compiler ignores it, but I don’t Case does matter String, class Hello and Hello.java

10 Comments You can write comments in a program. Comments are ignored by the compiler. Line comments: int i=6; // declare a variable Block comments: /* The main entry point in the program: */ public static void main(String args[]){

11 Java statements Print out System.out.println( …. Reading input … Declare variables int counter; Change values of variables counter = 1; this can be combined into int counter = 1; Method calls if statements while statements

12 Expressions and values For now: strings and numbers String name=”Hello”; int counter = 2; (case does matter) Operations + on strings to concatenate +,-, *,/ on numbers add, subtract, multiply, divide

13 Types String var1 = ”1”; // text strings char var2= ’1’; // single characters int var3 = 1; // integers double var4 = 1.0; // real values Everything can be converted to String – but it doesn’t work the other way. Int can be used as double, but not the other way. Division between two integer values is an integer. If one or both are reals then the result is a real. String to integer conversion: Integer.parseInt(stringVariable);

14 Assignment int counter; create a variable called counter counter=1; store the value 1 in the variable counter=counter+1; compute the value of the expression counter+1 and store that in the variable. counter now has the value 2

15 Input/output Input/output via command prompt screen Input/output via Dialog windows Input/output via window system (mouse/graphics etc.) Input/output via files We will wait a bit with the last two ways of reading a writing

16 Input/output Input/output via command prompt screen java.util.Scanner in= new java.util.Scanner(System.in); System.out.println("type a name"); String line=in.nextLine(); System.out.println("type a number"); int num=in.nextInt(); System.out.println("read: "+line+" "+num); Read a line and a number and save what you read in newly created variables

17 Input nextLine reads text until next linebreak, returns the text and skips the linebreak nextInt skips spaces and newlines until start of a number, it reads and returns the number Notice that java and DOS does not read anything until you have finished a line. After a nextInt call the next symbol is probably a newline character.

18 Input input via dialog windows String name= javax.swing.JOptionPane.showInputDialog( "type a name"); String num = javax.swing.JOptionPane.showInputDialog( "type a number"); javax.swing.JOptionPane.showMessageDialog( null,"read: "+name+" "+num); Read a line and a number and save what you read in newly created variables

19 Java programs public class Hello{ public static void main(String args[]){ System.out.println(”Hello world”); } Public: you can hide stuff from other parts of your program. Here: everything can be seen everywhere static: some stuff may only exist for some of the time a program is executed. Here: it exists all the time void: methods may return values. Here: it does not return anything. String args[]: When you start a program it may have some command line arguments (you drop a file on a program etc). Here: it is not used.

20 Objects and classes Next Friday: Objects and classes JC chapter 2 and 3, (CCJ chapter 2) JC P3.1-3.5, P3.7 (CCJ P2.5-P2.9, P2.11). First home assignment, Exercise JC P3.6 (CCJ P2.10) is to be handed in 26th September 13.00


Download ppt "Datalogi A 1: 8/9. Book: Cay Horstmann: Big Java or Java Consepts."

Similar presentations


Ads by Google