Chapter 3 Intro to Java Introducing Java Barrett Computer Science Designing Technology Solutions 1
2 Key Terminology - OS Operating System
3 Why Program in Java? Java is an Object Oriented Programming (OOP) language. OOP programmers create programs, called classes, that can be used over and over again in/for a variety of other programs. The Java platform runs on all major operating systems (windows and Mac OS) thus allowing Java applications to run on just about any computer.
4 What A Java program consist of: Object Class Package
5 Example of Classes and Objects Lets look at an example The class defines the information contained in a Blue-ray movie database (e.g., Red Box) - title, genre, year, loan status and borrower name. The objects in this example will be specific entries.
6 So what does that mean?...(1 of 3) Classes define the type of data and actions, that will be associated with an object. Objects are the instructions for performing actions on a specific set of data. Note: A class can be used to create many objects.
7 So what does that mean?...(2 of 3) Note: A class can be used to create many objects.
8 So what does that mean?...(3 of 3) A Package is a group of related classes Packages are sometimes referred to as libraries.
9 A Java Application There are four main parts in a Java application: Statement instructions A semicolon is required to indicate the end of a statement. Related statements are enclosed by curly braces { } to begin and end the instructions.
10 A Java Application Controlling Class program starting point Public class Greeting is our class declaration. The class is available to anyone (public) and is named greeting.
11 A Java Application Method named set of statements performing a single well defined task. The main () method is placed in the controlling class. To define our main method we will use the following method declaration: Public static void main (String[] args){ } The main method s statements are automatically run when the program is executed.
12 A Java Application cont. Comments info provided to reader and has nothing to do with executing a program The three types of comments: 1) /* */ to enclose single or multiline comments 2) // are used for adding a comment to the end of a statement or to create a single line comment 3) /** */ are used for documentation.
13 Let s look at an Example HERE is an example of java code.. comment //The Greeting class displays a greeting class declaration public class Greeting{ public static void main(string[] args){ } System.out.println("Hello,world!"); } statement method
14 Making your first Java Project and class - Walk class through example using the LCD Projector. Dismiss students to make their first project and class Project Name: Bbarrett Chpater3 Class Name: HelloWorld Comments include: /* Name Date Period Class */
15 Java Terminology Java code typed by a programmer is called source code. For this code to execute it must be translated into code the computer understands. This process of translating is called compiling. Compiled java source code is called bytecode. The java Virtual Machine (JVM) interprets the bytecode.
16 Executing a Java Application source code public class Greeting public static void System.out.printl } } compiler...c cilbup bytecode 03 3b 84 01 ff f9 68 05 1a JVM...48 b3 30 Hello,world! This is your output
17 General Java Notes Programs are created to solve problems All Java Applications have at least one class The main method is the method that runs (executes) a program Platform Independence: This means that Java applications can be run on just about any computer no matter the brand
18 Displaying Output Output stream sends data to an output device To process data for the output stream, Java provides the System class with methods for displaying output Displaying data to the standard output stream (Typically this is the computer screen): You would Use System.out.
19 Displaying Output Examples of a type of output stream are: System.out.print() System.out.println() These methods require arguments in the () One type of arguments are strings Strings A set of characters, which are enclosed in quotation marks.
20 Escape Sequences Another example of output are called escape sequences escape sequence is a backslash (\) followed by a symbol that together represent a character. They are used to display special characters The 4 Commonly used escape sequences: 1. \n newline 2. \t tab (8 spaces) 3. \\ backslash 4. \" double quotation mark
Exercises (Chapter 3 pt. 1) 21
22 Exercises Exercise 3 (p 72) Save as Ch3Ex3 Exercise 7 (p 74) Save as Ch3Ex7 Both exercises will be saved in your chapter 3 project folder.
23 The Format Method CS, DTS
24 The format() Method The System.out.format() method is used to Control the way output is displayed.
25 The format() Method String A sequence comprised of characters. Ex: This sentence is a string. Numbers can be represented as strings or numerals. It all depends on how it is defined by the programmer, aka YOU!
26 The format() Method Example The format string specifier takes the form: %[alignment][width]s % - Indicate the start of a specifier Alignment means skip for right alignment You would use a minus sign (-) for left alignment Width the number of characters to use for output S indicates that the corresponding argument is a string An example. System.out.format("%-6s %4s", "Test1", "90"); Will display: Test1 90
Exercises (Chapter 3 pt. 2) 27
28 Exercises Exercise 4 (p 73) Save as Ch3Ex4 Exercise 5 (p 73) Save as Ch3Ex5 Both exercises will be saved in your chapter 3 project folder.