Presentation

Download Report

Transcript Presentation

Chapter 2
Java Basics
Mrs. Ulshafer
August 2013
Java
- Object-Oriented Language
- style of programming in which
programs are constructed using classes
- True Java needs 2 classes
• - Test - run
• - Class- describes objects
Objects - Have a description and behvaiors
- Knows and does things
Class
- A blueprint for how objects are created
Recipe
- All java code is defined
Vs
Cookies
- Construct objects
Bank Account Example
Beginning of each Chapter
• Create a new folder in your U drive with
the chapter #
Project
• Double click on Netbeans
• File > New Project
• Select Java Application
• Name Project: ch#Ex# (ch1Ex1)
•Be sure to save to your U: drive
Comments – in grey
/*
*/ - multi-line comment
// - follows statements
- Used to clarify and explain what is
going on within your statements or
program.
- Ignored by compiler
Packages-
- Groups of classes
- Very organized
- Must be the first line in a source
file
- Can only be 1 in each source file
- Java created or programmer
created
Same
Control
Easy
named
Access
Searching
classes
Library
a collection of program code that can
be included in a program to perform a
variety of tasks
Collection of packages
Public Class Main{
}
- Every application needs:
- at least 1 class
- 1 main method
This is main class that holds the main
method
Everyone can see the new class named
main
Public static void ….
- Creates method called main
- can only have 1 main method in an
application
- runs the program!
- Methods- instructions between { }
- what objects do behavior
- set of statements to accomplish a
task
System.out.prinln(“ “);
System is a class regarding system resources
Out represents the console window- object
Println- is the method
Could use print
Difference is the ending place of cursor
(“ “) - what you want to print
String
- sequence of charactors encloseed in “ “
- will print EXACTLY what is between “ “
- Not necessary for numbers
Dot .
Used to call methods
Semicolons ;
- ends statements
- where most errors occur
- Must have at end of each line of code
StatementsA line of code
Syntax Errors
Statements that violate Java rules
• Complete the Error Worksheet
• Edit the HelloWorld Program to look for
errors
•Click Run > Build Main Project
• Copy the error message and return
program to original text
\* My Program
Mrs U.
Feb 3, 2006
package hello;
public class main{
Public static void Main (String [] Args){
System.OUT.println(“Hello there!)
}
Concatenation
- Joining strings and numbers
- Use +
Displaying Numbers
- requires concatentation or new
statement
- No quotations
Calculations
- already defined
- Should be in parentheses
Operations
•+
Addition
•-
Subtraction
•*
Multiplication
•/
Division
• Go to File > New Project
• Be sure java and java application is
selected
•Click Next
•Project name: circlearea
•Write code for printout on page 2-5
Computer should be doing the
operations, you dont just print
out the answer!
• Displaying Special Characters
• Escape Character
• - a special character that is used to escape
from the normal interpretation
•
Backslash /
• Escape Sequence
• Combination of escape characters and
symbols
•Examples
• \n
go to the next line
• \”
double quotation marks
•\‘
single quotation marks
• \\
single backslash
• \t
Tab (moves 8 spaces)
• Sailboat Program
• See page 2-7 in Textbook
• illustrates use of escape sequences
Use print with \n
Or use println by itself
Programming Styles
Free Form Language
- spaces and blank lines have no real
effect
Style Guidelines
- 1 statement per line
- equal number of spaces when
indenting
Style Guidelines
- 1 statement per line
- equal number of spaces when indenting
(Java does this for you)
- comments at beginning to describe
program, author, and date
- blank lines set off sections of code
- comments within the body to clarify