© A+ Computer Science - www.apluscompsci.com Basic Java © A+ Computer Science - www.apluscompsci.com.

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

IT151: Introduction to Programming
© A+ Computer Science - public class CompSci { } All Java programs start with a class.
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.
Chapter 1 Introduction to JAVA. Why Learn JAVA? Java is one of the fastest growing programming language in the world. Java is one of the fastest growing.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
© A+ Computer Science - public class CompSci { } All Java programs start with a class.
CMT Programming Software Applications
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
 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.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
CS 106 Introduction to Computer Science I 09 / 11 / 2006 Instructor: Michael Eckmann.
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:
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
2.2 Information on Program Appearance and Printing.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
3 Major Steps for Creating/Running a Java Program You write the source code for the program and save it as a.java file. You compile the.java program using.
Java Syntax and Output Java Part 3. public class CompSci { } All Java programs start with a class.
Chapter 2: Java Fundamentals
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
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
CSC Programming I Lecture 6 September 4, 2002.
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
ECE 122 Feb. 1, Introduction to Eclipse Java Statements Declaration Assignment Method calls.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
1 WELCOME TO CIS 1068! Instructor: Alexander Yates.
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.
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
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,
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
© A+ Computer Science - public class CompSci { } All Java programs start with a class.
Introducing Java Chapter 3 Review. Why Program in Java? Java, is an object-oriented programming language. OOP languages evolved out of the need to better.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CSC 110 – Intro to Computing - Programming
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
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
Computer Programming Your First Java Program: HelloWorld.java.
C++ First Steps.
Lecture 2a- Java Fundamentals
CSC201: Computer Programming
Chapter 1 Introduction to Computers, Programs, and Java
Console Output, Variables, Literals, and Introduction to Type
Chapter 2, Part I Introduction to C Programming
Intro to Java.
Chapter 2 - Introduction to Java Applications
Chapter 3 Classes and Objects
MSIS 655 Advanced Business Applications Programming
Java Tutotrial for [NLP-AI] 2
Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes
elementary programming
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Review of Previous Lesson
Unit 3: Variables in Java
Output Manipulation.
Instructor: Alexander Stoytchev
How to Run a Java Program
Presentation transcript:

© A+ Computer Science - www.apluscompsci.com Basic Java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com A Simple Class public class AplusCompSci { } This is a very simple Java class. The name of the class is AplusCompSci. All Java programs start with a class. Pieces are added to the class to make a complete program. All Java programs start with a class. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com A Simple Class + main public class AplusCompSci { public static void main(String[] args) System.out.println("Aplus Comp Sci!"); } This AplusCompSci class is a bit more sophisticated than the previous one. This AplusCompSci class has a single method named main. The main method is typically used to test the class in which it is contained. For this particular example, the main method contains a statement that prints out Aplus Comp Sci! OUTPUT Aplus Comp Sci! © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Syntax Rules public class AplusCompSci { //open brace public static void main(String[] args) { System.out.println("Aplus Comp Sci!"); } } //close brace Java requires that all classes and methods have an open brace { and a close brace }. Braces come in pairs; thus, every open { brace must have a matching close } brace. Braces are used to indicate the beginning of a code block and the ending of a code block. Program statements are placed inside of the code blocks starting after the open brace and ending before the close brace. Braces – You gotta have ‘em! Every class and every method must have a { and a } . © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Syntax Rules public class AplusCompSci { public static void main(String[] args) System.out.println("Aplus Comp Sci!"); } In Java, all program statements are terminated with a semi-colon ; . System.out.println() is a program statement and must be terminated with a ; . public class CompSci is a class declaration not a program statement; as a result, there is no terminating ; . You must put a semi-colon at the end of all Java program statements ( ; ). © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Syntax Rules Never put a ; before an open { brace ;{ //illegal }; //legal The rule above simply states that you should never place a semi-colon before an open brace { . Following this rule will cut down on syntax errors. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Indentation public class AplusCompSci { public static void main(String[] args) System.out.println("Aplus Comp Sci!"); } Indent all code 3 spaces to make it easier to read. Indentation and spacing is not required. Java will allow entire programs to be written on a single line, but this style is strongly discouraged. Indenting code statements 3 spaces is a good style to indicate that the statements are inside of a particular block of code. System.out.println() is inside of method main; thus, it is indented 3 spaces to make this visibly clear. The main method is inside of class CompSci which is why it is indented 3 spaces. 123 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com apluscompsci.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Basic Java Output © A+ Computer Science - www.apluscompsci.com

frequently used methods © A+ Computer Science - www.apluscompsci.com System.out frequently used methods Name Use print(x) print x and stay on the current line println(x) print x and move to next line down printf(s,x) print x according to s specifications The chart above lists the most commonly used System.out methods. This chart is a great reference when preparing for quizzes and tests and when working on lab assignments. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Basic Java Output reference command / method System.out.print("aplus compsci"); OUTPUT aplus compsci System is a class that contains a reference named out. out is a static reference to a PrintStream. out can be used via methods print, println, and printf to display values on the console window. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Basic Java Output System.out.print("aplus compsci"); OUTPUT aplus compsciaplus compsci print is a method used to print values on the console window. print will print a value and remain on the same line as the value printed. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Basic Java Output System.out.println("aplus compsci"); OUTPUT aplus compsci println is a method used to print values on the console window. println will print a value on the current output line and then move down to the next line. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Basic Java Output System.out.println("aplus compsci"); OUTPUT aplus compsci aplus compsci println is a method used to print values on the console window. println will print a value on the current output line and then move down to the next line. This examples shows that aplus compsci is printed on the first line and then aplus compsci is printed on the second line. This output occurs because both output commands use println. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com one.java © A+ Computer Science - www.apluscompsci.com

Intermediate Java Output © A+ Computer Science - www.apluscompsci.com Escape Sequences \n newline \t tab \r carriage return \b backspace System.out.println("aplusc\tompsci"); \n, \t, \r, and \b are common escape sequences used with print, println, and printf . \t is used to tab over five spaces. \n is used to move the cursor down to the next line. \r is used to move the cursor to the beginning of the current output line. \b is used to backspace one place on the current line. OUTPUT aplusc ompsci © A+ Computer Science - www.apluscompsci.com

Intermediate Java Output © A+ Computer Science - www.apluscompsci.com Escape Sequences \n newline \t tab \r carriage return \b backspace System.out.println("apluscom\tpsci"); \n, \t, \r, and \b are common escape sequences used with print, println, and printf . \t is used to tab over five spaces. \n is used to move the cursor down to the next line. \r is used to move the cursor to the beginning of the current output line. \b is used to backspace one place on the current line. OUTPUT apluscom psci © A+ Computer Science - www.apluscompsci.com

Intermediate Java Output © A+ Computer Science - www.apluscompsci.com Escape Sequences \n newline \t tab \r carriage return \b backspace System.out.println("apluscomp\nsci"); \n, \t, \r, and \b are common escape sequences used with print, println, and printf . \t is used to tab over five spaces. \n is used to move the cursor down to the next line. \r is used to move the cursor to the beginning of the current output line. \b is used to backspace one place on the current line. OUTPUT apluscomp sci © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com two.java © A+ Computer Science - www.apluscompsci.com

Intermediate Java Output © A+ Computer Science - www.apluscompsci.com Escape Sequences \\ outs \ \" outs " \’ outs ’ System.out.println("aplus\\compsci\"/"); \\, \", and \' are common escape sequences used with to print out a \, ', and a ". \\ is used to print out a single \. \" is used to print out a single ". \' is used to print out a single '. OUTPUT aplus\compsci"/ © A+ Computer Science - www.apluscompsci.com

Intermediate Java Output © A+ Computer Science - www.apluscompsci.com Escape Sequences \\ outs \ \" outs " \’ outs ’ System.out.println("aplus\\'comp\'sci\'/"); \\, \", and \' are common escape sequences used with to print out a \, ', and a ". \\ is used to print out a single \. \" is used to print out a single ". \' is used to print out a single '. OUTPUT aplus\'comp'sci'/ © A+ Computer Science - www.apluscompsci.com

Escape Sequences frequently used combinations Name Use \t tabs over five spaces \n moves to front of next line \b deletes previous character \r moves to front of current line \\ nets one backslash \ \" nets one double quote " \’ nets one single quote ’ This chart lists the most commonly used escape sequences. This chart should be a great reference point when working on labs and when preparing for quizzes and tests. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Basic Java Comments // single-line comments /* */ block comments //this line prints stuff on the screen System.out.println("aplus cs"); Comments are used to add clarity and descriptions to code. When properly placed, comments can add quite a bit of readability to a piece of code. // is a single line comment used for a single line. /* */ is used when multiple comment lines are needed. Comments are also very useful to isolate a section of code when testing/debugging. It is very handy to comment off a section of code in order to the test the remaining code. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Basic Java Comments // single-line comments /* */ block comments /* this line prints stuff on the screen */ System.out.println("aplus cs"); Comments are used to add clarity and descriptions to code. When properly placed, comments can add quite a bit of readability to a piece of code. // is a single line comment used for a single line. /* */ is used when multiple comment lines are needed. Comments are also very useful to isolate a section of code when testing/debugging. It is very handy to comment off a section of code in order to the test the remaining code. © A+ Computer Science - www.apluscompsci.com

Intermediate Java Output © A+ Computer Science - www.apluscompsci.com System.out.printf("%s","apluscs\n"); OUTPUT apluscs printf is a method used to print values on the console window. printf can be used to set the number of decimal when printing a decimal number. printf can be used to print any type of data. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com three.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Advanced Java Output System.out.println( 7 + 8 + 9 ); OUTPUT 24 This example just adds up the numbers and prints out the sum which is 24. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Advanced Java Output System.out.println( 7 + " " + 8 + 9 ); OUTPUT 7 89 This one prints 7 and then encounters quotes with a space in between. Double quotes indicate a string. Anything encountered after a string is converted to a string. This code would print 7 89. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Advanced Java Output System.out.println( 7 + 8 + " " + 9 ); OUTPUT 15 9 This example is similar to the prior 2 examples. 7 and 8 are added together before the double quotes are encountered. 15 is output first. 9 is added to the 15 and space but not added via math as the double quote string in encountered first. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com four.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Programming Errors Syntax errors occur when you type something in wrong, causing the code to not compile. //missing semicolon - ; expected System.out.println("aplus cs") //case problem – should be System system.out.println("aplus cs") © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Programming Errors Runtime errors occur when something goes wrong while the program is running. //an out of bounds exception is thrown String s = "runtime_error"; System.out.println( s.charAt(15) ); © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com five.java © A+ Computer Science - www.apluscompsci.com

Work on Programs! Crank Some Code! © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Basic Java © A+ Computer Science - www.apluscompsci.com