Download Simple Java Programs - Introduction to Programming in Java - Lecture Slides and more Slides Network security in PDF only on Docsity! • Objectives – give some simple examples of Java applications and one applet 2. Simple Java Programs Docsity.com Contents 1. Steps in Writing a Java Application 2. Hello.java 3. A Better Programming Environment? 4. Comparison.java 5. Steps in Writing a Java Applet 6. WelcomeApplet.java Docsity.com Compile & Run
> javac Hello. java
baer: af
Hello Andrew
ba
Docsity.com
Notes • import imports pre-defined classes from the java.io package – * means “all classes” • All Java applications must have a main() function (method) – static means the method is ‘inside’ the class – public means the method can be called from outside the class – args[] stores command line arguments (not used here) continued Docsity.com • The Java file (e.g. Hello.java) must contain a public class with the file’s name (e.g. Hello class). continued Docsity.com • Useful Notepad++ features – it will format Java code automatically – colour-coded display of code – it is possible to add calls to javac, java, appletviewer to the Notepad++ menu • no need to leave the editor to compile/run – there is an optional window that show the output from running Java code Docsity.com
| ete
File Edit Search View Encoding Language Settings Macro Run TextFX Plugins Window
(2H RSS| 4oG\ae\m e454 (Ba E10
(OP (Java)\Code\Intro\Hello.java - Notepad++
1
2 // Hello.java
3 /f Bndrew Davison, Nov 2007,
[email protected]
4
5 Public class Hello
6 {
a public static void main(String args[])
8 { System.out.printin("Hello Andrew"); }
3 } // end of Hello class
10
Console
NPP_EXEC: "Compile and Run™
cD: Di \Teaching\ OOP (Java) \Code\Intra
Current directory: D:\Teaching\ OOP (Java) \Code\Intro
javac Hello.java
Process started >>>
<<< Process finished.
java Hello
Process started >>>
Hello Andrew
<<< Process finished.
READY
length: 213 |Ln:10 Col:1 Sel:0 | Dos\Windows | ANSI UINS |
Docsity.com
Notepad++ Macro Menu Read the Notepad++Java.pdf document at the course Website Docsity.com if ( number1 >= number2 ) result = result + "\n" + number1 + " >= " + number2; // Display results JOptionPane.showMessageDialog( null, result, "Comparison Results", JOptionPane.INFORMATION_MESSAGE ); } // end of main() } // end of Comparison class Docsity.com Compile & Run $ javac Comparison.java $ java Comparison $ Docsity.com Notes • The Comparison class is just a single main() function (method) continued Docsity.com Calling Methods • Methods are defined in classes. • Syntax for calling a method: Class.method-name or object.method-name – e.g. JOptionPane.showMessageDialog(...); • this calls the showMessageDialog() method in the class JOptionPane Classes start with an upper case letter. Docsity.com The Integer Class • int is a C-like built-in type • Integer is a Java class for integers – used when integer methods are required • Integer.parseInt() converts a string to int Docsity.com Classes as Libraries • One way of using a class is as a library for useful methods. – the JOptionPane class has many methods for creating different kinds of dialog boxes; – the Integer class has many methods for manipulating integers Docsity.com 6. WelcomeApplet.java import javax.swing.JApplet; import java.awt.Graphics; public class WelcomeApplet extends JApplet { public void paint(Graphics g) { g.drawString(“Welcome Andrew”, 25,25); } } Docsity.com WelcomeApplet.html <html> <head> <title>Welcome Andrew</title> </head> <body> <applet code=“WelcomeApplet.class” width=300 height=30> [Java Welcome applet runs here] </applet> </body> </html> Docsity.com Compile & Run $ javac WelcomeApplet.java $ appletviewer WelcomeApplet.html Docsity.com • A better solution is to use the Opera browser: – free from http://www.opera.com – it comes with JRE 1.6, the latest version of the Java Runtime Environment, or it can be linked to the JRE already on your machine – there is no need for a Java plugin – Opera is very fast, small-size, and supports many networking standards continued Docsity.com ‘elcomeApplet.html - Opera
L) File Edit View Bookmarks Widgets Tools Help |) x)
GCHaAQHe F OU
Open Save Print Find Home Panels Tile Cascade
ia DZone EB amazon killer EBresp i= CoE Java (2 JavaCrawl W/ wikipedia
oT +> bP & SS File Mocalhost/D:/Teaching/JavafCode/Intro/Welcomedpplet. htrl [=| [G] soodle [=|
Welcome Andrew
Load WelcomeApplet.html
3 Docsity.com