Datalogi A 16: 21/11
-
Upload
perdita-march -
Category
Documents
-
view
25 -
download
0
description
Transcript of Datalogi A 16: 21/11
![Page 1: Datalogi A 16: 21/11](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/1.jpg)
Datalogi A 16: 21/11
![Page 2: Datalogi A 16: 21/11](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/11.jpg)
Jar to exe filePlaySound.exe can be played on other
computers with jre (java runtime environment)
![Page 12: Datalogi A 16: 21/11](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/17.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/19.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/20.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/25.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/26.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062321/568130f1550346895d9716a0/html5/thumbnails/28.jpg)
Java: classesclass content:
• fields,
• Static fields
• Methods
• Static methods
• Constructors