Download presentation
Presentation is loading. Please wait.
Published byHarry Carter Modified over 8 years ago
1
First Programs Chapter 2 The Java language (compiler) on CD ROM Create a program using an editor Compile the program Run the program Integrated Development Environments (IDEs) may be used or any text editor
2
File names and extension names must be unique Extension follows a period – after the program name. Tells what type of program (file) it is. –Game.java –Letter.doc Folders may hold several related files –Toms (folder) may hold files and/or other folders: –Myprogs letters(folders
3
Windows Explorer can list folder and file names UNIX/Linux, Apple O.S. may do the same
4
Using an Editor Create files Retrieve (open) an old file Save a changed file Delete, insert, change data, cut, copy/paste from/to files Search/replace text Many editors are available: NotePad, WordPad Students must become familiar with the editor of their choice
5
Create a first Java Program: Several programs are provided to help Editor Compile Java Virtual Machine Use an editor to key in the following slide
6
import java.awt.event.*; import javax.swing.*; public class Hello extends JFrame { – public static void main (String [ ] args) { –JOptionPane.showMessageDialog (null, “Hello World!”); –JOptionPane.showMessageDialog (null, “Goodbye”); –System.exit(0); –}// end main method –} // end Hello class import java.awt.*;
7
Give the file a name “Hello.java” Compiling (translates/converts Java statements into byte code) a language a Java compiler understands. Bytecode is an idealized language Not machine code. Bytecode must be inperpreted by the JVM (see appendix “I” for details)
8
Error messages appear when program does not use correct syntax (grammer) “Hello.java: 9: ‘;’ expected Line number nine of the program seems to need a semicolon
9
Common errors Semicolons missing/wrong placement Brackets m issing Single quotes used when double quotes required
10
After editing and compiling: run the program After successful compile the “Hello.java” program is translated into a second program “Hello.class” (bytecode) When run “Hello World!” (without quotes) Is displayed Click OK button to display “GoodBye”
11
The libraries The class file may need help from an already written program – in a library of such programs Possible errors –Library missing (or was not called) –Library name misspelled
12
Text of Hello program refering to line numbers 1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 public class Hello extends JFrame { –6 public static void main (String [ ] args) { –7 JOptionPane.showMessageDialog (null, “Hello World!”); –8 JOptionPane.showMessageDialog (null, “Goodbye”); –9 –10 System.exit(0); –}// end main method –} // end Hello class
13
Meaning of line numbers cited 1-3 libraries invoked 4 blank line to help readability 8-9 output Hello and GoodBye 10 –stop run (System exits)
14
Objects, methods: an introduction The CD played example –One in the kitchen –2 nd in bedroom –Buttons identical on each –Vague: “Stop” –Better: kitchenCD.stop (more specific) –Java:kitchenCD.stop();
15
bedroomCD.select(4) Might mean play the 4 th track on the CD in the bedroom (as an example) Parameters: the (4) in the CD example tell which track to play – some methods need parameters/arguments passed to them
16
Classes an analogy The CD example kitchen/bedroom in an “inheritance” example of a class. CD we may need another instance “den” which will inherit from the class
17
Java uses “new” to Create instances (also known as objects) of a class Classes can produce instances as required
18
The Greeting program frame.setSize(300,200); See page 18 for the full text of “Greeting” The size is in pixels 300 horizontal wide and 200 vertical height
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.