SlideShare una empresa de Scribd logo
1 de 19
Java in two semesters by Quentin Chratan and Aaron Kans
 public class Hello
    Object Oriented languages require that program is to be
     written in separate units called classes
    Telling the compiler that we are writing a class named
     as Hello
    Public indicates that we are placing this class accessible
     to every one
    A public class should always be saved in file with the
     same name as of the class
    So what will be name of our program??
       Hello.java
    Our program will interact with multiple built in java
     libraries classes
{}
    Everything in class has to be contained between two curly
     brackets (Braces) to indicate the beginning and end of class
 Java is case sensitive
    Upper case letters and lower case letters are different
 public static void main(String[] args)
    Every program will have at least one class with this line
    This is method
    Main is a special method, this is where the program begins
    A program starts with the first instruction of main and then
     obeys each instruction in sequence.
    The program terminates when it has finished obeying the final
     instruction of main
    { } indicates the body of main and marks its starting and
     ending
 System.out.println(“Hello World”);
      Display “Hello World”
      println is short for “print line”
;
      Java instruction has to end with a semi colon.
 Although you can choose any name such as x, its best to pick a
  name that describe the purpose of data item
 E.g.
    A computer game might need a piece of data to record the
     player’s score as secret key.
    What should be the name of variable to store such data
        Score
    What should be the data type of such variable (byte, short, int
     and long)
        int
 You can choose any name for variable as long as
    The name is not already a word in java language (class, void,
     public)
    Name has no spaces in it
    Name does not include operators or mathematical symbols such
     as + or –
    Name starts with either a letter, an underscore (_) or a dollar sign
     ($)
    You can use any letter as starting letter but java convention is to
     begin the name with lower case letter
Computer Memory         Java Instruction


                  int score; hits;
                      score,



                  char level;
 Allows values to be put into variables
 Written with equality symbol (=)
    Assignment Operator
 variableName = value;
    score =0;
    Set value of score to zero OR score becomes equal to zero
    Puts the number zero into memory we called score
 Combine variable statement with a variable declaration
        int score = 0;        int score;
                              score =0;
 What will be the effect of following statement in java?
    int score = 2.5
    This statement will not compile as 2.5 is a real number
 double something =1000;
   Though 1000 is an integer but this statement is legal as
    will not result in information loss. It will be considered
    as 1000.0
 Character Assignment
   char level = ‘A’;
      Enclosed he character in single quotes
      Assignment at the time of declaration
   level =‘B’
      Assignments changed
 Data items in program have values that do not change
 Following are examples of such items
   Maximum score in a exam
   Number of hours in a day
   Mathematical value of  (3.14176)
 In these cases the values do not vary
 Values remain constant
 Such items should be named and declared as
  constants
 Add the keyword “final” before declaration
   final int HOURS = 24
 The statement HOURS = 12 will not compile now
Operation        Java Operator
Addition         +
Subtraction      -
Multiplication   *
Division         /
Remainder        %
int x;
x =10 + 25;
double cost;
cost = 500*(1+17.5/100);
int x;
x=30/4;
What Will be the answer? 7 or 7.5?

The answer is 7 as division operator is overloaded
double price, tax, cost;
price = 500;
tax =17.5;
cost =price+(1+tax/100);
price =price+(1+tax/100);
 X = X + 1;
    X++;
 X = X – 1;
    X--;
 int X = 5;
 int Y = 0;
      Postfix             Prefix
      Y=X--;
      Y=X++;              Y=--X;
                          Y=++X;
      Y value will be 5   Y value will be 6
                                          4
      X value will be 6
                      4   X value will be 6
                                          4


 •Y = Y + X;
 •Y + = X;
 System.out.println(“Hello World”);
 System.out.println(“Welcome to java world”);
 Hello World
 Welcome to java world

 System.out.print(“Hello World”);
 System.out.println(“Welcome to java world”);
 Hello WorldWelcome to java world

 System.out.println();
 Blank line in the program
 Strings
    Collection of characters
    Always enclosed in speech marks “ “
    Print statements print strings
    Several strings can be combined using + operator
       Concatenation Operator (+)
    System.out.println(“Hello” + “World”);
       HelloWorld
    Spaces included in speech marks are printed
       System.out.println(“Hello ” + “World”);
             Hello World
 System.out.println(10*10);
    The instruction prints 100 on screen.
    Java converts value/expression to a string before displaying it
    As these numbers are converted into string so they can be
     combined
    System.out.println(“Cost = ”+(10*10));
       Cost = 100
 Complex data type
    Name
    Address
    Car Registration Number
    Any meaningless sequence of characters
 Declare it in the same was as declare variables
    String Name;
    Name = “Saira”;
   OR
    String Name = “Saira”;
 Part of Java release 5.0 and later
 Class that makes it easy for us to write a program
  that obtains information that is typed in at the
  keyboard
 Scanner is part of Java package called util
    A package is a collection of pre-compiled classes
 To make Scanner class accessible to compiler, we
  have to tell the compiler that it should look in util
  package
    import java.util.*;
       * means all classes in util package are made available
    import java.util.Scanner;
       Only Scanner class is accessible
 Create Object
    Scanner sc = new Scanner(System.in);
       System.in represent KeyBoard
       new instantiates a class by allocating memory for a new object
        and returning a reference to that memory.
 Integer Input
    int x;
    x=sc.nextInt();
 Double Input
    double x;
    x=sc.nextDouble();
 String Input
    String x;
    x=sc.next(); OR x=sc.nextLine();
 Character Input
    char x;
    x=sc.next().charAt(0);

Más contenido relacionado

La actualidad más candente

Python functions part12
Python functions  part12Python functions  part12
Python functions part12Vishal Dutt
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPsRavi Bhadauria
 
Applicative Functor - Part 2
Applicative Functor - Part 2Applicative Functor - Part 2
Applicative Functor - Part 2Philip Schwarz
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Nettjunicornfx
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Rasan Samarasinghe
 
DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#Rasan Samarasinghe
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsMegha V
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.pptTareq Hasan
 
DISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in JavaDISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in JavaRasan Samarasinghe
 
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONSVISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONSSuraj Kumar
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The BasicsRanel Padon
 
Advance python programming
Advance python programming Advance python programming
Advance python programming Jagdish Chavan
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAMaulik Borsaniya
 

La actualidad más candente (20)

Python functions part12
Python functions  part12Python functions  part12
Python functions part12
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Applicative Functor - Part 2
Applicative Functor - Part 2Applicative Functor - Part 2
Applicative Functor - Part 2
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
Perl slid
Perl slidPerl slid
Perl slid
 
DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Functions
FunctionsFunctions
Functions
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
 
DISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in JavaDISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in Java
 
Python Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - FunctionsPython Programming Essentials - M17 - Functions
Python Programming Essentials - M17 - Functions
 
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONSVISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The Basics
 
Advance python programming
Advance python programming Advance python programming
Advance python programming
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
 

Destacado

Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Chloe Main
 

Destacado (8)

Comp102 lec 9
Comp102   lec 9Comp102   lec 9
Comp102 lec 9
 
Comp102 lec 3
Comp102   lec 3Comp102   lec 3
Comp102 lec 3
 
Comp102 lec 5.1
Comp102   lec 5.1Comp102   lec 5.1
Comp102 lec 5.1
 
Comp102 lec 5.0
Comp102   lec 5.0Comp102   lec 5.0
Comp102 lec 5.0
 
Comp102 lec 8
Comp102   lec 8Comp102   lec 8
Comp102 lec 8
 
Comp102 lec 10
Comp102   lec 10Comp102   lec 10
Comp102 lec 10
 
Comp102 lec 7
Comp102   lec 7Comp102   lec 7
Comp102 lec 7
 
Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!
 

Similar a Comp102 lec 4 (20)

Class 8 - Java.pptx
Class 8 - Java.pptxClass 8 - Java.pptx
Class 8 - Java.pptx
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptx
 
CSL101_Ch1.pptx
CSL101_Ch1.pptxCSL101_Ch1.pptx
CSL101_Ch1.pptx
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
java_lect_03-2.ppt
java_lect_03-2.pptjava_lect_03-2.ppt
java_lect_03-2.ppt
 
Unit I Advanced Java Programming Course
Unit I   Advanced Java Programming CourseUnit I   Advanced Java Programming Course
Unit I Advanced Java Programming Course
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java tutorial PPT
Java tutorial  PPTJava tutorial  PPT
Java tutorial PPT
 
PHP Reviewer
PHP ReviewerPHP Reviewer
PHP Reviewer
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
Java Basics 1.pptx
Java Basics 1.pptxJava Basics 1.pptx
Java Basics 1.pptx
 

Comp102 lec 4

  • 1. Java in two semesters by Quentin Chratan and Aaron Kans
  • 2.  public class Hello  Object Oriented languages require that program is to be written in separate units called classes  Telling the compiler that we are writing a class named as Hello  Public indicates that we are placing this class accessible to every one  A public class should always be saved in file with the same name as of the class  So what will be name of our program??  Hello.java  Our program will interact with multiple built in java libraries classes
  • 3. {}  Everything in class has to be contained between two curly brackets (Braces) to indicate the beginning and end of class  Java is case sensitive  Upper case letters and lower case letters are different  public static void main(String[] args)  Every program will have at least one class with this line  This is method  Main is a special method, this is where the program begins  A program starts with the first instruction of main and then obeys each instruction in sequence.  The program terminates when it has finished obeying the final instruction of main  { } indicates the body of main and marks its starting and ending
  • 4.  System.out.println(“Hello World”);  Display “Hello World”  println is short for “print line” ;  Java instruction has to end with a semi colon.
  • 5.  Although you can choose any name such as x, its best to pick a name that describe the purpose of data item  E.g.  A computer game might need a piece of data to record the player’s score as secret key.  What should be the name of variable to store such data  Score  What should be the data type of such variable (byte, short, int and long)  int  You can choose any name for variable as long as  The name is not already a word in java language (class, void, public)  Name has no spaces in it  Name does not include operators or mathematical symbols such as + or –  Name starts with either a letter, an underscore (_) or a dollar sign ($)  You can use any letter as starting letter but java convention is to begin the name with lower case letter
  • 6. Computer Memory Java Instruction int score; hits; score, char level;
  • 7.  Allows values to be put into variables  Written with equality symbol (=)  Assignment Operator  variableName = value;  score =0;  Set value of score to zero OR score becomes equal to zero  Puts the number zero into memory we called score  Combine variable statement with a variable declaration int score = 0; int score; score =0;  What will be the effect of following statement in java?  int score = 2.5  This statement will not compile as 2.5 is a real number
  • 8.  double something =1000;  Though 1000 is an integer but this statement is legal as will not result in information loss. It will be considered as 1000.0  Character Assignment  char level = ‘A’;  Enclosed he character in single quotes  Assignment at the time of declaration  level =‘B’  Assignments changed
  • 9.  Data items in program have values that do not change  Following are examples of such items  Maximum score in a exam  Number of hours in a day  Mathematical value of  (3.14176)  In these cases the values do not vary  Values remain constant  Such items should be named and declared as constants  Add the keyword “final” before declaration  final int HOURS = 24  The statement HOURS = 12 will not compile now
  • 10. Operation Java Operator Addition + Subtraction - Multiplication * Division / Remainder %
  • 11. int x; x =10 + 25; double cost; cost = 500*(1+17.5/100); int x; x=30/4; What Will be the answer? 7 or 7.5? The answer is 7 as division operator is overloaded double price, tax, cost; price = 500; tax =17.5; cost =price+(1+tax/100); price =price+(1+tax/100);
  • 12.  X = X + 1;  X++;  X = X – 1;  X--;  int X = 5;  int Y = 0; Postfix Prefix Y=X--; Y=X++; Y=--X; Y=++X; Y value will be 5 Y value will be 6 4 X value will be 6 4 X value will be 6 4 •Y = Y + X; •Y + = X;
  • 13.
  • 14.
  • 15.  System.out.println(“Hello World”);  System.out.println(“Welcome to java world”); Hello World Welcome to java world  System.out.print(“Hello World”);  System.out.println(“Welcome to java world”); Hello WorldWelcome to java world  System.out.println(); Blank line in the program
  • 16.  Strings  Collection of characters  Always enclosed in speech marks “ “  Print statements print strings  Several strings can be combined using + operator  Concatenation Operator (+)  System.out.println(“Hello” + “World”);  HelloWorld  Spaces included in speech marks are printed  System.out.println(“Hello ” + “World”);  Hello World  System.out.println(10*10);  The instruction prints 100 on screen.  Java converts value/expression to a string before displaying it  As these numbers are converted into string so they can be combined  System.out.println(“Cost = ”+(10*10));  Cost = 100
  • 17.  Complex data type  Name  Address  Car Registration Number  Any meaningless sequence of characters  Declare it in the same was as declare variables  String Name;  Name = “Saira”; OR  String Name = “Saira”;
  • 18.  Part of Java release 5.0 and later  Class that makes it easy for us to write a program that obtains information that is typed in at the keyboard  Scanner is part of Java package called util  A package is a collection of pre-compiled classes  To make Scanner class accessible to compiler, we have to tell the compiler that it should look in util package  import java.util.*;  * means all classes in util package are made available  import java.util.Scanner;  Only Scanner class is accessible
  • 19.  Create Object  Scanner sc = new Scanner(System.in);  System.in represent KeyBoard  new instantiates a class by allocating memory for a new object and returning a reference to that memory.  Integer Input  int x;  x=sc.nextInt();  Double Input  double x;  x=sc.nextDouble();  String Input  String x;  x=sc.next(); OR x=sc.nextLine();  Character Input  char x;  x=sc.next().charAt(0);