COSC125_Lecture2_Java Review.pptx

Full Transcript

Data Structure and Programming Techniques (COSC 125) Module 01: Java Review Part 01: Simple Java Program Ms. Amal Mohammed Nizar Slides adapted from Ms. Fatema Akbar 2022/2023 Anatomy of a Java Program Class name Main method Statements Statement terminator Reserved words Comm...

Data Structure and Programming Techniques (COSC 125) Module 01: Java Review Part 01: Simple Java Program Ms. Amal Mohammed Nizar Slides adapted from Ms. Fatema Akbar 2022/2023 Anatomy of a Java Program Class name Main method Statements Statement terminator Reserved words Comments Blocks 8 Class Name Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is Welcome. // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 9 Main Method Line 2 defines the main method. In order to run a class, the class must contain a method named main. The program is executed from the main method. // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 10 Statement A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") in the program in Listing 1.1 is a statement to display the greeting "Welcome to Java!“. // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 11 Statement Terminator Every statement in Java ends with a semicolon (;). // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 12 Reserved words Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 13 Blocks A pair of braces in a program forms a block that groups components of a program. public class Test { Class block public static void main(String[] args) { System.out.println("Welcome to Java!"); Method block } } 14 Special Symbols Character Name Description {} Opening and closing Denotes a block to enclose statements. braces () Opening and closing Used with methods. parentheses [] Opening and closing Denotes an array. brackets // Double slashes Precedes a comment line. " " Opening and closing Enclosing a string (i.e., sequence of characters). quotation marks ; Semicolon Marks the end of a statement. 15 { …} // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 16 ( … ) // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 17 ; // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 18 // … // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 19 "…" // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 20 Programming Style and Documentation Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles 21 Appropriate Comments Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Include your name, class section, instructor, date, and a brief description at the beginning of the program. 22 Naming Conventions Choose meaningful and descriptive names. Class names: Capitalize the first letter of each word in the name. For example, the class name ComputeExpression. 23 Proper Indentation and Spacing Indentation Indent two spaces. Spacing Use blank line to separate segments of the code. 24 Block Styles Use end-of-line style for braces. Next-line public class Test style { public static void main(String[] args) { System.out.println("Block Styles"); } } End-of-line style public class Test { public static void main(String[] args) { System.out.println("Block Styles"); } } 25 Programming Errors Syntax Errors Detected by the compiler Runtime Errors Causes the program to abort Logic Errors Produces incorrect result 26 Syntax Errors public class ShowSyntaxErrors { public static main(String[] args) { System.out.println("Welcome to Java); } } ShowSyntaxErrors Run 27 Runtime Errors public class ShowRuntimeErrors { public static void main(String[] args) { System.out.println(1 / 0); } } ShowRuntimeErrors Run 28 Logic Errors public class ShowLogicErrors { public static void main(String[] args) { System.out.println("Celsius 35 is Fahrenheit degree "); System.out.println((9 / 5) * 35 + 32); } } ShowLogicErrors Run 29 Reading Input from the Console 1. Create a Scanner object Scanner input = new Scanner(System.in); 2. Use the method nextDouble() to obtain to a double value. For example, System.out.print("Enter a double value: "); Scanner input = new Scanner(System.in); double inputdouble = input.nextDouble(); ComputeAreaWithConsoleInpu Run t ComputeAverage Run 30 Getting user input (Reading Input from the Console) java.util.* ; // Implicit import java.util.Scanner; // Explicit Import No performance difference 31 Declaring Variables int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; // Declare a to be a // character variable; 32 Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a; 33 Variable Naming By Java convention: Variable names start with a lowercase letter Class names start with an uppercase letter It is an error to use a variable that has never had a value assigned to it int height; int width = height; // ERROR - uninitialized variable height Remedy: assign a value to the variable before you use it int height = 20; int width = height; // OK Operators & Conditional Statements Numeric Operators Name Meaning Example Result + Addition 34 + 1 35 - Subtraction 34.0 – 0.1 33.9 * Multiplication 300 * 30 9000 / Division 1.0 / 2.0 0.5 % Remainder 20 % 3 2 39 Integer Division 5 / 2 yields ………... 5.0 / 2 yields …………. 5 % 2 yields ………. 40 Integer Division 5 / 2 yields an integer 2. 5.0 / 2 yields a double value 2.5 5 % 2 yields 1 (the remainder of the division) 41 How to Evaluate an Expression 3 + 4 * 4 + 5 * (4 + 3) - 1 (1) inside parentheses first 3 + 4 * 4 + 5 * 7 – 1 (2) multiplication 3 + 16 + 5 * 7 – 1 (3) multiplication 3 + 16 + 35 – 1 (4) addition 19 + 35 – 1 (5) addition 54 - 1 (6) subtraction 53 42 Augmented Assignment Operators 43 Augmented Assignment Operators 44 Relational Operators Java Mathematics Name Example Result Operator Symbol (radius is 5) < < less than radius < 0 false greater than radius > 0 true >= ≥ greater than or equal to radius >= 0 true == = equal to radius == 0 false != ≠ not equal to radius != 0 true 45 The boolean Type and Operators Often in a program you need to compare two values, such as whether i is greater than j. Java provides six comparison operators (also known as relational operators) that can be used to compare two values. The result of the comparison is a Boolean value: true or false. boolean b = (1 > 2); 46 Conditional Statements One way if. if(num>0) System.out.print(“Positive”); Two-way if: if(num>0) System.out.print(“Positive”); else System.out.print(“Negative”) Conditional Statements Multiple if. if(num>0) System.out.print(“Positive”); else if(num

Use Quizgecko on...
Browser
Browser