1 / 28

Datalogi A 16: 21/11

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. <uses new version of JCanvas> import javax.sound.sampled.Clip; public class PlaySound{

Download Presentation

Datalogi A 16: 21/11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Datalogi A 16: 21/11 CS A

  2. 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

  3. 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); } }

  4. Sound playClip(Clip clip) starts the sound, and returns loadClip(String filename) format: .au, .wav, .aiff stopClip(Clip clip) loopClip(Clip clip,int n)

  5. Jar files Compress 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

  6. Access to resources BufferedImage image = JCanvas.loadImage(”pict.jpg”); Clip sound = JCanvas.loadClip(”sound.wav”); Find it in local directory – same as the class file. More robust approach:

  7. Access to resources Access 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.

  8. Jar files with resources Put 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”.

  9. JSmooth Java 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

  10. Running JSmooth Skeleton: 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

  11. Jar to exe file PlaySound.exe can be played on other computers with jre (java runtime environment)

  12. Java: reading from a file How 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

  13. Java: exceptions try{ //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).

  14. Read lines from a file 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); } ArrayList<String> data=new ArrayList<String>(); while(in.hasNextLine()){ data.add(in.nextLine()); } in.close();

  15. Write to a file import 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();

  16. Java I/O import 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.)

  17. Sorting The easy principle: Find the smallest and place at index 0, Find the smallest of the remaining and place at index 1,…

  18. Sorting Sorting 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); }

  19. 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; }

  20. Timing the sorting algorithm size 1000 time 0ms size 2000 time 20ms size 3000 time 20ms size 4000 time 40ms size 5000 time 70ms size 6000 time 90ms size 7000 time 130ms size 8000 time 161ms size 9000 time 200ms size 10000 time 260ms

  21. Java: overview Variables: a type and a name Simple types: int, float, double, boolean, String, char Objects: a class name Arrays: a type + ”[]”

  22. Java: variables Local variable: void method(){ int i =64; while(i>0){ System.out.println(i); i=i/2; } }

  23. Java: parameters Like 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.

  24. Java: fields in objects class 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); }}

  25. Java: static fields One 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; …. } }

  26. Java: statements if- 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

  27. Java: arrays int[] 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);

  28. Java: classes class content: • fields, • Static fields • Methods • Static methods • Constructors

More Related