datalogi a 16: 21/11

28
Datalogi A 16: 21/11

Upload: perdita-march

Post on 01-Jan-2016

25 views

Category:

Documents


0 download

DESCRIPTION

Datalogi A 16: 21/11. CS A. Java. Play sounds in applications Compiling to jar-files Compiling to exe-files Exceptions Sorting, reading from a file, writing to a file A short overview of java. Sound. import javax.sound.sampled.Clip; public class PlaySound{ - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Datalogi A 16: 21/11

Datalogi A 16: 21/11

Page 2: Datalogi A 16: 21/11

JavaPlay sounds in applications

Compiling to jar-files

Compiling to exe-files

Exceptions

Sorting, reading from a file, writing to a file

A short overview of java

Page 3: Datalogi A 16: 21/11

Sound<uses new version of JCanvas>

import javax.sound.sampled.Clip;public class PlaySound{ public static void main(String args[]){ Clip crash = JCanvas.loadClip("crash.au"); JCanvas.playClip(crash); JCanvas.sleep(4000); }}

Page 4: Datalogi A 16: 21/11

SoundplayClip(Clip clip) starts the sound, and

returns

loadClip(String filename) format: .au, .wav, .aiff

stopClip(Clip clip)

loopClip(Clip clip,int n)

Page 5: Datalogi A 16: 21/11

Jar filesCompress and collect several files in an archive

(zip like)

>jar cf HelloWorld.jar HelloWorld.class

Run it:>java –cp HelloWorld.jar HelloWorld

cp: classpath: HelloWorld.jar,

Main class: HelloWorld

Page 6: Datalogi A 16: 21/11

Access to resourcesBufferedImage image

= JCanvas.loadImage(”pict.jpg”);

Clip sound

= JCanvas.loadClip(”sound.wav”);

Find it in local directory – same as the class

file.

More robust approach:

Page 7: Datalogi A 16: 21/11

Access to resourcesAccess from a URL:

BufferedImage image

= JCanvas.loadImage(

PlaySound.class.getResource(”pict.jpg”));

Clip sound

= JCanvas.loadClip(

PlaySound.class.getResource(”sound.wav”));

Find the main class (PlaySound), from that class

create a URL of a ressource local to that class.

Page 8: Datalogi A 16: 21/11

Jar files with resourcesPut class files an resources in a jar file:

> jar cf PlaySound.jar *.class *.au

> java –cp PlaySound.jar PlaySound

Works if you access sounds using ”getResource”.

Page 9: Datalogi A 16: 21/11

JSmoothJava program launcher:

JSmooth: takes a .jar file, an .ico file and generates an exe file.

The exe file locates a java runtime environment on the machine and run the jar-file

Page 10: Datalogi A 16: 21/11

Running JSmoothSkeleton: Windowed wrapper

Executable:

Executable Binary: full name of exe file

Executable Icon: full name of icon file

Current Directory: full name of directory

Application

Main Class: main class

Embedded jar: yes, jar file

System: save as

Project: compile

Page 11: Datalogi A 16: 21/11

Jar to exe filePlaySound.exe can be played on other

computers with jre (java runtime environment)

Page 12: Datalogi A 16: 21/11

Java: reading from a fileHow to open a file for reading:

Scanner in=null;try{ in=new Scanner(new File("text.txt"));}catch(IOException e){ System.out.println("Cannot open file text.txt"); System.exit(0);}

Scanner can now be used as with keyboard input

Page 13: Datalogi A 16: 21/11

Java: exceptionstry{

//try block

}catch(Exception e){

// handle exceptions from try-block

}

Examples of exceptions: null pointer de-reference, class cast, array index, Arithmetic exception (division by zero).

Page 14: Datalogi A 16: 21/11

Read lines from a fileScanner in=null;try{ in=new Scanner(new File("text.txt"));}catch(IOException e){ System.out.println("Cannot open file text.txt"); System.exit(0);}

ArrayList<String> data=new ArrayList<String>();while(in.hasNextLine()){ data.add(in.nextLine());}in.close();

Page 15: Datalogi A 16: 21/11

Write to a fileimport java.io.*;…

PrintWriter out= null;try{ out=new PrintWriter(new FileWriter("text1.txt"));}catch(IOException e){ System.out.println("Cannot write to file text1.txt"); System.exit(0);} for(String s:data) out.println(s);out.close();

Page 16: Datalogi A 16: 21/11

Java I/Oimport java.io.*;

class File, class PrintWriter

File: make directory listings:

boolean isDirectory() is the file a directory

Long length size of a file(bytes)

String[] list() (list of files in a dir.)

Page 17: Datalogi A 16: 21/11

SortingThe easy principle:

Find the smallest and place at index 0,

Find the smallest of the remaining and place at index 1,…

Page 18: Datalogi A 16: 21/11

SortingSorting algorithm:

int[] data=new int[10];for(int i=0;i<data.length;i++) data[i]=random(1000);

for(int i=0;i<data.length;i++){ int j=indexSmallest(data,i); swap(data,i,j); }

Page 19: Datalogi A 16: 21/11

Index of smallest in array

static int indexSmallest(int[] data,int from){ int index=from; for(int i=from+1;i<data.length;i++) if(data[i]<data[index])index=i; return index;}

static void swap(int[] data,int i1, int i2){ int h=data[i1]; data[i1]=data[i2]; data[i2]=h;}

Page 20: Datalogi A 16: 21/11

Timing the sorting algorithm

size 1000 time 0mssize 2000 time 20mssize 3000 time 20mssize 4000 time 40mssize 5000 time 70mssize 6000 time 90mssize 7000 time 130mssize 8000 time 161mssize 9000 time 200mssize 10000 time 260ms

Page 21: Datalogi A 16: 21/11

Java: overviewVariables:

a type and a name

Simple types:

int, float, double, boolean, String, char

Objects:

a class name

Arrays:

a type + ”[]”

Page 22: Datalogi A 16: 21/11

Java: variablesLocal variable:

void method(){

int i =64;

while(i>0){

System.out.println(i);

i=i/2;

}

}

Page 23: Datalogi A 16: 21/11

Java: parametersLike local variables – but initialised at the call

int sqare(int x){return x*x;}

You may change the value of a parameter in the method – but it will not change the argument in the call.

Page 24: Datalogi A 16: 21/11

Java: fields in objectsclass point{

int x,y;

Point(int x1,int y1){

x=x1; y=y1

}

}public class Application{

public static void main(String args[]){

Point p=new Point(2,3);}}

Page 25: Datalogi A 16: 21/11

Java: static fieldsOne instance per class. You may use them from static

methods.

public class Program{ static int data[]=new int[10]; public static void main(String args[]){ data[0]=1; …. }}

Page 26: Datalogi A 16: 21/11

Java: statementsif- if(x<0)x=-x;

While while(x>1){y=y*2;x--;}

for- for(int i=0;i<10;i++)..

Assignments-Method calls-System.out.println(”hello”);

Blocks

Page 27: Datalogi A 16: 21/11

Java: arraysint[] data =new int[10]

Point[] points=new Points[20];

for(int i=0;i<data.length;i++)data[i]=i*i;

for(Point p:points)p.setY(0);

Page 28: Datalogi A 16: 21/11

Java: classesclass content:

• fields,

• Static fields

• Methods

• Static methods

• Constructors