170 likes | 340 Views
CA591/CA598 Object-Oriented Programming in Java. Chapter 2 Dr. James Murphy jamurphy@computing.dcu.ie. Learning Objectives. Source code and machine code. Be able to write simple Java program. Implement in Java programs designed from Lesson 1. Be able to “walk through” code
E N D
CA591/CA598 Object-Oriented Programmingin Java Chapter 2 Dr. James Murphy jamurphy@computing.dcu.ie
Learning Objectives • Source code and machine code. • Be able to write simple Java program. • Implement in Java programs designed from Lesson 1. • Be able to “walk through” code • Simulate actions of computer as program is processed.
Java Programming Language • Source code – programming language e.g. Java, C++, etc. • Compiler – translates source code to machine code (01111001) • Each programming language has unique syntax and semantics. • Syntax – command is in correct form (i.e. grammar) • Sematics – produce correct results (meaningful).
Java Syntax – Naming Identifiers • Must start with letter, underscore or $ • e.g. sum, _sum, $sum • May contain letters, digits, underscores, $ • Cannot be reserved word. • Is case sensitive (i.e. Total not the same as total) • No spaces – i.e. costPrice instead of cost Price • Tip: keep the name short but meaningful.
Literals: • Explicit data values • Numeric literal – 0 123 0.5 • Character literal – ‘A’ ‘\n’ • Comments: • Parts of the program that compiler ignores. • There to help human reading the program, record notes etc. • // ignores everything on current line • /* ignore everything between these symbols • across multiple lines */
Variables • Location in memory that holds a value • Must have name (identifier) and type. • Some data types in Java: • int – integer (whole number), e.g. 2 • double – numbers with fractional part, e.g. 1.5 • char – characters, e.g. ‘A’
To declare a variable: • Declare the type followed by the name • Example: • int number; • double radius; • Declaring multiple variables of same type: • int num1, num2;
Declaring Constants • Constant – represents value that will not change in program. • Declared like variable but with final before it: • final double PI = 3.1415;
Assignment Statements • variable = expression • pay = rate * hours; • Note a statement is always followed by a semicolon ;
Notes on Arithmetic Expressions • Division with two integers gives integer result (fractional part discarded) • 7 / 4 = 1 • Modulus operator (%) must only be used with integers. • 7 % 4 = 3 • Operator Precendence: • Parenthesised expressions ( ) • Multiplication, division, modulus ( * / % ) • Addition and substraction ( + - ) • Examples: • 2 + 4 * 3 Answer: 14 • (2 + 4) * 3 Answer: 18
Print statement • System.out.print(“The sum of the two numbers is “ ); • System.out.println(sum); • Calling some pre-written code (don’t need to understand underlying mechanism) • println – goes on to the next line after printing. • System.out.println(“The sum of the two numbers is “ + sum); • + means concatenation (combine two strings)
Classes and Methods • Method contains a group of statements. • Class can contain a number of methods. • Java program must include at least one class and one method. • To declare class: • class AddNumbers • Note: class has small c, but class name starts with capital letter. • We invoke or “call” a method when we want it to execute.
Structure of a Java Program • Java application must have a main method: • public static void main(String[] args) • Each class begins with opening brace { and ends with a closing brace } class ClassName { methodName() { some code } // end of method } // end of class
Reading Input From Keyboard • You will be provided with class called Scanner • Contains methods for reading different types of data • nextInt() – to read an integer from keyboard. • nextDouble() – to read a floating point number. • nextLine() – to read a line of characters. • Usage: • Scanner sc = new Scanner(System.in); • firstNumber = sc.nextInt(); • Note: Must add line to start of program: • import java.util.*;
Summary • Declare variables • Assign values to a variable. • Create java expression • e.g. firstNumber * secNumber • Assign the result of this evaluation to a variable: • sum = firstNumber * secNumber;