Tutorial 2 Simple Java Programming Comp 202 -Intro to Computing

Document Sample
Tutorial 2 Simple Java Programming Comp 202 -Intro to Computing Powered By Docstoc
					Tutorial 2: Simple Java Programming
  Comp 202 – Intro to Computing

           Mahtab Nazari
            Winter 2009
•   Syntactic & Semantic Errors
•   Expressions & Data Types
•   Conversions
•   Writing Simple Programs
•   Notes on Assignment Submission
• Are caught by Compiler
• Produce consistent compile – time errors
• Depending on the language : Case-Sensitivity,
  White Space, etc.
• The following code contains 4 errors:

   public class SimpleClass
   {
     public static viod main(string[] args)
     {
           system.out.println(“Hello World”):
     }
   }
• Please compile it ....
• Not always are caught by compiler
• Sometimes compiler finds out. Exp: In case
  printing an uninitialized variable.
• Receiving 0 errors after compiling doesn’t
  mean the program is working     Logic Errors
  public class SimpleClass
  {
    public static void main(String[] args)
    {
          int number ;
          System.out.println(number);
    }
  }
• Please compile it ....
• Error is caught by compiler
import java.util.Scanner;
public class SimpleClass
{
  public static void main(String[] args)
  {
        Scanner keyboard = new Scanner(System.in);
        int number = keyboard.nextInt();
        System.out.println("1/" + number + "=" + (1/number) );
  }
}
• Please compile it ...
• Error in run- time .... ( 1 / 0) ! NAN
•   Division by 0
•   Incorrect bounds on comparison.
•   Integer division.
•   Incorrect exit conditions on loops
•   No exit condition in recursions
• Print-Line debugging works by placing
  System.out.println() calls thought-out your
  code.
• 2 purposes:
  – Checking to ensure a specific line of code has been
    run
  – Reflecting back the current value of a specific
    value
     System.out.println("1/" + number );
• Expressions elements
  – Variables /Constants
  – Operators
• Expression return values that can be data type
  or object.
• Expression is in “Bold Blue Text”
    – double x = 1.1;
•   Variable : x
•   Operator : =
•   Constant : 1.1
•   Value type : double
    int x = 1;
    int y = 2;
    boolean z = (x == y);


•   Variables: x, y, z
•   Operators: =, ==
•   Value types: int, boolean
•   z = false is the answer
• 8 fundamental types
1. boolean 2. byte   3. char   4. double   5. float   6. int   7. long   8. short


• 11 Operator

1.    Postfix ( x++ , x--)
2.    Unary ( ++x, +x , -x )
3.    Multiplicative ( *, / , % )
4.    Additive ( + , - )
5.    Shift ( << , >> , >>> )
6.    Relational ( < , > , <= , => )
7.    Equality ( == , |= )
8.    Bitwise ( & , ^ , | ) ^ : XOR
9.    Logical ( && , || )
10.   Ternary ( ?: )
11.   Assignment ( = , += , -= , *= , /= , %= , &= , ^= , |= )
• Transforms variables from one type to another
  type. Exp : short int
• Two types of conversion:
    1. Widening : Kind of useless
    2. Narrowing : Loosing data
1                          2
• Occurs when value of one type assigned to a variable of
  another type

   public class AssignConv
   {
     public static void main(String[] args)
     {
           int x = 1;
           float y = x;
           System.out.println(y);
     }
   }

OUTPUT : y = 1.0
• Converting between two types can be done by cast
   public class CastConv
   {
     public static void main(String[] args)
     {
           double x = 2.5;
           int y = (int) x;
           System.out.println(y);
     }
   }

OUTPUT : y = 2
• Occurs due to operators and operands
   public class ProConv
   {
     public static void main(String[] args)
     {
           float sum = 1.5 + 2.4 + 5.7;
           int con = 3;
           float avg = sum / con;
           System.out.println(avg);
     }
   }

con is “int” but converts to “float” for division .
• Write a program for the Vieta's Theorem in the
  Quadratic Equation .
• If the two roots of the equation are “a” and “b”, then
  the Quadratic equation can be written as
  x ^ 2- (a+b) x + ab=0
• Requirement: Enter the two roots and display the
  equation.
import java.util.Scanner;
public class Quad{
   public static void main(String[] args) {
         Scanner keyboard = new Scanner(System.in);
         int sum, mul, a, b;
         System.out.print("Please enter first root: ");
         a = keyboard.nextInt();
         System.out.print("Please enter second root: ");
         b = keyboard.nextInt();
         sum = - (root1 + root2);
         mul = root1 * root2;
         System.out.println("Your quadratic is : x^2 + (" + sum + ")x + " +
   "(" + mul + ")");
   }
}
Write a program which consists of a single class called
  BMI Calculator. This class must define a method
  called main() which does the following:
(1) Asks the user to enter his/her weight (in kilograms)
  and
his/her height (in meters)
(2) Calculates the user's Body Mass Index (BMI); a
  person‘s BMI can be computed by dividing his /her
  weight in kilograms by the square of his/ her height in
  meters.
(3) Displays the user's BMI it to the screen
import java.util.Scanner;
public class Quad{
  public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        double Weight, Height, BMI;
        S ystem.out.print("Please enter your Weight : ");
        Weight = keyboard.nextInt();
        S ystem.out.print("Please enter second Height: ");
        Height = keyboard.nextInt();
        BMI = Weight / (Height * Height);
        S                     our
          ystem.out.println("Y BMI is : " + BMI );
  }
}
This document was created with Win2PDF available at http://www.win2pdf.com.
The unregistered version of Win2PDF is for evaluation or non-commercial use only.
This page will not be added after purchasing Win2PDF.