130 likes | 354 Views
Introduction to Algorithm. What is Algorithm?. an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output
E N D
What is Algorithm? • an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output • An algorithm is thus a sequence of computational steps that transform the input into the output • We can also view an algorithm as a tool for solving a well-specified computational problem.
How it represented? • Description in natural language (Bahasa, English, any human language) • Visualization (flow chart, flow diagram, etc) • Pseudo code (Algorithmic notation)
Example • Sorting algorithm: one might need to sort a sequence of numbers into nondecreasing order. • For example, given the input sequence 31, 41, 59, 26, 41, 58 • a sorting algorithm returns as output the sequence 26, 31, 41, 41, 58, 59 • find the largest number in an (unsorted) list of numbers
Solution Algorithm LargestNumber Input: A non-empty list of numbers L. Output: The largest number in the list L. largest ← L0 for each item in the list L≥1, do if the item > largest, then largest ← the item return largest
What is Java • The Java programming language is a high-level object-oriented language which was developed by Sun Microsystems. Like other object oriented languages, Java programs use classes and objects • Java is more than simply a programming language. It is a cross platform software environment, which means that the same program can run on many different combinations of processor and operating system, without being recompiled for each platform. • A Java source file is compiled to an intermediate language called bytecode. Bytecode is interpreted by a piece of software called a Java Virtual Machine (JVM), which translates it into the machine code for the system it runs on. The bytecode is exactly the same for all system. • Java source files have a .java extension. Bytecode (compiled) files have a .class extension.
The Java Platform • The Java platform consists of the following: • The JVM • The Java Application Programming Interface (API). • The JVM provides the environment on which Java programs can run. The API provides a large collection of software components, or classes, which the programmer can include in programs. These provide a wide range of capabilities, e.g. user interface components, networking components, and many more. These components are organised in packages. • The Java Runtime Environment (JRE) : This provides the JVM and the core Java classes. If you want to simply run existing Java programs on a system, then it is sufficient to have a JRE installed on the system. • The Java Software Development Kit (JDK or Java SDK): This includes the JRE, together with compilers and debuggers and other development tools. If you want to develop Java programs, a JDK must be installed on your system.
Types of Java project • Applications An application is a standalone program which runs on a JVM. It can produce text output to a command console, or it can have a graphical interface similar to a Visual Basic application. Graphical Java applications use the user interface components provided by the JVM, and look the same on any platform. • Applets An applet is a program designed to run inside a web browser. A web page will have an area set aside to display the output from an applet, and the applet is downloaded over the web and runs in the browser. The output is usually graphical, and can use some of the same user interface components as an application. • Web Applications A web application is a program which runs on a web server. Java web applications use technologies called servlets and Java Server Pages (JSP). Web applications are often used in web sites which interact with databases.
Java Development Tools • The only software which is actually needed to write, compile and run Java programs is a JDK and a text editor. Current versions of the JDK (and JRE) are available free from Sun, and can be downloaded from their web site. Previous versions can also be downloaded. • A source file can be created using the text editor and saved as a .java file. The JDK provides a compiler which can be run from a command line prompt to create a .class file. The .class file can then be executed from the command prompt. • Productivity can be improved greatly by using an Integrated Development Environment (IDE), similar to the Visual Basic environment. Commonly used IDEs include NetBeans, Eclipse and Borland JBuilder. • In this Kuliyah we will use BlueJ, an IDE which has been specifically designed for learning Java.
What is BlueJ • BlueJ is a Java integrated development environment (IDE) which has been designed specifically for learning object oriented programming in Java • It is more convenient to use than the standard Java command line tools, and easier to learn than a full-featured IDE such as NetBeans or Eclipse. • It will also help you understand how the classes in your object oriented programs are related to each other
Java class features • Comments Text between /* and */ symbols are comments to make the code easier to understand, and are ignored by the Java compiler. Comments can also be used to generate documentation automatically for your programs. Get into the habit of writing comments in this form at the top of all your programs. Comments can also be on a single line with // at the start. • Classes A class starts with a declaration: public class HelloWorld and all the code is inside a pair of curly brackets { … } A more complicated program can have more than one class. Every program must have at least one class. • Methods Methods are the actions that a class can perform. A class needs at least one method before it can actually do anything.
Java class features (2) • Statements A statement is a single instruction. In Java, each statement must be ended with a semicolon (;) • Variables A variable is a name which identifies a piece of data used by the program. The example contains two variables which contains a string of characters. String greeting = "How are you today?”; (String yourName) The variable yourName is created as a parameter of the sayHello method. • Console output Applications can output results to the terminal window, or console, using System.out.println statements. These can output specified characters or the value of variables.