Datalogi A 16: 21/11. Java Play sounds in applications Compiling to jar-files Compiling to exe-files Exceptions Sorting, reading from a file, writing.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
Computer Science A 13: 31/3. From.java to.exe - classpath - jar - resources - wrapper/launcher.
Simple Java I/O Part I General Principles. 2 Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
Computer Science A 11: 24/3. Files and Input/Output Get information about a file : File f= new File("data.txt"); if(f.exists()) System.out.println("file.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
18 File handling1June File handling CE : Fundamental Programming Techniques.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Review Java.
A Short Introduction to JAVA
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
1 CSE 331 Java Packages; JAR Archives slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Java Spring PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 11: Handling Errors; File Input/Output.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
Mixing integer and floating point numbers in an arithmetic operation.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
WEEK 2 Introduction to Java II CSE 252 Principles of Programming Languages LAB SECTION.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
CSCI 212 Object-Oriented Programming in Java. Prerequisite: CSCI 111 variable assignment statement while loop for loop post-increment (i++) strong typing.
Java Part I By Wen Fei, HAO. Program Structure public class ClassName { public static void main(String[] args) { program statements } user defined methods.
3/5/2002e-business and Information Systems1 Java Java Java Virtual Machine (JVM) Java Application Program Interface (API) HW Kernel API Application Programs.
Objectives  File I/O: using Scanner with File  Inserting into Partially Filled Array  Deleting from Partially Filled Array  Static methods and variables.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
Object Oriented Programming Lecture 2: BallWorld.
Spring 2006 Special Topics in Computer Engineering: Java Intro 1 Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Introduction of Java Fikri Fadlillah, S.T.
Reading from a file and Writing to a file
Intro to ETEC Java.
Chapter No. : 1 Introduction to Java.
Strings and File I/O.
Advanced Programming in Java
5 Variables, Data Types.
An Introduction to Java – Part I, language basics
class PrintOnetoTen { public static void main(String args[]) {
In this class, we will cover:
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

Datalogi A 16: 21/11

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{ public static void main(String args[]){ Clip crash = JCanvas.loadClip("crash.au"); JCanvas.playClip(crash); JCanvas.sleep(4000); }

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

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

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:

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.

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

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

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

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

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

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

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 data=new ArrayList (); while(in.hasNextLine()){ data.add(in.nextLine()); } in.close();

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();

Java I/O import java.io.*; class File, class PrintWriter File: make directory listings: boolean isDirectory()is the file a directory Long lengthsize of a file(bytes) String[] list()(list of files in a dir.)

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

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

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

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 time 260ms

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

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

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.

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

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

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

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);

Java: classes class content: fields, Static fields Methods Static methods Constructors