Lab 01-2 Objectives:  Writing a Java program.  How to send output to the command line console.  Learn about escape sequences.  Learn how to compile,

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

IT151: Introduction to Programming
© 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Your First Java Program: HelloWorld.java
How to Create a Java program CS115 Fall George Koutsogiannakis.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
 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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
GTECH 731 Lab Session 2 Lab 1 Review, Lab 2 Intro 9/6/10 Lab 1 Review Lab 2 Overview.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Copyright 2013 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Unit 2: Java Introduction to Programming 2.1 Initial Example.
“Introduction to Programming With Java”
Hello AP Computer Science!. What are some of the things that you have used computers for?
2.2 Information on Program Appearance and Printing.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
Welcome to the Lecture Series on “Introduction to Programming With Java”
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Program Statements Primitive Data Types and Strings.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2: Java Fundamentals
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
Getting Started.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
By Mr. Muhammad Pervez Akhtar
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
© 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects 1. How is a class different from an object?
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Chapter 2 1.What is the difference between print / println 2.What are String Literals 3.What are the Escape Characters for backslash, double quotataions,
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Lecture 4 – Scanner & Style
Introduction to programming in java
Computer Programming Your First Java Program: HelloWorld.java.
The eclipse IDE IDE = “Integrated Development Environment”
Console Output, Variables, Literals, and Introduction to Type
While Statement.
An Introduction to Java – Part I, language basics
Chapter 3 Classes and Objects
Java Intro.
CS110D Programming Language I
Anatomy of a Java Program
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming with BlueJ Objectives
Review of Previous Lesson
In this class, we will cover:
Loops CGS3416 Spring 2019 Lecture 7.
Instructor: Alexander Stoytchev
How to Run a Java Program
Presentation transcript:

Lab 01-2 Objectives:  Writing a Java program.  How to send output to the command line console.  Learn about escape sequences.  Learn how to compile, debug, and execute a Java program.

Console Output In Java, console output is achieved by calling System.out.print or System.out.println. The data to be output is given as an argument in parentheses. System.out.println(“Blackjack”); Every invocation of println ends a line of output. Blackjack _ Lab 01-3

println Versus print The print method is like println, except that it does not end a line With println, the next output goes on a new line With print, the next output goes on the same line Lab 01-4

System.out.print(“Hello ”); System.out.print(“World”); Lab 01-5 The print Method

System.out.print(“Hello ”); System.out.print(“World”); Output HelloWorld Lab 01-6 The print Method

System.out.print(“Hello ”); System.out.print(“World”); Lab 01-7 The print Method

System.out.print(“Hello ”); System.out.print(“World”); Output Hello World Lab 01-8 The print Method

System.out.println(“Hello”); System.out.println(“World”); Lab 01-9 The println Method

System.out.println(“Hello”); System.out.println(“World”); Output Hello World Lab The println Method

21 is a Blackjack 7 7 is lucky Lucky number 34 A plus sign is used to connect more than one item System.out.println(21 + “ is a Blackjack”); System.out.println(3 + 4); System.out.println( “ is lucky”); System.out.println(“Lucky number “ ); The + Symbol Lab 01-11

6 Error – the * symbol can not be used with Strings 7 is lucky Error – the – symbol can not be used with Strings 3 Be careful when using other math symbols. System.out.println(3 * 2); System.out.println(3 * “Hello World”); System.out.println(11 – 4 + “ is lucky”); System.out.println(“Lucky number “ ); System.out.println(15 / 4); Other Math Symbol Lab 01-12

Escape Sequences Escape sequences are used to print characters that are non-printable. Escape sequences always begin with a \ (backslash) character. Common Escape sequences \n – new line \” – quote symbol \t – tab \\ - backslash Lab 01-13

Escape Sequences (Cont…) Output Hello World Lab System.out.println(“Hello\nWorld”);

Escape Sequences (Cont…) System.out.println(“Hello\tWorld”); Lab Output HelloWorld

Escape Sequences (Cont…) System.out.println(“\”Hello\tWorld\””) Lab Output “HelloWorld”

Normally program statements execute from top to bottom. This is called sequential control. Sequential control is the default control structure. There are other control structures that we will discuss later. Lab Sequential Programming The order in which statements occur is important.

Sequential Programming (cont…) What will be output by the following program segment? System.out.print(“Chester ”); System.out.print(“Nimitz”); Lab Output Chester Nimitz

Sequential Programming (cont…) Now what will be output by the following program segment? System.out.print(“Chester\nNimitz”); Lab Output Chester Nimitz

Sequential Programming (cont…) Now what will be output by the following program segment? System.out.println(“Chester\\Nimitz”); Lab Output Chester\Nimitz

Sequential Programming (cont…) Keep in mind as you are programming that where you place a statement determines when it will be executed. SEQUENTIAL CONTROL Lab 01-21

String Concatenation System.out.print(“Hello ” ); Lab Here is a typical print statement.

String Concatenation (cont…) System.out.print(“Hello ” ); Lab “Hello 1”

System.out.print(“Hello ” ); String Concatenation (cont…) Lab “Hello 1” “Hello 12”

String Concatenation (cont…) System.out.print( “ Hello”); Lab Here is another typical print statement.

System.out.print( “ Hello”); String Concatenation (cont…) Lab

System.out.print( “ Hello”); String Concatenation (cont…) Lab “3 Hello”

String Concatenation (cont…) System.out.print(“Hello ” + 3 * 2); Lab Here is another typical print statement.

String Concatenation (cont…) System.out.print(“Hello ” + 3 * 2); Lab Multiplication has a higher precedence than addition! Multiplication has a higher precedence than addition!

String Concatenation (cont…) System.out.print(“Hello ” + 3 * 2); Lab

Sequential Programming (cont…) System.out.print(“Hello ” + 3 * 2); Lab “Hello 6”

String Concatenation (cont…) System.out.print(“Hello ” ); Lab Here is another typical print statement.

String Concatenation (cont…) System.out.print(“Hello ” ); Lab “Hello 6”

String Concatenation (cont…) System.out.print(“Hello ” ); Lab “Hello 6” Compile Time Error “operator - can not be applied to String, int” Compile Time Error “operator - can not be applied to String, int”

System.out.print(“Hello ” ); String Concatenation (cont…) Lab How can we “fix” this problem?

System.out.print(“Hello ” + (6 - 4)); String Concatenation (cont…) Lab Use parantheses!

System.out.print(“Hello ” + (6 - 4)); Sequential Programming (cont…) Lab

System.out.print(“Hello ” + (6 - 4)); Sequential Programming (cont…) Lab “Hello 2”

Lab  Start JCreator.  Create a new file called “Lab01.java”.  Save the new file in your Lab01 folder. Top Down Design

Lab Creating A Java Class File

Lab Creating A Java Class File (cont…)

Lab Creating A Java Class File (cont…)

Lab Creating A Java Class File (cont…)

Lab Creating A Java Class File (cont…)

Lab Creating A Java Class File (cont…) Class names should always begin with an uppercase letter. Class names should always begin with an uppercase letter.

Lab Creating A Java Class File (cont…) Click this button to set the location. Click this button to set the location.

Lab Creating A Java Class File (cont…)

Lab Creating A Java Class File (cont…)

public class Lab01 { } Lab Declaring A Java Class This class can be used by other classes! This class can be used by other classes!

public class Lab01 { } Lab Declaring A Java Class The keyword class defines this file as a Java class. The keyword class defines this file as a Java class.

public class Lab01 { } Lab Declaring A Java Class User defined name. Class names should always begin with an uppercase letter. User defined name. Class names should always begin with an uppercase letter.

public class Lab01 { } Lab Declaring A Java Class The body of every class begins and ends with a set of curly brackets. The body of every class begins and ends with a set of curly brackets.

Lab The main Method In Java, you need to have a method named main in at least one class. This method must appear within a class, but it can be any class. A class containing a main method is a program. Every program has a main method but not every class is a program.

public class Lab01 { public static void main(String[ ] args) { } Lab The main Method (cont…) Curly brackets are used to define block statements. Methods, like classes, are block statements and must begin and end with a set of curly brackets. Curly brackets are used to define block statements. Methods, like classes, are block statements and must begin and end with a set of curly brackets.

public class Lab01 { public static void main(String[ ] args) { } Lab The main Method (cont…) The main method is static.

public class Lab01 { public static void main(String[ ] args) { System.out.println(“Main Method!”); } Lab The main Method (cont…)

Lab Compiling A Java Class File

Lab Compiling A Java Class File

Lab Running A Java Program

Lab Running A Java Program

public class Lab01 { public static void main(String[ ] args) { System.out.println(“Main Method!”); } public void output() { System.out.println(“Hello World”); } Lab The main Method (cont…) output is not static

public class Lab01 { public static void main(String[ ] args) { System.out.println(“Main Method!”); } public void output() { System.out.println(“Hello World”); } Lab The main Method (cont…) Run the program

Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { System.out.println(“Main Method!”); } public void output() { System.out.println(“Hello World”); } Lab The main Method (cont…) output did not execute

public class Lab01 { public static void main(String[ ] args) { System.out.println(“Main Method!”); } public void output() { System.out.println(“Hello World”); } Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { } public void output() { System.out.println(“Hello World”); } Lab The main Method (cont…) How do we get from here

public class Lab01 { public static void main(String[ ] args) { } public void output() { System.out.println(“Hello World”); } Lab The main Method (cont…) How do we get from here to there

public class Lab01 { public static void main(String[ ] args) { output(); } public void output() { System.out.println(“Hello World”); } Lab The main Method (cont…) A method call to output

Lab The main Method (cont…)

Lab The main Method (cont…)

Lab The main Method (cont…) error: non-static method output() cannot be referenced from a static context

Lab The main Method (cont…) Double click on the first line of the error.

Lab The main Method (cont…) The editor will send the cursor to the line that contains the error.

Static methods are class methods. Non-static methods are instance methods. Class methods can not call instance methods – however instance methods can call class methods. More on this topic later. To be able to call output we need an instance of the class Lab01. Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { output(); } public void output() { System.out.println(“Hello World”); } Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); } public void output() { System.out.println(“Hello World”); } Lab The main Method (cont…) lab is an object. An object is an instance of a class.

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println(“Hello World”); } Lab The main Method (cont…) We can call output using lab which is an instance of the class Lab01

Lab Run The Program

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println(“Hello\nWorld”); } Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println(“Hello\\World”); } Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println(“Hello World” ); } Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println( “Hello World”); } Lab The main Method (cont…)

Control Structures Lab 01-85

Sequential Control Branching Conditions Looping Lab Control Structures

How The Program Executes. Lab 01-87

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println( “Hello World”); } Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println( “Hello World”); } Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println( “Hello World”); } Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println( “Hello World”); } Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println( “Hello World”); } Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println( “Hello World”); } Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println( “Hello World”); } Lab The main Method (cont…)

public class Lab01 { public static void main(String[ ] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println( “Hello World”); } Lab The main Method (cont…)

Refers to the order in which the individual statements of a program are executed or evaluated Lab Control Structures

Lab Sequential (Default) Branching Conditional Repetition (looping) Control Structures

Sequential Control: Statements are executed in the order in which they are written. Lab Control Structures

System.out.println(“ABC”); System.out.println(“DEF”); Lab Control Structures

System.out.println(“ABC”); System.out.println(“DEF”); Lab Output: ABC DEF Control Structures

Branching: Allows the flow of execution to jump to a different part of the program. Lab Control Structures

public static void main(String[] args) { Lab01 lab = new Lab01(); lab.output(); } public void output() { System.out.println(“Hello World”); } Lab Control Structures

public void methodA() { methodB(); methodC(); } public void methodB() { System.out.print(“Hello ”); } public void methodC() { System.out.print(“World”); } Lab Control Structures

Questions? Lab