Chapter 1 Introduction to Java Software Solutions
Page 1
Focus of the Course
- Object-Oriented Software Development
- Problem Solving
- Program Design, Implementation, and Testing
- Object-Oriented Concepts:
- Classes
- Objects
- Encapsulation
- Inheritance
- Polymorphism
- The Java Programming Language
Page 2
Introduction
- Chapter 1 Introduction focuses on:
- Programming and Programming Languages
- An introduction to Java
- Overview of Object-Oriented Concepts
Outline
- The Java Programming Language
- Program Development
- Object-Oriented Programming
Page 3
Java
- Created by Sun Microsystems, Inc. and introduced in 1995.
- Popularity has rapidly grown since its introduction.
- Definition: A programming language specifies the words and symbols used to write a program.
- Rules: A programming language includes rules for assembling these words and symbols into valid statements.
Java Program Structure
- Components of a Java program:
- One or more classes
- Each class contains one or more methods
- Each method contains program statements
- Essential method:
main(entry point of Java applications).
Page 4
Example: Lincoln.java
// Lincoln.java
// Author: Lewis/Loftus
// Demonstrates the basic structure of a Java application.
public class Lincoln {
public static void main (String[] args) {
System.out.println ("A quote by Abraham Lincoln:");
System.out.println ("Whatever you are, be a good one.");
}
}
Output:
A quote by Abraham Lincoln:
Whatever you are, be a good one.
Page 5
Java Program Structure
- Basic structure of a class and method:
public class MyProgram {
} // class header
// comments about the class
public static void main (String[] args) {
} // method header
// comments about the method
- Comments can be placed across the program for clarity.
Page 6
Comments
- Essential for explaining program purpose and steps.
- Java comment types:
//- Comment to end of the line/* ... */- Multi-line comment/** ... */- Javadoc comment
Identifiers
- Words in a program made of:
- Letters, digits, underscore (_), dollar sign ($)
- Cannot start with a digit
- Java is case-sensitive:
Total,total, andTOTALare distinct. - Common identifier conventions:
- Title case for classes (e.g.,
Lincoln) - Upper case for constants (e.g.,
MAXIMUM)
Page 7
Identifiers (Continued)
- Choices for identifiers are either:
- Defined by the programmer (e.g.,
Lincoln) - From other programmers’ code (e.g.,
println) - Reserved Words: Predefined meanings that cannot be redefined.
- Examples include:
abstract,boolean,class,if,return, etc.
Page 8
White Space
- Includes spaces, blank lines, and tabs.
- Used to separate words and symbols; extra white space is ignored.
- Java programs can be formatted in various ways, but consistent indentation helps readability.
Quick Check
Valid Java Identifiers:
grade- ValidquizGrade- ValidNetworkConnection- Validframe2- Valid3rdTestScore- Invalid (cannot begin with a digit)MAXIMUM- ValidMIN_CAPACITY- Validstudent#- Invalid (cannot contain#)Shelves1&2- Invalid (cannot contain&)
Page 9
Program Development
- Involves several key activities:
- Writing the program in a specific language (e.g., Java)
- Translating into an executable form
- Investigating and fixing various types of errors.
- Software tools assist throughout this process
Page 10
Language Levels
- Four programming language levels:
- Machine language
- Assembly language
- High-level language
- Fourth-generation language
- Each CPU has a specific machine language; higher-level languages make it easier for humans to program.
Programming Languages
- Each CPU executes a specific machine language requiring translation of programs written in high-level languages (like Java).
- Compiler: A tool that translates source code into machine language.
Page 11
Java Translation
- The Java compiler translates source code into bytecode, which is:
- An intermediate representation, not tied to traditional CPU machine language.
- Executed by the Java Virtual Machine (JVM).
- Java's architecture-neutral design allows it to run on various machines.
Page 12
Development Environments
- Java software development supported by various tools, including:
- Java Development Kit (JDK)
- Eclipse
- NetBeans
- IntelliJ
- BlueJ
- jGRASP
- Basic compilation and execution processes are consistent across these environments.
Syntax and Semantics
- Syntax rules: Define valid program structure.
- Semantics: Define the meaning of a program statement.
- A program may be syntactically correct but semantically incorrect (logical errors).
Page 13
Errors
- Three types of errors:
- Syntax Errors: Detected by the compiler, prevent executable creation.
- Run-time Errors: Occur during execution (e.g., divide by zero).
- Logical Errors: Program runs but produces incorrect results.
Basic Program Development Steps
- Edit and save the program
- Compile the program
- Execute and evaluate results
Page 14
Problem Solving
- Purpose of writing a program is to solve a problem, involving:
- Understanding the problem
- Designing a solution
- Constructing and refining solutions
- Implementing the solution
- Testing it
- Activities overlap and are interactive rather than linear.
Page 15
Problem Solving
- Break down the solution into manageable pieces.
- The object-oriented approach promotes decomposition into objects and classes.
Object-Oriented Programming
- Java is an object-oriented programming language.
- Objects: Fundamental entities representing real-world entities (e.g., employee).
Page 16
Objects
- Objects defined by:
- State: Characteristics (e.g., bank account balance)
- Behaviors: Actions performed (e.g., deposits, withdrawals)
- Behaviors can alter the object's state.
Classes
- Classes serve as blueprints for creating objects.
- Methods within classes define behaviors.
- One class defines multiple objects that follow the same structure.
Page 17
Class = Blueprint
- Example: A single class can represent multiple bank accounts.
- Bank Account Class: Represents the concept.
- Objects from this Class: Different instances:
- John’s Bank Account
- Bill’s Bank Account
- Mary’s Bank Account
Page 18
Inheritance
- Classes can be derived from other classes through inheritance.
- Hierarchical organization of classes can enhance code reusability and structure.
Summary
- Chapter 1 overview focused on:
- Programming and programming languages
- Introduction to Java
- An overview of object-oriented concepts