Computer Science A, 1

Download Report

Transcript Computer Science A, 1

Computer Science A 1: 3/2
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
• Freeware book
Computer Science A
What you have to do
• 6 * home work
• 3 * mini-project
• 1 * oral test based on mini-project
All information available on
http://akira.ruc.dk/~madsr/csa-f09.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
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)
(three more types of values; some ways to construct
more complex values from simple ones.)
Operations
+ on strings concatenate
+,-, *,/ on numbers add, subtract, multiply, divide
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
input 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");
Output:
javax.swing.JOptionPane.showMessageDialog(
null,"read: "+name+" "+num);
Read a line and a number and save what you read in newly
created variables
TextIO
Eck has written his own class to do
Input/output. To use it you need to download
it from his web page and place it in the
same directory as your program.
To read a number write
int num = TextIO.getlnInt();
To read a String write
String s = TextIO.getln();
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
On Friday: expressions, variables etc
Eck chapter 2: 2.1 – 2.5 The important part is
2.1-2.3.2. The rest you can always look up when
you need it