Interesting Facts about YOU_

Document Sample
Interesting Facts about YOU_ Powered By Docstoc
					G51PRG-Sem2
Interesting Facts About
         YOU!
• You each made an average of
  thirty errors compiling each
  exercise last term
• For every error per person in
  the morning, there were 2.5
  errors in the afternoon



G51PRG                            1
G51PRG-Sem2

          The Moral
• You are morning people after
  all (unlike me)
• If I can make the TUEs and
  THUs at 9:00 – so can you!
• Come in early and do lots of
  programming


G51PRG                           2
G51PRG-Sem2

How to Avoid Over 80%
   of Your Mistakes




G51PRG              3
G51PRG-Sem2
         The Most Common
          Mistakes - ;;;;;;;;
• The most common error is
  missing a semicolon or a
  comma:

• String s = “Every line ends with a”;
• Fred.method(“arg1”, 12); // needs comma




G51PRG                                      4
G51PRG-Sem2
         Arguments and
           Operators
• The next most common error is
  missing a + between strings or
  similar:
    – System.Out.println(“root is “ + r);
    – People often put:
    – System.Out.println(“root is “, r);
• The number of errors went up
  a lot when methods were
  taught
G51PRG                                      5
G51PRG-Sem2

         Undefined variables
• The most common semantic
  error is using a variable which
  has not been defined or to a
  lesser extent not initialised
• Think carefully about types and
  your style of declaration


G51PRG                          6
G51PRG-Sem2

         Static Confusion
• non-static variable in a static
  context
• Oh Dear! The result of writing
  Fortran in Java…
• Avoid static except for main()
• Keep main() SHORT


G51PRG                              7
G51PRG-Sem2

Use of = in Conditionals
• If (x = 2) something
• In C or C++ this compiles
= is assignment

              ==
is the comparison operator

G51PRG                        8
G51PRG-Sem2

 Right Hand Side != LHS
• All variables have a type
• All expressions return a type
• These MUST be the same
• Well you can use a cast
int i = 0;
Double b = 10.2
i = (int) b;
G51PRG                            9
G51PRG-Sem2

         1. Compile and run:
public class Fred {
 public static void main() {
   System.out.println(“Hello”);
 }
}

What Happens?

G51PRG                            10
G51PRG-Sem2

         2. After Execution…
String [] types = new String[20];

What will the first element of the array contain?
a) An empty String?
b) The null value?
c) Zero?


G51PRG                                              11
G51PRG-Sem2

3. What will be Printed?
Integer x1 = new Integer(5);
Integer x2 = x1;
Integer x3 = new Integer(5);
if(x1 == x2)
  System.out.println(“x1 = x2”);
if(x1 == x3)
  System.out.println(“x1 = x3”);
G51PRG                         12
G51PRG-Sem2

4. What will be printed?
String s1 = “yes”;
String s2 = “yes”;
if(s1 == s2)
  System.out.println(“same”);
if(s1 != s2)
  System.out.println(“different”);
(very trick question!)
G51PRG                          13
G51PRG-Sem2

 5. What is printed out?
int [] counts;
System.out.println(“counts = “
  + counts);




G51PRG                           14
G51PRG-Sem2

 6. What is printed out?
int [] counts;
counts = new int[10];
System.out.println(“counts = “ +
  counts);




G51PRG                             15
G51PRG-Sem2

 7. What is printed out?

int [] ids;
ids = new int[5];
System.out.println(ids[2]);




G51PRG                        16
G51PRG-Sem2

 8. What is printed out?
String [] names;
names = new String[5];
System.out.println(names[2]);




G51PRG                          17
G51PRG-Sem2

         9. String to int
String age = “97”;
int freds_age;

How can I get freds_age from
age?



G51PRG                         18
G51PRG-Sem2

         10. int to String
String age;
int freds_age = 99;


How can I turn freds_age into a String?




G51PRG                                    19
G51PRG-Sem2

  11. What will happen?
public class Example {
  public Boolean [] flags = new Boolean[4];
  public static void main(String args[]) {
    Example E = new Example();
    System.out.println(“Flag 1 is “ +
     E.flags[1]);
  }
}


G51PRG                                    20
G51PRG-Sem2
     12. What will be the
           result?
public int adder(int N) {
  return 0x100 + N++;
}

In n of calling: n = adder(2);




G51PRG                           21
G51PRG-Sem2
    13. How many String
     Objects are created
           here?
String A, B, C;
A = new String(“1234”);
B = A;
C = A + B;


G51PRG                     22
G51PRG-Sem2


• 14. What keyword is used to
  derive once class from
  another?
• 15. What keyword is used to
  state that a certain set of
  methods must be implemented
  by a class?



G51PRG                      23
G51PRG-Sem2


16. A variable in a class is
 defined as:

protected int age;

Why does the keyword protected
 make age more visible!?


G51PRG                         24
G51PRG-Sem2

17. This is Student.java
Public class Student {
  String name;             What message is
  int     age;             generated
  boolean male;
                           by javac?
  Module [] subjects;
  public class Module {
     String module_code;
     int Semester;
     int mark;
  }
}

G51PRG                                       25
G51PRG-Sem2

          18. Which line will
         cause a compilation
               failure?
double fred;
boolean bill;
int    2fred;
String _lucy;
float   $ron;
G51PRG                          26
G51PRG-Sem2

19. Define the following
• A primitive
• An Object Reference
• Which can have the value zero?
• Which can have the value null?




G51PRG                         27

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:42
posted:4/5/2010
language:English
pages:27