Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Previous Lesson

Similar presentations


Presentation on theme: "Review of Previous Lesson"— Presentation transcript:

1 Review of Previous Lesson
21/04/2019 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

2 How to Compile & Run your First Java Program
21/04/2019 How to Compile & Run your First Java Program

3 21/04/2019 Vocabulary Content: Start End describe compile run program

4 Learning Objectives 21/04/2019 Content: Start End
Describe how to compile and run a Java program.

5 Language Features and other Testable Topics
21/04/2019 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 6 Methods visibility (public), static 9, 10 visibility (protected), public static void main(String[] args), command line arguments, Classes create class visibility (private, protected) Standard Java Library String

6 Language Features and other Testable Topics
21/04/2019 Notes: 6. User input is not included in the AP Java subset. There are many possible ways for supplying user input: e.g., by reading from a Scanner, reading from a stream (such as a file or a URL), or from a dialog box. There are advantages and disadvantages to the various approaches. The exam does not prescribe any one approach. Instead, if reading input is necessary, it will be indicated in a way similar to the following: double x = /* call to a method that reads a floating point number */; or double x = ...; // read user input 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. 10. Students are required to understand when the use of static methods is appropriate. In the exam, static methods are always invoked through a class (explicitly or implicitly), never an object (i.e., ClassName.staticMethod( ) or staticMethod( ), not obj.staticMethod( )).

7 How to compile and run your 1st Java program
21/04/2019 How to compile and run your 1st Java program 1. Create a folder for all your Java programs. I suggest “Java Programs” in “My Documents”. 2. Open a text editor, like Notepad and name it HelloWorld.java For mac: Use a programming text editor (such as jEdit, gedit, Sublime Text, or Atom) to input the following source code and save as "Hello.java" under the directory "~/myProject". [If you use Mac OS X's default text editor "TextEdit“, you need to open a new file ⇒ choose "Format" ⇒ "Make Plain Text" ⇒ Enter the source code ⇒ Save as "Hello.java".] 3. Write the Java program on the proceeding slides.

8 21/04/2019 Syntax Computers are picky about how you tell them to do something. Truth be told, they are kind of dumb. They can’t “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.

9 Hello World 21/04/2019 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.

10 Hello World HelloWorld { 21/04/2019
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.

11 21/04/2019 Object Orientated Programming Paradigm (OOP) enables modelling of the real world by basing itself on: Examples of classes of real-world entities objects like dog, person, car etc… with appropriate variables and methods of using those variables. We will look at these concepts in more detail in later presentations. Actual real-world entities like actual breeds of dogs, names of people, or makes of cars etc...

12 Hello World 21/04/2019 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 isn’t. 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.

13 First Java Program (String[] args):
21/04/2019 (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

14 First Java Program 21/04/2019 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.

15 First Java Program } // 21/04/2019
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.

16 { } Formatting Issue 21/04/2019 Or
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!

17 How to compile and run a Java program
21/04/2019 Steps below can be replaced by navigating to the folder where you save all your Java Programs, shift right clicking in the folder and choosing “Open PowerShell window here” or “Open command window here”. 4. Navigate to the folder where you save all your Java Programs, click the yellow folder icon in the address bar and copy the address for pasting later. 5. Open command prompt (cmd) on Windows (if you are Mac OS then open Terminal). 6. Set the Current Working Directory to the directory where you save all your Java Programs by typing and entering: cd address of the folder where you save all your Java Programs You can paste the address you copied earlier with Ctrl + v

18 How to compile and run a Java program
21/04/2019 7. Compile the program. Type and enter: 8. Run the program. HelloWorld.java HelloWorld

19 What to send me? You send independent programs only. 21/04/2019
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). The code and screenshot/s of your program working in an Adobe PDF file. 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. The text file with your code (.java file).

20 21/04/2019 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. What does each line do or for? 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.

21 Multi-Line Comments Start with /* and end with */
21/04/2019 Multi-Line Comments Start with /* and end with */ Allow long comments that can span multiple lines. e.g. /* Hello, Java! */

22 21/04/2019 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).

23 21/04/2019 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. You can use /* …. ……. …… */ For comments that need more than one line.

24 4/21/2019 Grade yourself Grade yourself on the vocabulary and learning objectives of the presentation.


Download ppt "Review of Previous Lesson"

Similar presentations


Ads by Google