CST8116 Intro to Comp Prog - Java Overview PDF

Document Details

FaithfulLaplace2771

Uploaded by FaithfulLaplace2771

Algonquin College

Tags

Java programming Computer programming Software licensing Introduction to Computer Programming

Summary

This document is a presentation on Java programming for Algonquin College. It covers topics such as software licensing, algorithm design, testing, and basic Java programming concepts like classes, methods, and System.out.println().

Full Transcript

CST8116 Intro. to Comp. Prog. Week 01 Lesson 03 Java Overview Week 01 Lesson 03 Java Overview Software Licensing (In Brief) Designing a Simple Algorithm Testing an Algorithm - Exercises & Assignments Translating the Algorithm into Java (Syntax Matters) An...

CST8116 Intro. to Comp. Prog. Week 01 Lesson 03 Java Overview Week 01 Lesson 03 Java Overview Software Licensing (In Brief) Designing a Simple Algorithm Testing an Algorithm - Exercises & Assignments Translating the Algorithm into Java (Syntax Matters) Anatomy of Java Programs: Class Anatomy of Java Programs: Method main Anatomy of System.out.println("Hello World"); Demonstration of Editing, Compiling, Running Explore Editing, Compiling, Running Good vs Poor Programmer Comments Documenting Java Programs 2 Welcome In this presentation we will talk briefly about software licensing, then look at creating a simple Algorithm, testing it, and translating the algorithm into the Java programming language using a basic development environment via a demonstration. 3 Software Licensing (In Brief) Before you use any software product, including a programming language, development tools or code libraries (APIs), stop and review the license. Typically, many programming languages are free for personal use. However if you start to sell or distribute programs you create it is no longer personal use. You will need to ensure that you are licensed or permitted to use the software. Java version 1.8 and earlier was nearly free for use. Oracle Corporation who currently own the Java technology changed the license for Java 1.9 onwards so it is definitely not free for use in a production environment now. (Programmers say 1.8, 1.9, 1.11 while marketing uses Java 8, Java 9, Java 11) Oracle did make an open-source of Java available, referred to as OpenJDK. We are using Amazon Corporations flavor of OpenJDK 11 called Corretto 11 4 Software Licensing (In Brief continued) While OpenJDK is open-source, Oracle does not support the use of it in a production environment. (i.e. it may have bugs). Oracle releases a new version of Java every six months, however these versions are prone to changes or are no longer supported at the end of the six months (no updates or patches). Periodically a version is marked as long-term-support which means it is stable, bug-free(er), and updated and patched regularly to correct bugs and security flaws. Java 11 is the current long-term-support version, however industry in many cases is still using Java 8 the previous long-term support version. Amazon Corretto 11 is fully compliant with Java 11, is actively maintained and patched, is production ready and for our purposes works the same as Oracle Java…. However Amazon’s licensing is not as restrictive as Oracle’s. 5 Time to check your learning! What could happen if I use a code-library from the internet and do not read the licensing carefully? I or my company may owe the original author a fee for using the software library, either a one-payment or per-use. The software I am creating might be forced to become open-source, if I use an open-source library and do not read the license as the license may mandate that any software using the library also become open- source. I could get sued, fined and/or fired from work, if I do not read licenses carefully or request my client’s legal counsel and / or my supervisor to review licenses before I proceed to use the software or libraries. 6 Designing a Simple Algorithm Problem Statement: Create a program that outputs Hello World on screen. Understanding the problem: Output Hello World to the computer screen. Pseudocode and Flow Chart start output “Hello World” stop 7 Testing an Algorithm - Exercises & Assignments Testing of an algorithm for our course will initially be done using a table like this: Input Output Expected Output Description none Hello World Hello World Program should output Hello World. No inputs required. We would use test values for inputs, then read our algorithm (pseudocode, flowchart) and manually check that calculations and logic work as we expect. If we determine a problem with our logic or calculations we need to revisit the algorithm and fix the problem, before we attempt to write the Java program. 8 Time to check your learning! What is the flowchart symbol for Starting and Stopping the algorithm? (Ellipse or Parallelogram) What is the flowchart symbol for output ? (Ellipse or Parallelogram) What are the four column names we are using to test algorithms in a table? 9 Translating the Algorithm into Java (Syntax Matters) Where this simple program prints Hello World, we name the class HelloWorld: public class HelloWorld{ public static void main(String[] args){ System.out.println("Hello World"); } } Java is a case sensitive programming language. Method main is where Java programs start. The first line is called the Class Header, the contents in the outermost {} the class body. The second line is called a Method Header, the contents between the inner{} the method body. Note the indentation, so the structure of the program can be seen. The third line starting with System and ending with ; is a statement. 10 Anatomy of Java Programs: Class public – is an access modifier, more on this in a future lecture class – tells the compiler the next word is the name of the class HelloWorld – the name of the class, spaces are not allowed in identifiers(names) By convention, Java class names start with an upper case letter, then each new word in the name is also upper case. HelloWorld All of the parts of the class must be between the braces, i.e. { and } Note: The opening brace can be used on the next line, however by convention Java programmers use end-of-line, as seen on the previous slide. public class HelloWorld { 11 Anatomy of Java Programs: Class and File You are only permitted to have one public class in a source code file in Java Programming. The file name, must match the name of the public class exactly including capitalization. So if the source code has: public class HelloWorld{ The file on the hard-disk must be: HelloWorld.java 12 Anatomy of Java Programs: Method main public – is an access modifier, more on this in a future lecture static – is a modifier, more on this in a future lecture void – the return type of the method, more on void and return in a future lecture main – the name of the method, case-sensitive not Main, mAin etc. By convention Java method names start with a lower case letter. ( ) – These parenthesis enclose the parameters, they are required for methods String[] args – A parameter, will come back to this in a future lecture public static void main(String[] args) { // think of this as the start of the program or method (module) } // think of this as the stop of the program or method (module) 13 Anatomy of System.out.println("Hello World"); System – this is a utility class that helps us with input and output in Java programs.. – a period, called a dot, separates classes, objects and methods when used together. out – this is the standard output stream, an object System grants access to println() – this is a method of the standard output stream, we pass data between the () for it to process, this is called “passing an argument to a parameter” " Hello World" – this is String data to be output on screen. Caution, Java only understands standard double-quotes and single- quotes e.g. " and ' , if you are using word processor “smart quotes” will not work in a Java program. ; - All Java program statements must end with a semi-colon. 14 Time to check your learning! Which of the following words is an access modifier? public, class, void, main Which if the following words is the name of the method Java programs start up on? public, class, void, main, String, args What characters are used to enclose the parameter list of a method? Parenthesis: ( and ), or Braces { and } What characters are used to enclose a class body or method body? Parenthesis: ( and ), or Braces { and } 15 Demonstration of Editing, Compiling, Running Use Notepad to type the Java program, then save as HelloWorld.java Java only permits one public class per file, and the file-name must match the class name exactly, including upper and lower case letters. Verify that Notepad didn’t save your file as HelloWorld.java.txt Compile HelloWorld.java inside a command prompt (cmd.exe) using javac HelloWorld.java Verify that there is a HelloWorld.class output by the compiler, or fix any mistakes in the program syntax, save, then re-compile. Run HelloWorld, by typing java HelloWord Use the program output to verify the program, i.e. revisit the Algorithm test plan and check the program works correctly. 16 Explore Editing, Compiling, Running What happens if you insert more System.out.println(“Hello World“);? What happens if some of them are print, rather than println? Can we use multiple print statements to print each letter and get the expected output? What if we call System.out.println(); and do not pass it any data? What happens if we do this: System.out.println(40 + 2); // numerical data Or this: System.out.println("40" + "2"); // text data 17 Documenting Java Programs Programmers document code to help themselves and other programmers when they need to maintain a program (imagine you have not looked at a program for six months or more and now need to come up to speed on it again) Programmer comments are retained in the source file, but ignored by the compiler. // - two forward-slashes are a single line comment, used to make brief comments on a line of code, either above or after the end of the line. is a comment, used to write brief descriptions of classes and methods. By convention * characters are placed vertically but this is not required. (See example next slide) */ This is a Javadoc comment, will learn about these in a future course. 18 Documenting HelloWorld.java public class HelloWorld{ // start class body public static void main(String[] args){ // start method main System.out.println("Hello World"); // output "Hello World" } // stop method main } // end class body comments like //start //stop //end are not typically used, this is demo only The comment block at top is required in all of your assigned work’s code files. 19 Documenting HelloWorld.java Each source code file must have a comment block at the very top with student name, lab professor name, due date, and brief description like “exercise 1” or “assignment 1” etc in our course. The “Modified” line is optional, and would only be used if you were provided starter code to edit i.e. updating existing code. 20 Documenting HelloWorld.java Each class that you author must have a brief comment above the class header. It should briefly overview the purpose of the class. public class HelloWorld{ 21 Documenting HelloWorld.java Each method needs to have a brief programmer comment with the intended purpose of the method. public static void main(String[] args){ System.out.println("Hello World"); } Single line comments should be used sparingly, for important details. } // stop method main } // end class body 22 Good vs Poor Programmer Comments Good programmer comments are: brief, and; Answer questions like: What is this class / method for? Why is this line or lines of code important? Poor programmer comments are: Too detailed, typically attempt to explain ‘how’ the program logic works. Analogy: If someone is asking for driving directions they want left, right, street name etc. they are not likely interested in the history of the landmarks they will see on their drive because they need to arrive on time for a meeting. 23 Conclusion In this lesson we reviewed: Software Licensing (In Brief) Designing a Simple Algorithm Testing an Algorithm - Exercises & Assignments Translating the Algorithm into Java (Syntax Matters) Anatomy of Java Programs: Class Anatomy of Java Programs: Method main Anatomy of System.out.println("Hello World"); Demonstration of Editing, Compiling, Running Explore Editing, Compiling, Running Good vs Poor Programmer Comments Documenting Java Programs 24

Use Quizgecko on...
Browser
Browser