Computer Science A, 1

advertisement
Datalogi A 1: 8/9
Book: Cay Horstmann:
Big Java or Java Consepts
Cay Horstmann: Java Essentials
• If you have an earlier
edition – then use it.
buy it from students who
have attended the course
earlier
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.
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
Windows, frames, buttons, menus
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
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
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
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[]){
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
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
Types
String var1 = ”1”;
char var2= ’1’;
int var3 = 1;
double var4 = 1.0;
//
//
//
//
text strings
single characters
integers
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);
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
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
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
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.
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
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.
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