Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.

Slides:



Advertisements
Similar presentations
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Advertisements

Begin Java Pepper. Objectives What is a program? Learn the basics of your programming tool: BlueJ Write a first Java program and see it run Make some.
IT151: Introduction to Programming
JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine.
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.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
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.
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
Copyright 2013 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
1 Building Java Programs Chapter 1: Introduction to Java Programming These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may.
Editing Java programs with the BlueJ IDE. Working environments to develop (= write) programs There are 2 ways to develop (write) computer programs: 1.Using.
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.
Chapter 2 How to Compile and Execute a Simple Program.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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()
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
How to Run a Java Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
How to Run a Java Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 2: Java Fundamentals
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
Comments in Java. When you create a New Project in NetBeans, you'll notice that some text is greyed out, with lots of slashes and asterisks:
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.
Introduction to programming in the Java programming language.
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
Chapter 2 part #1 C++ Program Structure
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
1 WELCOME TO CIS 1068! Instructor: Alexander Yates.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
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,
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
© 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects 1. How is a class different from an object?
CSC 110 – Intro to Computing - Programming
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,
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Introduction to programming in java
Computer Programming Your First Java Program: HelloWorld.java.
The eclipse IDE IDE = “Integrated Development Environment”
CSC201: Computer Programming
Chapter 2, Part I Introduction to C Programming
Intro to Java.
Writing Methods.
How to Run a Java Program
Chapter 3 Classes and Objects
Tonga Institute of Higher Education
Java Tutotrial for [NLP-AI] 2
Java Intro.
How to Run a Java Program
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.
© A+ Computer Science - Basic Java © A+ Computer Science -
Chapter 2 part #1 C++ Program Structure
How to Run a Java Program
Presentation transcript:

Output in Java Hello World!

Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open and closing braces which indicate the start and end of the file. All contents of the Java file will be placed inside the braces.  The term class is a reserved Java keyword. Java knows exactly what this word means (which his why it appears in blue.  The name of the program is HelloWorld. This must match the name of the Java file itself!

The main() method  Like a maze needs a starting point, a Java program needs a place to begin its execution.  The main() method tells Java where the starting point of your program will be.  Without a main() method, your program won’t do anything! class HelloWorld { public static void main(String[] args) { // Code goes here. }

The main() method - Explained class HelloWorld { public static void main(String[] args) { // Code goes here. } Things to note:  The main() method is indented within the class. After an opening brace, all subsequent lines should be indented! The contents of the main() method make up the body.  Comments are lines that begin with two forward slashes (//). They are used to write notes within code that will not be executed by Java.

Output – System.out.println  Java provides two commands for output: System.out.print()&System.out.println()  Any text put inside of the parentheses will be printed to screen. This text must be in quotations.  The print() method simply prints the text to screen, whereas the println() method prints the text to screen and then moves the cursor to a new line. class HelloWorld { public static void main(String[] args) { // Prints Hello World to screen. System.out.println(“Hello World!”); }

Output – System.out.println - Explained class HelloWorld { public static void main(String[] args) { // Prints Hello World to screen. System.out.println(“Hello World!”); }  All statements in the main method are indented – this pattern will continue in all of your programs!  Write one statement per line! This makes your code easy to read.

Output – Example class OneLine { public static void main(String[] args) { System.out.print(“This text is on the same line…”); System.out.print(“…as this text”); }  The above code demonstrates how the print() method prints to the same line, whereas the println() method moves the cursor to a new line.

Special Characters  Java provides special codes, called escape sequences, which can be used to print certain spacing and characters in output statements.  The chart below shows some of these escape sequences: Escape Sequence Resulting Output \nMoves the cursor to a new line \tInserts a Tab (space) \\Inserts a backward slash “\” \’Inserts a single quotation \”Inserts a double quotation

Special Characters - Examples  The following code prints the numbers 1, 2, and 3 on their own lines in two different ways: using println() and using escape sequences class OneTwoThree { public static void main(String[] args) { // Print 1, 2, and 3 using println(). System.out.println(“1”); System.out.println(“2”); System.out.println(“3”); // Print 1, 2, and 3 using escape sequences. System.out.println(“1\n2\n3”); }

Some Useful Hints: Using NetBeans 1. Set Up NetBeans for Matching Braces Go to Tools -> Options. Navigate to the Formatting tab. Select Java from the Languages dropdown and Braces from the Category dropdown. Under Braces Placement, ensure that New Line is selected for each of the three categories (Class Declaration, Method Declaration, and Other: ). 2. Fix Your Formatting With One Click! To ensure that the format (spacing) of your program is correct, go to Source -> Format. If you notice that an opening brace does not have a matching closing brace, your program has formatting errors!

Some Useful Hints: Using NetBeans 3. Running a Different Program If you’ve written two programs in one project, NetBeans does not know which of the two main() methods to run. You will have to explicitly tell it as follows: Right-click on your Project and select Properties. Under the Run category, find the Main Class: field. Click Browse and choose the program you want to run.