Introduction To Java - MFC 158 G

Week 3 Lecture notes - Fall 2000

 

Chapter 2 - Introduction to Java Applications- MFC 158 (1 of 5)

 

Java Language

-         Facilitates a disciplined approach to computer program design

-         Applications (typically run locally on a machine / local network using a java interpreter - java)

-         Applets (typically run over the internet / with browsers or appletviewer)

 

// Fig. 2.1: Welcome1.java - pg 36 - A Java Application

// A first program in Java

public class Welcome1 {

   public static void main( String args[ ] )

   {

      System.out.println( "Welcome to Java Programming!" );

   }

}

(C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall.

 

-         Single line comments / multiline comments (c style)

-         Javadoc - comments - can create HTML formatted documentation from source code

-         Blank lines and space characters are allowed (white-space)

-         public class Welcome1 {… - class definition - all Java programs have at least one class defined by the programmer

-         the class name must be the same as the filename.java

-         keywords (reserved words) are reserved for use by Java ()

-         identifiers are names of classes and variables

-         statements end with semicolons ( ; )

-         every class in chapter 2-7 begins with public - publically accessable class

-         braces { } are used to define the beginning and end of classes - to create a 'block' of code

-         public static void main( String args[ ]) …a method called main - exists in every Java application

-         all applications execute beginning with the main method (interpreter)

-         String args[ ] - indicates that method main can accept arguments

-         Java Welcome1 argument1 argument2…argumentn

-         void indicates that the method main returns NOTHING (void)

-         System.out - allows applications to display strings, etc. to the command window

-         System.out.println - a method that displays a line of text

-         Right brackets indicate end of method main and of the class Welcome1

-         The program displays "Welcome to Java Programming!"

 

 

Introduction To Java - MFC 158 G

Week 3 Lecture notes - Fall 2000

 

Chapter 2 - Introduction to Java Applications- MFC 158 (2 of 5)

 

// Fig. 2.3: Welcome2.java

// Printing a line with multiple statements

 

public class Welcome2 {

   public static void main( String args[] )

   {

      System.out.print( "Welcome to " );

      System.out.println( "Java Programming!" );

   }

}

(C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall.

 

System.out.print - prints a string and leaves the cursor at the end of the string

System.out.println - prints a string and puts the cursor at beginning of next line

 

 

// Fig. 2.4: Welcome3.java

// Printing multiple lines with a single statement

 

public class Welcome3 {

   public static void main( String args[] )

   {

      System.out.println( "Welcome\nto\nJava\nProgramming!" );

   }

}

(C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall.

 

A Backslash indicates a 'special' character is to be outputted (I.E.  \n - newline, \t - horizontal tab, \\ - print a backslash,  \" - print a double quote

 

 

Dialog boxes 

-         windows for displaying messages to users of an application

-         Java class JoptionPane - allows you to easily display various dialog boxes

-         Gives the application a 'windows' type of interaction

-         Can be used for input and output

 

 

 

 

 

 

Introduction To Java - MFC 158 G

Week 3 Lecture notes - Fall 2000

 

Chapter 2 - Introduction to Java Applications- MFC 158 (3 of 5)

// Fig. 2.6: Welcome4.java

// Printing multiple lines in a dialog box

import javax.swing.JOptionPane;  // import class JOptionPane

 

public class Welcome4 {

   public static void main( String args[] )

   {

      JOptionPane.showMessageDialog(

         null, "Welcome\nto\nJava\nProgramming!" );

 

      System.exit( 0 );  // terminate the program

   }

}

(C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall.

-         the compiler uses import statements to identify and load classes

-         import tells the compiler to load class JoptionPane class from the javax.swing package.

-         Javax.swing contains several classes for creating GUI applications

-         Components such as buttons, menus, text fields and labels can be used.

-         ShowMessageDialog

-         Accepts two arguments (null for first argument - more on Chapter 13)

-         displays the text in a window

-         System.exit allows the program to pass a value (zero or other) to a calling program (batch file, script, etc.)

-         Class System is part of the package java.lang.  No import is necessary as java.lang is automatically imported in all Java programs.

---------------------------------------------------------------------------------------------------------

Concepts to consider for the next program

Variables:

-         consist of a name which references a location in computer memory.

-         each variable has a name, data type, size and value

 

Data types: Int / long (integers), float / double (decimal numbers), boolean (true/false), strings (composed of characters and words).

 

Arithmetic operators

Operator

Algebraic Expression

Java Expression

Addition

+

F + 7

F + 7

Subtraction

-

P - C

P - C

Multiplication

*

BM

B * M

Division

/

X / Y

X / Y

Modulus

%

R mod S

R % S

 

Introduction To Java - MFC 158 G

Week 3 Lecture notes - Fall 2000

 

Chapter 2 - Introduction to Java Applications- MFC 158 (4 of 5)

 

 

// Fig. 2.8: Addition.java

// An addition program

 

import javax.swing.JOptionPane;  // import class JoptionPane

 

public class Addition {

   public static void main( String args[] )

   {

      String firstNumber,   // first string entered by user

             secondNumber;  // second string entered by user

      int number1,          // first number to add

          number2,          // second number to add

          sum;              // sum of number1 and number2

 

      // read in first number from user as a string

      firstNumber =

         JOptionPane.showInputDialog( "Enter first integer" );

 

      // read in second number from user as a string

      secondNumber =

         JOptionPane.showInputDialog( "Enter second integer" );

 

      // convert numbers from type String to type int

      number1 = Integer.parseInt( firstNumber );

      number2 = Integer.parseInt( secondNumber );

 

      // add the numbers

      sum = number1 + number2;

 

      // display the results

      JOptionPane.showMessageDialog(

         null, "The sum is " + sum, "Results",

         JOptionPane.PLAIN_MESSAGE );

 

      System.exit( 0 );   // terminate the program

   }

}

 

 

 

 

Introduction To Java - MFC 158 G

Week 3 Lecture notes - Fall 2000

 

Chapter 2 - Introduction to Java Applications- MFC 158 (5 of 5)

 

 

Become familiar with the equality, relational operators and the order of evaluation Pg 54-57

 

 

 

// convert numbers from type String to type int

      number1 = Integer.parseInt( firstNumber );

      number2 = Integer.parseInt( secondNumber );

 

      // initialize result to the empty string

      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 + " <= " + number2;

      if ( number1 >= number2 )

         result = result + "\n" + number1 + " >= " + number2;

 

      // Display results

      JOptionPane.showMessageDialog(

         null, result, "Comparison Results",

         JOptionPane.INFORMATION_MESSAGE );

 

      System.exit( 0 );

   }