Introducing Java Chapter 3. Why Program in Java? 0 Java was developed by Sun Microsystems. It is a widely used high-level programming language. 0 One.

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
IT151: Introduction to Programming
University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.
JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. GCOC – A.P. Computer Science A College Board Computer Science A Topics Covered Program Design - Read and understand.
© 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects.
Chapter 1: Introduction
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
Introducing Java.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 1: Creating Java Programs
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Chapter 1 Introduction. Goal to learn about computers and programming to compile and run your first Java program to recognize compile-time and run-time.
Chapter 1 CSIS-120: Java Intro. What is Programming?  A: It is what makes computer so useful.  The flexibility of a computer is amazing  Write a term.
The Java Programming Language
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
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.
Board Activity Find your seat on the seating chart Login – Remember your login is your first initial your last name and the last three numbers of your.
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
By Mr. Muhammad Pervez Akhtar
Creating a Java Application and Applet
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
© 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects 1. How is a class different from an object?
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.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Introduction to Object Oriented
Chapter 1 – Introduction
Chapter 6 JavaScript: Introduction to Scripting
Working with Java.
CSC201: Computer Programming
Chapter 1 Introduction to Computers, Programs, and Java
GC101 Introduction to computer and program
CSCI-235 Micro-Computer Applications
Console Output, Variables, Literals, and Introduction to Type
Chapter 2, Part I Introduction to C Programming
Data types and variables
Introduction to Scripting
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data.
Introduction Java Chapter 3.
C# Programming: From Problem Analysis to Program Design
Chapter 2 – Getting Started
Programming Vocabulary.
String Output ICS 111: Introduction to Computer Science I
Chapter 3 Classes and Objects
Chapter 1: Computer Systems
Fundamentals of Programming
Creating your first C program
Chapter 1 Introduction(1.1)
Anatomy of a Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Computer Programming-1 CSC 111
Presentation transcript:

Introducing Java Chapter 3

Why Program in Java? 0 Java was developed by Sun Microsystems. It is a widely used high-level programming language. 0 One reason for its wide-spread use is the ability to create platform-independent applications. 0 Java is an object-oriented programming language. 0 Object-oriented programming (OOP) evolved out of the need to develop complex programs in a systematic, modular approach. 0 OOP programmers create modules, called classes, that can be used over an over again in a variety of programs.

Programming in Java 0 A class groups related data and the instructions for performing actions on that data. 0 Properly designed, classes encapsulate data to hide implementation details, are versatile enough to be extended through inheritance, and give the programmer options through polymorphism. 0 Encapsulation, inheritance, and polymorphism are features of every object-oriented language.

Java is a Platform 0 Java is more than just a programming language, it is a platform. 0 A platform can be a combination of hardware and software like an operating system, but the Java platform is software only and is used to run Java applications. 0 Java applications are therefore referred to as platform-independent applications. 0 The OOP and platform-independence features of Java have allowed programmer to create programs for the masses.

Objects and Classes 0 Object-oriented program development involves selecting objects to perform the tasks outlined in a program specification. 0 An object consists of related data and the instructions for performing actions on that data. 0 The design for an object is called a class. A class defines the type of data and actions that will be associated with an object of that class, but not the actual data for an individual object. 0 Classes are required to created objects 0 Many objects of the same class may be needed in an application.

Objects and Classes DVD class title genre year onLoan LoantoFriend() Shrek comedy 2001 onLoan to Jim Miracle drama 2004 Spider-Man 2 action 2004 Freaky Friday comedy 2003 ClassObjects A class can be used to create many objects--each object will have the same type of data and possible actions, but each object maintains its own set of data.

Packages 0 A package, sometimes called a library, is a group of related classes. 0 For example, classes related to a particular task, such as input and output, can be grouped together in a package. media package DVD class title genre year onLoan LoantoFriend() CD class title genre year onLoan LoantoFriend() A package contains related classes.

Packages 0 An application is a type of package. It contains a controlling class and can contain other classes as well. 0 Packages are importable, which means an application can use existing classes from another package. circleTestpackage CircleDemoclass main() Circle class radius area() An application is contained in a package.

A Java Application 0 A Java application is a package with at least one class that contains a main method(). 0 A program consists of a set of instructions called statements. A semicolon is required to indicate the end of a statement. Related statements are enclosed in curly braces ({ }) to begin and end the instructions. 0 package firstApplication declares a package name. If a package is not declared, then the application is given the package name Default by the compiler. Simple applications often do not have package statements. 0 public class Greeting is a class declaration. The class is available to anyone ( public ) and is named Greeting. The Greeting class does not define any data or actions for objects. However, it contains the main() method making it the application’s controlling class. The controlling class is a program’s starting point. /* *Greeting.java *A first Java application *A student *Course *Date */ package firstApplication; /** *The Greeting class displays a greeting. */ public class Greeting { //start class definition public static void main(String[] args) { System.out.println(“Hello, world!”); } }//end class definition Hello, world!

Methods 0 public static void main(String[ ] args) defines the main() method. A method is a named set of statements that perform a single, well- defined task. 0 The main() method is placed in the controlling class. Its statements are automatically run when the program is executed. The statement in this main() method uses the println() method to display “Hello, world!” to the system output (the screen).

Comments 0 In addition to statements, programs contain comments, which provide information about the program to the reader of the code. 0 Comments have no effect on the program, but they allow a reader of the program to quickly understand the purpose and logic behind segments of code. 0 Because complex applications are often developed and maintained by more than one programmer, properly commented code allows for easier modifications and can decrease the number of mistakes.

Types of Comments 0 Java programs can contain three types of comments: 1. /**/ are used to enclose single or multiline comments. These comments are appropriate at the beginning of a program to describe the application and where necessary to clarify a segment of code. 2. // are sued for adding a comment to the end of a statement or to create a single line comment. The // is also useful for debugging a program. 3. /***/ are used for documentation. The javadoc tool copies these comments into a separate HTML document to create an instructional manual or external documentation. Documentation comments are appropriate for describing a class or method. 0 Multiline comments that describe a program, class, or method are sometimes referred to as a comment block. Tips throughout the text will provide additional pointers about commenting code.

Executing a Java Application 0 Java code typed by a programmer is called source code. For source code to execute, or run, it must be translated to code the computer understands in a process called compiling. 0 Many Java compilers provide an environment for entering source code as well as for compiling and running an application. Source code files have the extension.java, while compiled files have the extension.class. 0 Compiled Java source code is called bytecode. Executing a Java application means that its bytecode is interpreted with the Java Virtual Machine (Java VM). The interpreter runs each bytecode instructiion as it is read. 0 The Java VM can reside on any computer, regardless of operating system environment, making Java applications extremely portable, reliable, and platform-independent.

Executing a Java Application 0 Although bytecode is versatile for porting applications to several different environments, programs compiled to machine code run faster. 0 Machine code is comprised of just 1s and 0s and is different depending on the computer platform. A just-in-time compiler (JIT) converts bytecode to machine code before it is executed with the Java VM. 0 A program containing syntax errors will not compile. A syntax error occurs in a statement that violates the rules of Java. For example, all Java statements must end with a semicolon (;). A statement without a semicolon generates a syntax error when the program is compiled, preventing the compiler from generating bytecode for the application.

Displaying Output 0 An output stream sends data to an output device. 0 To process data for the output stream, Java provides the System class with methods for displaying output. 0 Typically output is displayed on the computer screen with using System.out. out contains print() and println() methods. 0 The difference between these methods is how they control output. 0 The print() method displays data and leaves the insertion point at the end of the output, while println() moves the insertion point to the next line after displaying the output. /** *Displays a welcome message */ public class MulticulturalGreeting { public static void main(String[] args) { System.out.print(“Hello”); System.out.print(“Buenos Dias”); System.out.println(“Bonjour”); System.out.println(“How do you greet another?”); } HelloBuenos DiasBonjour How do you greet another?

Displaying Output 0 The print() and println() methods require arguments. An argument is data passed to a method for processing. In this case, the print() and println() arguments are strings. 0 A string is a set of characters, which are enclosed by quotation marks. 0 An escape sequence is a backslash (\) followed by a symbol that together represent a character. Escape sequences are used to display special characters. Common escape sequences include: 0 \n new line 0 \t tab (8 spaces) 0 \\ backslash 0 \” double quotation marks /** *Displays a welcome message */ public class MulticulturalGreeting { public static void main(String[] args) { System.out.print(“Hello\t”); System.out.print(“Buenos Dias\t”); System.out.println(“Bonjour”); System.out.println(“How do you greet another?”); } HelloBuenos DiasBonjour How do you greet another?

Formatting Output 0 The format() method can e used in place of the print() or println() methods to control the way output is displayed. 0 The format() method arguments include a format string and an argument list. 0 The format() string contains specifiers that indicate how the corresponding strings in the argument list should be displayed. 0 A format string specifier takes the form: %[alignment][width]s % indicates the start of a specifier [alignment] skip for right alignment; include a minus sign (-) for left alignment [width] the number of characters to use for output s indicates that the corresponding argument is a string System.out.format(“%-10s %8s %8s”, “Team”, “Wins”, “Losses\n”); System.out.format(“%-10s %8s %8s”, Jaguars”, “10”, “5\n”); TeamWinsLosses Jaguars105

Formatting Output 0 If [width] is greater than the number of characters in the corresponding string argument, then spaces pad the output. 0 A string longer than [width] characters is displayed, but any strings to the right are moved over. 0 Text may also be included within the format string. For example, the statement: System.out.format(“The final game score: %-8s %8s”, “10”, “5”); The final game score:105

Code Conventions 0 Code conventions are a set of guidelines for writing an application. These guidelines provide details about commenting, rules for naming methods, classes, and packages, and statement formatting. 0 Just as comments inform a reader about a segment of code, a program that follows specific code conventions is easier to read and understand. 0 A company or organization that employs programmers will typically adhere to specific code conventions. 0 Code conventions can make modifying and maintaining code faster, easier, and less expensive. 0 Because of these benefits, organizations often not only encourage the use of code conventions, but require it.

Common Code Conventions 0 An introductory comment should begin a program. This comment should include information such as your name, class name, the date, and a brief statement about the program. 0 Package names should begin with a lowercase letter and then an uppercase letter should begin each word within the name. Package names may not contains spaces. 0 Class names should be nouns and begin with an uppercase letter and an uppercase letter should begin each word within the name. Class names may not contain spaces. 0 A comment block should be included before each class and method. A comment block is not typically placed before the main() method. 0 Comments should not reiterate what is clear from the code. 0 Statements in a method should be indented. 0 An open curly brace ({ ) should be placed on the same line as the class or method declaration (can be placed on the line below though), and the closing curly brace ( }) should be on a separate line and aligned with the class or method declaration.

Algorithm Design 0 Programs are created to solve problems. 0 Problems of any complexity require outlining, or designing, a solution before typing any source code. 0 One method of design is called an algorithm. An algorithm is a set of steps that outline how to solve a problem. 0 Algorithms can e created in several ways. Often more than one approach is implemented. 0 Steps written out in plain English is one approach. 0 Pseudocode is a mix of English and program code, and is useful for refining a plain English algorithm. 0 Flowcharts use symbols and text to give a visual representation of a solution. Indicates start or end Indicates input/output