TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview.

11
TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview

Transcript of TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview.

Page 1: TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview.

TCU CoSc 10403 Introduction to Programming

(with Java)

Java Language Overview

Page 2: TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview.

Consider another simple HelloWorld program

import javax.swing.*;

import java.awt.*;

/*

Another HelloWorld program in Java

*/

public class HelloWorld extends JApplet

{

JLabel helloLabel = new JLabel ("How ya doing?");

public void init()

{

setLayout(new FlowLayout());

add (helloLabel);

helloLabel.setForeground (Color.RED);

}

}

Page 3: TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview.

Syntax & Semantics

• Note that the preceding program contained a variety of different symbols: ,*/();.”

• These are delimiters (punctuation marks) in the language and must appear in particular places.

• Similarly, note that there are words such as (“class”, “public”, “extends”, “import”, “Applet”) - these are reserved words in the language and must not be misspelled or written in the wrong context.

Page 4: TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview.

Syntax & Semantics

• The syntax of a language defines how you can put symbols, reserved words, and identifiers together to make a valid program.

• During compilation, all syntax rules are checked.• If a program is not syntactically correct, the compiler gives error

messages and won’t produce the bytecode.• Syntax errors must be eliminated before the bytecodes are produced

that allows us to try to ‘execute’ the program.

• The semantics of a language construct is the meaning of the construct; it defines its role in a program

• A syntactically correct program does not mean it is logically (semantically) correct.

• A program will always do what it is told to do - not what the programmer meant to tell it to do.

Page 5: TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview.

Errors

A program may have three different types of errors.

1. Compile-time errors - errors with syntax (if compile-time errors exist, an executable version of the program is not created.

2. Run-time errors – program compiles – starts to excute and then crashes (terminates abnormally) (Example: divide by zero).

3. Logical errors – program compiles and executes.The program runs with no errors but produces incorrect results.

Page 6: TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview.

Problem Solving• Debugging : finding and fixing errors in code

• System.out.println prints to the console window

• use System.out.println statements to see where you are in the program’s execution or to display the value of variables:

– System.out.println( “a” );– System.out.println( “x = ” + x );

• These will display in the Console window at the bottom of the IDE:

6

System.out.println information appears in the Console window.System.out.println information appears in the Console window.

Page 7: TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview.

Coding Style

• White Space– Spaces, blank lines, and tabs are collectively called white space and

are used to separate words and symbols in a program

– Extra white space is ignored

– A valid Java program can be formatted many different ways

– Programs should be formatted to enhance readability, using consistent indentation

– Poor examples:

*** Choose some style (mine, theAuthor’s) and stick with it!!!

Page 8: TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview.

Better Style

• White space consists of blanks, tabs, and newline characters.– White space is used to

separate the words and symbols used in Java programs.

– A programmer can use white space to emphasize parts of the program and to make the program easier to read.

• Except when it’s used to separate words, the computer ignores white space.

/********************************************************************/

/* Program Name: Lab 1 */

/* */

/* Student Name: J.Q. Public */

/* Semester: Spring, 2004 */

/* Class & Section: CoSc 10403 - 15 */

/* Instructor: Dr. James Comer */

/* */

/* Program Overview */

/* This program displays a string of text in the center */

/* of the applet window. The text is drawn in blue in */

/* bold, 16 point Helvetica */

/* */

/* Input: */

/* There is no input to this program. */

/* */

/* Output: */

/* The string of text that is hard coded in the program. */

/* */

/* Program Limitations: */

/* The text is centered only if the window is 200 x 200. */

/* */

/* Significant Program Variables: */

/* None */

/* */

/********************************************************************/

import java.awt.*;

import javax.swing.*;

Import java.applet.;

public class MyApplet extends JApplet

{

public void init()

{

repaint();

}

public void paint(Graphics g)

{

g.setFont(new Font("Helvetica", Font.BOLD, 16));

g.setColor(Color.blue);

g.drawString("Texas Christian University", 5, 100);

}

}

Page 9: TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview.

Documentation

• All programs are to be documented using the Documentation Guidelines and the Documentation Standard provided on the 10403 class website.

• Failure to do so will result in several lost points on your graded lab assignment.

• Comments should include a detailed “preamble” set of comments (see the standard) and inline documentation (to explain the purpose of the program and to describe processing steps). These are to be positioned within the executable statements of your program.

• Java comments (which are ignored by the compiler) can take two forms:

// comment runs to the end of the line

and

/* The purpose of this program is ……………..…………………………………………………………………………………………………..*/

Page 10: TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview.

Documentation Standard/***************************************************************************************************//* Program Name: Lab# 3 *//* *//* Student Name: John Q. Programmer *//* Semester: Fall, 1999 *//* Class & Section: CoSc 10403 - 15 *//* Instructor: Dr. Hallie Pena *//* Due Date: September 23, 1999 *//* *//* Program Overview: *//* This applet uses a text field to accept user input representing a Fahrenheit temperature. *//* When the user presses the <enter key>, the temperature is converted to Celsius. the *//* result is displayed in a label and, in addition, in a text area that serves as an ongoing *//* log of all conversions that have been performed. The user can continue to perform *//* terperature conversions until the applet is terminated. *//* *//* Input: *//* Input data must be the integer Fahrenheit value that is to be converted to Celsius. *//* *//* Output: *//* The corresponding Celsius value for the given input data. Past conversions are displayed *//* in a scrollable text area at the bottom of the display window. */

/* *//* Program Limitations: *//* (1) only integer Fahrenheit values may be input for conversion. *//* (2) care should be taken when inputing values to be converted as the previous input value *//* remains in the input textfield until erased. */

/* *//* Significant Program Variables: *//* title - holds the title that is displayed at the top of the display window. *//* question - holds the string that is displayed as the user prompt. *//* . . . *//* *//* References: *//* (1) this program originally appeared in: "Java Software *//* Solutions", John Lewis and William Loftus, Addison-Wesley, *//* 1998, pp. 375-377. *//***************************************************************************************************/

Page 11: TCU CoSc 10403 Introduction to Programming (with Java) Java Language Overview.

Terminology for the parts of a Java program

consists ofclasses

consist ofmembers (instances of the class)

fields methods

are

variables objects void typed

classestypes

declared withconsist of

are

constructorsparameters

fieldsstatements

innerclasses

special typed methods

areare

fields

invocationassignmentrepetitionselectionexception

are

programA java