Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Simple Java Programs - Introduction to Programming in Java - Lecture Slides, Slides of Network security

The key points are:Simple Java Programs, Applications, One Applet, Writing a Java Application, Better Programming Environment, Application, Text File, Java Compiler, Java Bytecodes, Java Runtime System

Typology: Slides

2012/2013

Uploaded on 04/22/2013

sathaye
sathaye 🇮🇳

4.8

(8)

106 documents

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Objectives
give some simple examples of Java
applications and one applet
2. Simple Java Programs
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

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

  1. Simple Java Programs

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

2. Hello.java

import java.io.*;

public class Hello

public static void main(String args[])

System.out.println(“Hello Andrew”);

} // end of class

Compile & Run

  • The Java file (e.g. Hello.java) must contain a

public class with the file’s name (e.g. Hello

class).

continued

  • System.out is the standard output stream
    • like cout (C++) or stdout (C)
  • System.out.println() is the main print

function (method) in Java.

Hello

main() calls

System.out.println(…)

writes to screen

via output stream

  • 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

4. Comparison.java

import javax.swing.JOptionPane; // GUI dialogs

public class Comparison

public static void main( String args[] )

{ String firstNumber,secondNumber,result;

int number1,number2;

// read user numbers

firstNumber = JOptionPane. showInputDialog(

"Enter first integer:");

secondNumber = JOptionPane. showInputDialog (

"Enter second integer:" );

// convert numbers

number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber );

result = ""; if ( number1 == number2 ) result = number1 + " == " + number2; if ( number1 != number2 ) result = number1 + " != " + number2; if ( number1 < number2 ) result = result + "\n" + number1 + " < " + number2; if ( number1 > number2 ) result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) result = result + "\n" + number1 + " <= " + number :

Compile & Run$ javac Comparison.java

$ java Comparison

Notes

• The Comparison class is just a single

main() function (method)

continued

  • Notice the use of familiar C/C++ control

structures (e.g. if, while) and data types

(e.g. int, double)

  • “...” + “...” means concatenation

(put strings together)

  • String is a pre-defined class for strings.
  • int is a built-in type (just like C’s int).

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.