1 / 17

CA591/CA598 Object-Oriented Programming in Java

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

lexiss
Download Presentation

CA591/CA598 Object-Oriented Programming in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CA591/CA598 Object-Oriented Programmingin Java Chapter 2 Dr. James Murphy jamurphy@computing.dcu.ie

  2. 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.

  3. 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).

  4. 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.

  5. 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 */

  6. 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’

  7. 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;

  8. Declaring Constants • Constant – represents value that will not change in program. • Declared like variable but with final before it: • final double PI = 3.1415;

  9. Assignment Statements • variable = expression • pay = rate * hours; • Note a statement is always followed by a semicolon ;

  10. 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

  11. 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)

  12. 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.

  13. 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

  14. 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.*;

  15. 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;

More Related