24
Basics of Java Rabeea Ahmed

Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Basics of Java

                                                          Rabeea Ahmed 

Page 2: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Overview

1.History of Java

2.Using Java for CS50 Final Projects

3.Writing, Compiling and Running .java using Eclipse IDE

4.Coding and Debugging in Java

5.Other Resources

Page 3: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

 

 

Page 4: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

        Compiling Java Programs

Step 1: Compiler turns Java code into byte-code

 NOTE: byte-code is machine language of JVM.

Step 2: Interpreter turns byte-code into machine code for all

Page 5: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

WHY?

Page 6: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

 Portability: write once, run everywhere

●Once compiled, byte-code can be sent over the Internet.

● No need to recompile if on another computer.

●Operating System differences bypassed

       

Page 7: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

                        uses of java

1. Internet Applications

2. Android App. Development

3.  JavaServer Pages (JSP)

4.Mobile Information Devices Profile (MIDP)

Page 8: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

         Using Eclipse IDE for Java

● The Integrated Development Environment (eg. Netbeans)

● Compiles upon saving (no need for Terminal commands)

 Steps:

#1. Download Java Runtime Environment 

#2. Download Eclipse itself

Page 9: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

                    

            ... Let the coding begin 

Page 10: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Object Oriented Programming Language

● Java consists of○ Classes 

■ descriptions of objects (blueprint)■ describes structure containing methods.

○Object ■ realization of what the class described■ instantiazed class■ have associated methods

              

                

Page 11: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Remember: 

Java is an object oriented programing language            System.out.println("Hello, world!");

Class name and file name should be fully IDENTICAL

Page 12: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Data Types in Java

Class Types:                Blue-prints for objects                Specifies how data included in a class is dealt with                Eg. String

Primitive Types 

                Eg: Variables                Value is a single number or letter.

Page 13: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Primitive Data Types

Type Name Kind (of Value) Memory 

byte integer 1 byte

short integer 2 bytes

int integer 8 bytes

float floating-point 4 bytes

double floating-point 8 bytes

char  single-char 2 bytes

boolean true/false 1 bit

Page 14: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

STYLE: Legality of Identifier Names

Class Type:

                        YourBook                        MyFullName

Primitive Type:

                        myFirstInitial                        teamNumber                        days_Per_Week                        

Page 15: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Constants

                       public static final Type Variable = Constant                 public static final double PI = 3.14159;

                                    Type Variable = Constant;                 double PI = 3.14159;

Page 16: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Type Casting in Java

                int speed = 4.42;                int points = (int) speed;

                 NOT: int points  = speed;

    

Page 17: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Type Casting in Java

                double speed = 4.42;                int points = (int) speed;

                 NOT: int points  = speed;

    

Page 18: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Char To Int

                                 char symbol = '7';                     System.out.println((int)symbol);

Page 19: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Order of Precedence in Java

1. Unary operations +, - , !, ++ and --

2. Binary arithmetic operations *, / and %

3. Binary arithmetic operations     + and - 

Page 20: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Flow of Control

●      Very similar to C

●     if-else, while, do-while all exist in Java

●      ==, !=, <= and >= are same as      in C

●    System.exit (0) vs break;    

Page 21: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

                 Making Final Projects

Make your own objects

Page 22: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Making Objects For Yourself

Use constructors and methods (either public or private)

Use multiple classes made within the same directory. 

These classes can be re-usable!

Page 23: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Resources:

1) Java Class Library 

2) Java: An Introduction to Problem Solving and Programming    By: Walter Savitch    (see Cabot Library)

3) Java Programming: A Back to Basics Approach    By: Stuart Reges and Marty Stepp

4) Email me at: [email protected]

Page 24: Basics of Java - d2o9nyf4hwsci4.cloudfront.netd2o9nyf4hwsci4.cloudfront.net/2011/fall/seminars/basics_of_Java/b… · 1) Java Class Library 2) Java: An Introduction to Problem Solving

Thank You!