Comp102   lec 4
Upcoming SlideShare
Loading in...5
×
 

Comp102 lec 4

on

  • 316 views

 

Statistics

Views

Total Views
316
Views on SlideShare
316
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Comp102   lec 4 Comp102 lec 4 Presentation Transcript

  • 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 OperatorAddition +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 overloadeddouble 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);