Getting Started with Java

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Introduction to Java.
The Web Warrior Guide to Web Design Technologies
1 Java Programming Basics SE-1011 Dr. Mark L. Hornick.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Ch2: Getting Started with Java - Objectives After.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
IT151: Introduction to Programming
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 1 “ Introduction to Java and OOP”
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2: Getting Started with Java *Components.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java.
Chapter Chapter 2 Getting Started with Java.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Java Language and SW Dev’t
CE-2810 Dr. Mark L. Hornick 1 GNU GCC Assembler and C/C++ compiler.
© 2006 Pearson Education 1 Obj: cont 1.3 and 1.4, to become familiar with identifiers and to understand how programming languages work HW: p.51 #1.8 –
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2: Java Fundamentals
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Sample Slides - 1 Sample Animated Slides for Wu, Intro to OOP.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
CS-1030 Dr. Mark L. Hornick 1 Java Review Interactive.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
 2005 Pearson Education, Inc. All rights reserved. 1 A class A class is the blueprint from which objects are generated. In other words, if we have six.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
Introducing Java Chapter 3 Review. Why Program in Java? Java, is an object-oriented programming language. OOP languages evolved out of the need to better.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Working with Java.
GC101 Introduction to computer and program
Chapter 3 GC 101 Java Fundamentals.
Chapter 2 Java Programming Basics
Defining Your Own Classes
Unit-1 Introduction to Java
Chapter 1: Computer Systems
MSIS 655 Advanced Business Applications Programming
Focus of the Course Object-Oriented Software Development
Chap 2. Identifiers, Keywords, and Types
Getting Started with Java
Computer Programming-1 CSC 111
Instructor: Alexander Stoytchev
Presentation transcript:

Getting Started with Java Chapter 2 Getting Started with Java

Topics Components of a Java Program Declaring and creating objects classes methods comments import statements Declaring and creating objects Java identifiers Standard Java classes

The First Java Program This first program displays a window on the screen. The size of the window is 300 x 200 pixels The title is My First Java Program. The fundamental OOP concept illustrated by the program: An object-oriented program uses objects.

The First Java Program /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample1.java */ import javax.swing.*; class Ch2Sample1 { public static void main(String[ ] args){ JFrame myWindow; myWindow = new JFrame(); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); }

UML diagram for Ch2Sample1 This diagram shows the messages sent to myWindow

Class diagram for Ch2Sample1 This diagram shows the dependency relationship between the classes in the program. The Ch2Sample1 class uses a JFrame object. To use an object in a program, first we declare and create and object. Then we send messages to it.

Declaring Objects When we declare an object, we must give it a name and say what class it belongs to. syntax: the name must be a valid Java identifier the class must be defined and available if multiple objects are declared, the names are separated by commas

Java Identifiers A Java identifier is a sequence of letters, digits, underscores, and dollar signs used to name a class, object, method, etc. The first character in a Java identifier must be a letter. Uppercase and lowercase letters are distinguished. myWindow, mywindow, MYWINDOW No spaces are allowed in an identifier.

Java Naming Conventions Use of an uppercase letter for the first letter of class names class Account Use a lowercase letter for the first letter of object names. object account Use all upper case letters for constants final int WINDOW_HEIGHT

Instantiating Objects No object is created by the declaration. We create an object by invoking the new operation: Example

object declaration and object creation assigns a name to a memory address that will store the location of an object instantiation sets aside memory for the objects data stores the address of the objects data in the named memory location

Diagram to show memory state program diagram (or object diagram) state-of-memory diagram uses similar notation to UML but includes extra symbols

Another instantiation example The state after two new commands are executed. allocating a new object means the address of the original is forgotten an object with no references to it is garbage

Methods and Messages Syntax for sending messages The expressions “sending a message” and “calling a method” are synonymous. The object that receives a message must possess a corresponding method. Example:

Program Components Comments Import Statement Class Declaration Method Declaration

Comments Comments are used to state the purpose of the program, explain the meaning of code, and provide other descriptions to help programmers use and understand the code. A single-line comment is preceeded by // A comment is any sequence of text that begins with the marker /* and ends with the marker */. A javadoc comment begins with /** and ends with */

Using Classes from Java Library In Java, classes are grouped into packages, and the Java system comes with numerous packages. To use a class from a package, we refer to the class in the program by using the following syntax: <package name>.<class name> This notation is called dot notation.

import statements Using the fully qualified name of a class can be cumbersome. Using the import statement solves this problem. import javax.swing.*; Now we can just use the name of the class

Class definition A Java program is composed of one or more classes. The syntax for declaring a class is A class member is either a data value or a method. Most classes will contain some of each

The main class One of the classes in the program must be designated as the main class. If we designate a class as a main class, we must define a method called main, because when a Java program is executed, the main method of a main class is executed first. The main class is sometimes called a driver class

Method Definition Syntax Example:

Template for Java Program

Edit-Compile-Run Cycle

Editing Step One: Edit the program. Type in the program, using a text editor, and save the program to a file. Use the name of the main class and the suffix .java for the filename. This file is called a source file.

Compiling Step 2: Compile the source file. The process of compiling the source file creates the bytecode file. the bytecode file is binary The name of the compiler-generated bytecode file will have the suffix .class while its prefix is the same as the source file’s. The class file will not be created if there are syntax errors in your program You'll get a list of error messages

Running the Program Step 3: Execute the bytecode file. A java interpreter will go through the bytecode file and execute the instructions in it. If your program is error-free, a window will appear on the screen. If an error occurs while running the program, the interpreter will catch it and stop its execution.