Introduction to Java programming

Introduction to Java programming

02/19/2020 Review of Previous Lesson State as many Vocabulary words and Learning Objectives that you remember from the last lesson as you can. Remember to grade yourself from 0 - 3. 1 02/19/2020 How to Compile & Run your First Java Program 2

02/19/2020 Vocabulary Content: Start End describe compile run program 3 02/19/2020

Learning Objectives Content: Describe how to compile and run a Java program. Start End 4 02/19/2020 ESSENTIAL KNOWLEDGE System.out.println displays information on the computer monitor. 5

Language Features and other 02/19/2020 Testable Topics Tested in the AP CS A Exam Notes Not tested in the AP CS A Exam, but potentially relevant/useful Comments // Input / Output System.out.println Methods visibility (public), static Classes visibility (public), create class

Standard Java Library String 9 visibility (protected), public static void main(String[] args), command line arguments, visibility (private, protected) 6 ESSENTIAL KNOWLEDGE Types of comments in Java include /* */, which generates a block of comments, //, which generates a comment on one line Language Features and other

Testable Topics 02/19/2020 Notes: 9. The main method and command-line arguments are not included in the subset. In free-response questions, students are not expected to invoke programs. In the AP Computer Science Labs, program invocation with main may occur, but the main method will be kept very simple. 8 02/19/2020 How to compile and run your 1st Java program

1. In Windows only open a text editor, like Notepad and save it as in a new folder named My First Java Program (My Documents - AP CS - My First Java Program). For mac: Create your folder names with no spaces and create APCS in your home directory (Launch Finder Go Home). Go Go Home). Home). So your Mac folder structure should be: APCS - MyFirstJavaProgram Either use Mac OS X's default text editor "TextEdit, you need to open a new file Go Home). choose "Format" Go Home). "Make Plain Text or use a programming text editor (such as Sublime Text, Atom, jEdit or gedit). 2. Write the Java program on the proceeding slides. 9 02/19/2020

Syntax Computers are picky about how you tell them to do something. Truth be told, they are kind of dumb. They cant read between the lines or figure out what you really meant. Programming languages have a particular format you have to stick to when telling the computer what to do. This format is called syntax. When code is written in a way that the computer can understand, we say the code is syntactically correct. When the code is written in a way the computer cannot understand, we say there is a syntax error. Please also note that Java is case sensitive. 10

Hello World 02/19/2020 This keyword is an access modifier which specifies the accessibility or scope of what comes next. Keywords = Words special to the language and have defined meanings for that language. Public means it is accessible everywhere, what this exactly means is shown by the table below. Although it is unlikely that you will understand the terms in the column headings at this stage. 11 Hello World

02/19/2020 HelloWorld { Every Java program must have at least 1 class definition that consists of keyword class followed by the class name (which can be anything you choose). Note standard Java formatting dictates that class names start with a capital followed by lower case letters (for multiple words each word must start with a capital letter no spaces are allowed so this helps make the name more readable) . The name you saved the text document with earlier has to be the same as the name of the public class. Multiple classes are possible but only 1 can be public. The fact that it is public at this stage is inconsequential really as we are not trying to access it from another Java program. However, it is standard practice to declare one class as public. The { indicates the start of the class. See the next slide for some explanation of classes.

12 02/19/2020 Object Orientated Programming Paradigm (OOP) enables modelling of the real world by basing itself on: We will look at these concepts in more detail in later presentations. Examples of classes of realworld entities objects like dog, person, car etc with appropriate variables and methods of using those variables.

Actual real-world entities like actual breeds of dogs, names of people, or makes of cars etc... 13 Hello World 02/19/2020 Note the indentation, you can use 5 spaces or the tab key. It is standard Java formatting to indent methods and other code groups (more about this in later presentations). This second line is known as the main method (method = mini sub program), every program must contain

1 main method as it is the 1st method to be run by Javac and Javac will expect to find it in order to know where to start. The main method is analogous to the power button of a device. public: States that the method can be called from outside the class. See the first public slide. Java expects the main method to be public and Javac will throw an error if it isnt. The JVM would not be able to access it, if is not public, as the JVM is outside of the class. A power button must publically available otherwise nobody could switch on a device. static: Allows the method to be run without creating an object. See previous slide and note that we will deal with this more later. void: States that the method does not return anything.

main: Is the method (mini sub program) name see above. Continued on next slide. 14 First Java Program 02/19/2020 (String[] args): Used for command line arguments that are passed as strings. You can pass strings (text) to the main method when you run a Java program. e.g. to send 1 2 3 to a Java program: javac [filename].java 1 2 3

This appears to be part of the expected construction of the main method, and JVM will throw an error if it is not followed, even if it is not actually used. Note that AP Computer Science does not require you to do so. { indicates the start of the method 15 First Java Program 02/19/2020 This code statement prints the contents inside the . into the console and inserts a newline after. ; semicolons must be used to end almost all Java single code statements.

Code statements = Lines that express some action to be carried out. They are like sentences of a language. 16 First Java Program 02/19/2020 } Indicates the end of the main method and class It is standard formatting to place } on its own on the next line, indented out to line up with the method/class declaration. // Begins a single line comment. A comment is text you want Java to ignore and allow the programmer to describe code or keep notes.

By using comments in the Java code, you help yourself and other programmers understand the purpose of code that a comment refers to. All programs must contain comments to demonstrate your understanding of the code. // Allows comments to span a single line only and traditionally only used for short comments. 17 { } Formatting Issue 02/19/2020 Some programmers use this style to "save" lines. I will use this style as need to save space on the slides in my presentations, I am comfortable with it and many websites/books use it. Or Studies have shown that this style of indenting

leads to fewer errors than other styles. I will leave it up to you to decide which one you use, but you must choose one and be consistent! 18 How to compile and run a Java program 02/19/2020 3. In Windows only navigate to the folder where you saved your Java Program (My Documents - AP CS - My First Java Program), shift right click in the folder and choose Open PowerShell window here or Open command window here Mac: Open Terminal. Cd ~/APCS/MyFirstProgram

i.e. The folder where you saved your Java Program. 19 How to compile and run a Java program 02/19/2020 4. Compile the program. Type and enter: Compiles the .java file and produces a .class file. 5. Run the program.

Type and enter: HelloWorld Runs the .class file produced above. 20 What to send me? 02/19/2020 You send independent programs only. These are programs you have written yourself, not guided ones given to you in presentations e.g. the previous HelloWorld program. Every presentation will end with independent program ideas for you to write yourself (see later).

1. The code and screenshot/s of your program working in an Adobe PDF file. Copy and paste the code into a Word or rtf document, paste the screenshot underneath and Save as or Print (with doPDF) to Adobe pdf. Copy and paste the code into a Word or rtf document, paste the screenshot underneath and Save As or Print (with doPDF) to Adobe pdf. After copying and pasting code increase margins or split long comments into shorter lines if necessary to ensure indentation is maintained. Make sure the name of the Adobe PDF file is the same as the name of your public class. For simple programs one screenshot is sufficient but for more complex ones in the future, multiple screenshots will be necessary. More about this in future presentations. 2. The text file with your code (.java file). 21

02/19/2020 Comments In each presentation I will inform you of what type of comments I will be looking for. For the independent program in this presentation I will be expecting comments about absolutely everything. 1. What does each line do or for? 2. What does every keyword and symbol do and/or mean? Comment either before or after each line but, whatever you choose, be consistent. Comments for lines which are indented should also be indented. The indentation of comments and the line which is being commented on must be the same. After copying and pasting code increase margins or split long comments into shorter lines if necessary to ensure indentation is maintained. 22

02/19/2020 Multi-Line Comments Start with /* and end with */ Allow long comments that can span multiple lines. e.g. /* Hello, Java! */ 23 02/19/2020 Write your own programs:

Write your own programs from scratch. Of course you should use previous programs for reference, but write your code from scratch (do not copy and paste). 24 02/19/2020 What's your name? Write a program which prints your name into the Console. Write comments for every line and explain each keyword/symbol in your own words. Includes every { } ( ) ; You can use /* .

. */ For comments that need more than one line. Also note a screenshot to prove the program works will be expected. 25 02/19/2020 Grade yourself Grade yourself on the vocabulary and learning objectives of the presentation. 26

Recently Viewed Presentations

  • 59-320 Analytical Chemistry Laboratories

    59-320 Analytical Chemistry Laboratories

    Make-Up Experiment . Dates and times TBA (if time and space allows). May be scheduled outside of regularly scheduled laboratory session. Choice of ONE experiment (Experiment 1 to 4)
  • Introduction I. Introduction: Marketing in the Hospitality Industry

    Introduction I. Introduction: Marketing in the Hospitality Industry

    Introduction I. Introduction: Marketing in the Hospitality Industry 1) Customer orientation. The purpose of a Business is to create and maintain profitable customers.
  • Black Holes in Xi`an  Black holes  Accretion disks

    Black Holes in Xi`an Black holes Accretion disks

    Black Holes in Xi`an Black holes Accretion disks Jets Winds and outflows The broad line region
  • Key Frame Animation Amy Gooch AA3: Intro to

    Key Frame Animation Amy Gooch AA3: Intro to

    3D Animation Rendering 3D Scene and Motion Sequence of Frames Rates: Video 30fps, Film 24fps Persistence of Vision Animator must create .. Illusion of Life Weight Animation Almost every property of every object in the scene can be animated (changed...
  • Counting in High-Density Crowd Videos

    Counting in High-Density Crowd Videos

    Video Annotations. Method. Compute forward tracks. Compute backward tracks. Match forward and backward tracks. Compute average tracks by using weighting system.
  • Key Soft Skills for Youth Workforce Success Source:

    Key Soft Skills for Youth Workforce Success Source:

    Hart's Ladder. LAUREN. Facilitator should highlight: Hart's Ladder is one of the foundational frameworks in the youth engagement literature. It illustrates the various levels of youth participation. The bottom three rungs are NOT participation, but levels of tokenism.
  • chp 1

    chp 1

    Table 1.2 summarizes the major differences between these two perspectives on consumer research. To understand how an interpretive framework helps us to understand marketing communications, let's refer to an analysis of one of the best-known and longest-running (1959-1978) advertising campaigns...
  • Detecting Connectivity between Images: the 'Bubbles' Task in ...

    Detecting Connectivity between Images: the 'Bubbles' Task in ...

    Detecting connectivity between images: MS lesions, cortical thickness, and the 'bubbles' task in an fMRI experiment Keith Worsley, Math + Stats, Arnaud Charil, Montreal Neurological Institute, McGill Philippe Schyns, Fraser Smith, Psychology, Glasgow Jonathan Taylor, Stanford and Université de Montréal...