Java for Interfaces and Networks (DT3010, HT11)

Size: px
Start display at page:

Download "Java for Interfaces and Networks (DT3010, HT11)"

Transcription

1 Java for Interfaces and Networks (DT3010, HT11) Introduction Federico Pecora School of Science and Technology Örebro University Federico Pecora Java for Interfaces and Networks Lecture 1 1 / 52

2 This course... Outline 1 This course... 2 What is Java? 3 Java applications 4 Syntax and Data types 5 Object Orientation in Java Federico Pecora Java for Interfaces and Networks Lecture 1 2 / 52

3 This course... Preliminary course plan Week 45 Week 46 Week 47 Introduction and Basics Classes, Interfaces and Packages Exceptions and Garbage Collection Applets Introduction to Swing Threads Networking Networking with Threads Container Classes Java Database Connectivity (JDBC) Week 48 Week 49 Week 50 Standard Methods Inner Classes JavaDoc Garbage Collection (cont) Swing (cont) Threads (cont) Mouse Events Timers Serialization Week 51-2 Self study Exam (week 2) Federico Pecora Java for Interfaces and Networks Lecture 1 3 / 52

4 This course... Course details Staff Labs Exam Federico Pecora (lectures) room T2222, tel Jonas Ullberg (teaching assistant) room T1219, tel project assignments these must be submitted you do not have to submit labs but attending and completing labs greatly facilitates assignments! written examination which involves designing a software application Federico Pecora Java for Interfaces and Networks Lecture 1 4 / 52

5 This course... Exam Details Exam will involve writing some code (mostly to complete an already given structure) 4 exercises, 30 points in total, 15 required to pass Points Swedish system 1: [16,22] = G, [23,30] = VG Points Swedish system 2: [15,19] = 3, [20,25] = 4, [26,30] = 5 Points ECTS: [15,17] = E, [18,20] = D, [21,23] = C, [24,27] = B, [28,30] = A Federico Pecora Java for Interfaces and Networks Lecture 1 5 / 52

6 What is Java? Outline 1 This course... 2 What is Java? 3 Java applications 4 Syntax and Data types 5 Object Orientation in Java Federico Pecora Java for Interfaces and Networks Lecture 1 6 / 52

7 What is Java? C, C++ and Java Let s compare a simple hello world program: hello.c 1 #include <stdio.h> 2 int main(int argc, char* argv[]) { 3 printf("hello, world!\n"); 4 return 0; 5 } /* main */ linux> gcc -Wall hello.c -o hello linux>./hello Hello, world! linux> Federico Pecora Java for Interfaces and Networks Lecture 1 7 / 52

8 What is Java? C, C++ and Java Let s compare a simple hello world program: hello.cpp 1 #include <iostream> 2 int main(int argc, char* argv[]) { 3 std::cout << "Hello, world!\n"; 4 } // main linux> g++ -Wall hello.cpp -o hello linux>./hello Hello, world! linux> Federico Pecora Java for Interfaces and Networks Lecture 1 7 / 52

9 What is Java? C, C++ and Java Let s compare a simple hello world program: Hello.java 1 public class Hello { 2 public static void main(string[] args) { 3 System.out.println("Hello, world!"); 4 } 5 } // Hello linux> javac Hello.java linux> java Hello Hello, world! linux> Federico Pecora Java for Interfaces and Networks Lecture 1 7 / 52

10 What is Java? C, C++ and Java Let s compare a simple hello world program: Hello.java 1 public class Hello { 2 public static void main(string[] args) { 3 System.out.println("Hello, world!"); 4 } 5 } // Hello linux> javac Hello.java linux> java Hello Hello, world! linux> javac (the compiler) does not produce an ordinary executable file! Federico Pecora Java for Interfaces and Networks Lecture 1 7 / 52

11 What is Java? Java files: *.java, *.class, *.jar Source code goes in <ClassName>.java javac compiles source into <ClassName>.class contains bytecode Class libraries can be located in filesystem as (.class files) or in ZIP or JAR files Federico Pecora Java for Interfaces and Networks Lecture 1 8 / 52

12 What is Java? Java Virtual Machine Java compiler does not compile source to machine instructions, rather it produces bytecode The Java Virtual Machine (JVM) interprets the bytecode Bytecode is minimal to facilitate transmission over a network Bytecode is totally portable a Java brogram compiled in Linux can run in Windows (etc.) Bytecode can be compiled into platform-specific code Federico Pecora Java for Interfaces and Networks Lecture 1 9 / 52

13 What is Java? Java Virtual Machine *.java *.class Java source code Java bytecode Java compiler JVM Bytecode compiler Machine code Federico Pecora Java for Interfaces and Networks Lecture 1 10 / 52

14 What is Java? Microsoft C# Hello.cs 1 World class Hello { 2 public static void Main() { 3 System.Console.WriteLine("Hello, world!"); 4 } 5 } Microsoft C# is very similar to Java One view is that C# (and the whole.net architecture), is as close as legally possible to Java... Federico Pecora Java for Interfaces and Networks Lecture 1 11 / 52

15 What is Java? C and C++ can be hazardous This program contains a serious flaw... helloname.c 1 #include <stdio.h> 2 int main(int argc, char* argv[]) { 3 printf("what s your name: "); 4 char* s; 5 gets(s); 6 printf("hello, %s!\n", s); 7 return 0; 8 } /* main */ linux>./helloname What s your name: Bob Hello, Bob! linux> Federico Pecora Java for Interfaces and Networks Lecture 1 12 / 52

16 What is Java? C and C++ can be hazardous This program contains a serious flaw... helloname.c 1 #include <stdio.h> 2 int main(int argc, char* argv[]) { 3 printf("what s your name: "); 4 char* s; 5 gets(s); 6 printf("hello, %s!\n", s); 7 return 0; 8 } /* main */ linux>./helloname What s your name: Bob with a very long name Hello, Bob with a very long name! Segmentation fault (core dumped) linux> Federico Pecora Java for Interfaces and Networks Lecture 1 12 / 52

17 What is Java? A simple program Read lengths of two sides of a rectangle Output area and perimeter Console text-based interface RectangleCalculator.java 1 import java.io.*; 2 class RectangleCalculator { 3 public static void main(string args[]) { 4 int a = 0, b = 0; String buf; 5 BufferedReader kbd_reader 6 = new BufferedReader( 7 new InputStreamReader(System.in)); 8 try { 9 System.out.print("Input side a: "); 10 buf = kbd_reader.readline(); 11 a = Integer.parseInt(buf); 12 //continues... Federico Pecora Java for Interfaces and Networks Lecture 1 13 / 52

18 What is Java? A simple program Read lengths of two sides of a rectangle Output area and perimeter Console text-based interface RectangleCalculator.java 1 //... continued 2 System.out.print("Input side b: "); 3 buf = kbd_reader.readline(); 4 b = Integer.parseInt(buf); 5 System.out.println("Area: " + a * b); 6 System.out.println("Perimeter: " + 2*a + 2*b); 7 } 8 catch (IOException e) { 9 System.out.println("Read error!"); 10 } 11 } // main 12 } // RectangleCalculator Federico Pecora Java for Interfaces and Networks Lecture 1 13 / 52

19 What is Java? A simple program This program contains a logical error... RectangleCalculator.java 1 //... 2 System.out.print("Input side a: "); 3 buf = kbd_reader.readline(); 4 a = Integer.parseInt(buf); 5 //... linux> javac RectangleCalculator.java linux> java RectangleCalculator Input side a: 5 Input side b: 4 Area: 20 Perimeter: 108 linux> Federico Pecora Java for Interfaces and Networks Lecture 1 14 / 52

20 What is Java? A simple program This program contains a logical error... RectangleCalculator.java 1 //... 2 System.out.print("Input side a: "); 3 buf = kbd_reader.readline(); 4 a = Integer.parseInt(buf); 5 //... linux> java RectangleCalculator Input side a: Kalle Anka Exception in thread "main" java.lang.numberformatexception: For input string: "Kalle Anka" at java.lang.numberformatexception.forinputstring( NumberFormatException.java:48) at java.lang.integer.parseint(integer.java:447) at java.lang.integer.parseint(integer.java:497) at RectangleCalculator.main(RectangleCalculator.java:15) linux> Federico Pecora Java for Interfaces and Networks Lecture 1 14 / 52

21 What is Java? A simple program... what would happen in C++? rectanglecalculator.cpp 1 //... 2 cout << "Input side a: "; 3 cin >> a; 4 cout << "Input side b: "; 5 cin >> b; 6 cout << "Area: " << a * b << ", " << 2*a + 2*b << "\n"; 7 //... linux> g++ rectanglecalculator.cpp -o rectanglecalculator linux>./rectanglecalculator Input side a: Kalle Anka Input side b: Area: 0, Perimeter: 0 linux> Federico Pecora Java for Interfaces and Networks Lecture 1 15 / 52

22 What is Java? A simple program Java provides exception handling capabilities, which are useful for detecting/handling run-time errors... but Java does not safeguard against all run-time contingencies! linux> java RectangleCalculator Input side a: Input side b: Area: Perimeter: linux> Federico Pecora Java for Interfaces and Networks Lecture 1 16 / 52

23 Java applications Outline 1 This course... 2 What is Java? 3 Java applications 4 Syntax and Data types 5 Object Orientation in Java Federico Pecora Java for Interfaces and Networks Lecture 1 17 / 52

24 Java applications The main method The main() method (as in C/C++) is invoked when you execute a class with the Java interpreter The runtime system starts by calling the class s main() method The controlling class of every Java application must contain a main() method having one of the following signatures public static void main(string[] args) public static void main(string args[]) Federico Pecora Java for Interfaces and Networks Lecture 1 18 / 52

25 Java applications The main method public static void main(string[] args) public static void main(string args[]) public: the method can be called by any object (we will discuss the keywords public, private, and protected in more detail) static: the method can be called without instantiating an object of the class (we will discuss class methods/variables) void: the method doesn t return any value args: an array of String objects, which contains command line arguments entered by the user Federico Pecora Java for Interfaces and Networks Lecture 1 19 / 52

26 Java applications The main method: a method of class Main Rectangle.java 1 public class Rectangle { 2 public Rectangle(int a, int b) { 3 this.a = a; 4 this.b = b; 5 } 6 public int perimeter() { 7 return 2 * this.a + 2 * this.b; 8 } 9 public int area() { 10 return a * b; 11 } 12 private int a = 0, b = 0; 13 } // Rectangle Federico Pecora Java for Interfaces and Networks Lecture 1 20 / 52

27 Java applications The main method: a method of class Main Main.java 1 import java.io.*; 2 public class Main { 3 public static void main(string args[]) { 4 BufferedReader kbd_reader; 5 //... 6 int a, b; 7 String buf; 8 try { 9 //... get a and b from user, then: 10 Rectangle rect = new Rectangle(a,b); 11 System.out.println("Area: " + rect.area()); 12 System.out.println("Perimeter: " + 13 rect.perimeter()); 14 } 15 catch (IOException e) { /*... */ } 16 } // main 17 } // Main Federico Pecora Java for Interfaces and Networks Lecture 1 20 / 52

28 Java applications The main method: a method of class Main linux> javac Rectangle.java linux> javac Main.java linux> java Main Input side a: 5 Input side b: 6 Area: 30 Perimeter: 22 linux> Federico Pecora Java for Interfaces and Networks Lecture 1 21 / 52

29 Java applications The main method: a method of class Rectangle Rectangle.java 1 public class Rectangle { 2 public Rectangle(int a, int b) { /*... */ } 3 public int perimeter() { /*... */ } 4 public int area() { /*... */ } 5 private int a = 0, b = 0; 6 public static void main(string[] args) { 7 try { 8 //... read input from user, then: 9 Rectangle rect = new Rectangle(a,b); 10 System.out.println("Area: " + rect.area()); 11 System.out.println("Perimeter: " + 12 rect.perimeter()); 13 } catch( /*... */ ) { /*... */ } 14 } 15 } // Rectangle Federico Pecora Java for Interfaces and Networks Lecture 1 22 / 52

30 Java applications The main method: a method of class Rectangle linux> javac Rectangle.java linux> java Rectangle Input side a: 5 Input side b: 6 Area: 30 Perimeter: 22 linux> Federico Pecora Java for Interfaces and Networks Lecture 1 23 / 52

31 Java applications A simple program Applications run through a Java Virtual Machine (JVM) Applets run in a web browser (through a plugin which provides a JVM) Hello.java 1 import java.applet.*; 2 import java.awt.*; 3 public class Hello extends Applet { 4 public void paint(graphics gr) { 5 gr.drawstring("hello, world!", 100, 100); 6 } //paint 7 } //Hello NB: this will not run with java command, as there is no main() method Federico Pecora Java for Interfaces and Networks Lecture 1 24 / 52

32 Java applications A simple program Applications run through a Java Virtual Machine (JVM) Applets run in a web browser (through a plugin which provides a JVM) hello.html 1 <html> 2 <head> 3 <title>an applet example</title> 4 </head> 5 <body> 6 <applet code="hello.class" width="200" height="200"/> 7 </body> 8 </html> Federico Pecora Java for Interfaces and Networks Lecture 1 24 / 52

33 Java applications A simple program Applications run through a Java Virtual Machine (JVM) Applets run in a web browser (through a plugin which provides a JVM) Federico Pecora Java for Interfaces and Networks Lecture 1 24 / 52

34 Syntax and Data types Outline 1 This course... 2 What is Java? 3 Java applications 4 Syntax and Data types 5 Object Orientation in Java Federico Pecora Java for Interfaces and Networks Lecture 1 25 / 52

35 Syntax and Data types Basic syntax Syntax in Java is much like in C++ Assignment is done with =, comparison with ==... although you cannot override these two operators in Java! Arithmetic operators: +, -, *, /, ++, -, +=, /=,... Control flow: if, while, do...while, for, switch, break, continue. Comments 1 // Extends over the rest of the line 2 3 /* A comment over 4 several 5 lines */ 6 Federico Pecora Java for Interfaces and Networks Lecture 1 26 / 52

36 Syntax and Data types Data types Class (a struct with methods ) e.g., String, a class for working with strings e.g., Rectangle, a class for introducing Java to students in this course Primitive types byte, short int, long for integers char for characters float, double for floating point boolean for logical values (can be true or false) Primitive types also have wrapper classes which provide convenience methods associated to the primitive type e.g., Integer for int provides method parseint(string) for reading an integer from a String Federico Pecora Java for Interfaces and Networks Lecture 1 27 / 52

37 Syntax and Data types Primitive data types Type Size Range boolean 1 bit false or true char 16 bits Unicode characters byte 8 bits short 16 bits int 32 bits long 64 bits float 32 bits E E-45 double 64 bits E E-324 Federico Pecora Java for Interfaces and Networks Lecture 1 28 / 52

38 Syntax and Data types Variable declaration Can be done anywhere within a block ({... }) a block defines the scope of a variable Objects are declared with new e.g., Rectangle rect = new Rectangle(2,3); As in C/C++ you can do int i; or int i = 5; NB: in Java it is a good idea to maintain a variable s scope as small as possible Federico Pecora Java for Interfaces and Networks Lecture 1 29 / 52

39 Syntax and Data types Arrays Java has several different "container" types, which can contain other items, such as lists, sets and mappings None of these types work like in C/C++! Built-in, fixed-size arrays: String[] fruits = ("Apples", "Pears", "Oranges"); For dynamic sets of objects: Vector, ArrayList, LinkedList Federico Pecora Java for Interfaces and Networks Lecture 1 30 / 52

40 Syntax and Data types Arrays Built-in arrays can store both primitive types and objects Usually instantiated with new Example 1 int[] intarray = new int[10]; 2 int intarray[] = new int[10]; 3 String[] fruits = { "Apples", "Pears", "Oranges" }; 4 int intarray[10]; //Compilation error! 5 6 //Iterating over an array (method 1) 7 for (int i = 0; i < intarray.length; i++) { 8 System.out.println("intarray[" + i + "] = " 9 + intarray[i]); 10 } //Iterating over an array (method 2) 13 for (String fruit : fruits) { 14 System.out.println("fruits contains: " + fruit); 15 } Federico Pecora Java for Interfaces and Networks Lecture 1 31 / 52

41 Syntax and Data types More differences between Java and C/C++ Garbage collection and how objects are handled No pointers (or, better, everything is a pointer ) Parameters always transferred by value Exception handling Standard library (very large) Classes and files (no.h files) Environment-independence (e.g., window management in C++ is very different in Windows, Linux and OS/X) Federico Pecora Java for Interfaces and Networks Lecture 1 32 / 52

42 Object Orientation in Java Outline 1 This course... 2 What is Java? 3 Java applications 4 Syntax and Data types 5 Object Orientation in Java Federico Pecora Java for Interfaces and Networks Lecture 1 33 / 52

43 Object Orientation in Java Classes and inheritance An animal has a name, a weight, and it can eat Different types of animals exist: fish and birds Fish Bird an animal that can swim has a maximum depth at which it can swim an animal that can fly has a maximum altitude at which it can fly flutters its wings when it eats Federico Pecora Java for Interfaces and Networks Lecture 1 34 / 52

44 Object Orientation in Java Animal class An animal has a name and eats Animal.java 1 class Animal { 2 protected String name; 3 private int weight; 4 public Animal(String n, int w) { 5 name = n; 6 weight = w; 7 } 8 public void eat(string food) { 9 System.out.println(name + " eats some " + food + "."); 10 weight += 0.5; 11 } 12 } //class Animal Federico Pecora Java for Interfaces and Networks Lecture 1 35 / 52

45 Object Orientation in Java Fish class An fish can swim at a maximum depth Fish.java 1 class Fish extends Animal { 2 private int maxdepth; 3 public Fish(String n, int w, int d) { 4 super(n, w); 5 maxdepth = d; 6 } 7 public void swim() { 8 System.out.println(name + " swims."); 9 } 10 } //class Fish Federico Pecora Java for Interfaces and Networks Lecture 1 36 / 52

46 Object Orientation in Java Bird class An bird can fly at a maximum altitude and flutters when eating Bird.java 1 class Bird extends Animal { 2 private int maxaltitude; 3 public Bird(String n, int w, int a) { 4 super(n, w); 5 maxaltitude = a; 6 } 7 public void fly() { 8 System.out.println(name + " flies."); 9 } 10 public void eat(string food) { 11 System.out.println(name + " flutters its wings."); 12 super.eat(food); 13 } 14 } //class Bird Federico Pecora Java for Interfaces and Networks Lecture 1 37 / 52

47 Object Orientation in Java The Zoo test class Let s put all animals together in a test class Zoo.java 1 class Zoo { 2 public static void main(string[] args) { 3 Bird tweety = new Bird("Tweety", 3, 1000); 4 tweety.eat("corn"); 5 tweety.fly(); 6 7 Fish b = new Fish("Bubbles", 13, 100); 8 b.eat("fish food"); 9 b.swim(); Animal w = new Fish("Willy", 4000, 1000); 12 w.eat("tourists"); 13 // w.swim(); -- Doesn t work. See below. 14 } 15 } //class Zoo Federico Pecora Java for Interfaces and Networks Lecture 1 38 / 52

48 Object Orientation in Java The Zoo test class Let s put all animals together in a test class linux> javac *.java linux> java Zoo Tweety flutters its wings. Tweety eats some corn. Tweety flies. Bubbles eats some fish food. Bubbles swims. Willy eats some tourists. linux> Federico Pecora Java for Interfaces and Networks Lecture 1 38 / 52

49 Object Orientation in Java Inheritance and method overriding The Bird class has its own method called eat(), which overrrides the eat() method in Animal Overriding you have a method in a subclass with the same signature of a method in the superclass If you want to call the superclass method despite the existence of an overriding method, use the super keyword (e.g., super.eat()) This is done in the definition of Bird.eat() Federico Pecora Java for Interfaces and Networks Lecture 1 39 / 52

50 Object Orientation in Java Inheritance and method overriding Notice that w is defined as an object of type Animal w.swim() will give a compilation error because Animal has no method swim() Variable w contains Willy (a Fish), which has a swim() method Therefore it should be possible to call it when the program is running but the compiler must be sure that whatever is in w will always have a swim() method One way to promise the compiler that this will always be true is to use a cast: ((Fish)w).swim() Federico Pecora Java for Interfaces and Networks Lecture 1 40 / 52

51 Object Orientation in Java Polymorphism The world consists of figures, and a Figure can be either a Rectangle or a Circle A Figure has a center, as indicated by the coordinates x_center and y_center One can calculate the area and perimeter of a Figure, as well as its maximum x and y coordinates Note that how to calculate the area and perimeter depends entirely on the type of figure! Federico Pecora Java for Interfaces and Networks Lecture 1 41 / 52

52 Object Orientation in Java Figure class A Figure must implement area and perimeter methods Figure.java 1 public abstract class Figure { 2 public Figure(int x, int y) { 3 this.x_center = x; 4 this.y_center = y; 5 } //Figure 6 public abstract int max_x(); 7 public abstract int max_y(); 8 public abstract int area(); 9 public abstract int perimeter(); 10 protected int x_center, y_center; 11 } //class Figure Federico Pecora Java for Interfaces and Networks Lecture 1 42 / 52

53 Object Orientation in Java Rectangle class A Rectangle is a kind of Figure, defined by side and height attributes Rectangle.java 1 public class Rectangle extends Figure { 2 public Rectangle(int x, int y, int s, int h) { 3 super(x, y); 4 this.side = s; 5 this.height = h; 6 } //Rectangle 7 public int max_x() { return super.x_center+this.side/2; } 8 public int max_y() { return super.x_center+this.height/2; } 9 public int area() { return this.side*this.height; } 10 public int perimeter() { 11 return 2*this.side+2*this.height; 12 } 13 private int side, height; 14 } //class Rectangle Federico Pecora Java for Interfaces and Networks Lecture 1 43 / 52

54 Object Orientation in Java Rectangle class A Circle is a kind of Figure, defined by a radius Circle.java 1 public class Circle extends Figure { 2 public Circle(int x, int y, int radius) { 3 super( x, y ); 4 this.radius = radius; 5 } //Circle 6 public int max_x() { return super.x_center+this.radius; } 7 public int max_y() { return super.y_center+this.radius; } 8 public int area() { 9 return (int)(math.pi*this.radius*this.radius); 10 } //area 11 public int perimeter() { 12 return (int)(2*math.pi*this.radius); 13 } //perimeter 14 public int diameter() {return this.radius+this.radius; } 15 private int radius; 16 } //class Circle Federico Pecora Java for Interfaces and Networks Lecture 1 44 / 52

55 Object Orientation in Java The FigureMain test class Let s put all figures together in a test class FigureMain.java 1 public class FigureMain { 2 public static void main(string args[]) { 3 Figure[] figures = new Figure[4]; 4 /* x y side height */ 5 figures[0] = new Rectangle(10, 10, 10, 10); 6 /* x y radius */ 7 figures[1] = new Circle(10, 10, 10); 8 /* x y side height */ 9 figures[2] = new Rectangle(20, 20, 20, 20); 10 /* x y radius */ 11 figures[3] = new Circle(20, 20, 20); for (int index = 0; index < figures.length; index++) { 14 //continues... Federico Pecora Java for Interfaces and Networks Lecture 1 45 / 52

56 Object Orientation in Java The FigureMain test class Let s put all figures together in a test class FigureMain.java 1 //... continued 2 if (figures[index] instanceof Circle) { 3 System.out.println("Circle:"); 4 System.out.println(" Diameter: " + 5 ((Circle)figures[index]).diameter() ); 6 } 7 else { System.out.println("Rectangle:"); } 8 System.out.println(" Highest x: " + 9 figures[index].max_x()); 10 System.out.println(" Highest y: " + 11 figures[index].max_y()); 12 System.out.println(" Area: " + 13 figures[index].area() ); 14 System.out.println(" Perimeter: " + 15 figures[index].perimeter()); 16 } //for 17 } //main 18 } //class FigureMain Federico Pecora Java for Interfaces and Networks Lecture 1 45 / 52

57 Object Orientation in Java The FigureMain test class Let s put all figures together in a test class linux> java FigureMain Rectangle: Highest x: 15 Highest y: 15 Area: 100 Perimeter: 40 Circle: Diameter: 20 Highest x: 20 Highest y: 20 Area: 314 Perimeter: 62 Rectangle: Highest x: 30 Highest y: 30 Area: 400 Perimeter: 80 Circle: Diameter: 40 Highest x: 40 Highest y: 40 Area: 1256 Perimeter: 125 linux> Federico Pecora Java for Interfaces and Networks Lecture 1 45 / 52

58 Object Orientation in Java What is a Java class? A class represents a type of objects which have attributes and can perform operations A collection of instance methods (in C++, member functions) the operations performed by objects of this type A collection of instance variables (in C++, member variables) the attributes that define the type of objects A collection of class variables (in C++, static-declared member variables) attributes that are the same for all objects of this type A collection of static methods Federico Pecora Java for Interfaces and Networks Lecture 1 46 / 52

59 Object Orientation in Java What is a Java object? An object is an instance of a class Circle circle; value is null by default circle = new Circle( 10, 10, 10 ); The variable circle is a reference to an object of type Circle Circle circle2; circle2 = circle; Now circle and circle2 both refer to the same object C++ s delete() does not exist in Java, which has automatic garbage collection Federico Pecora Java for Interfaces and Networks Lecture 1 47 / 52

60 Object Orientation in Java General class definition syntax <modifier> class <name> [ extends <baseclassname> ] [ implements <interfacename> ] {... <variable definitions>... <method definitions>... } Federico Pecora Java for Interfaces and Networks Lecture 1 48 / 52

61 Object Orientation in Java Accessing variables/methods inside an object radius variable in Circle is accessible only in the class methods: private int radius; To make it accessible outside we declare it public: private int radius; Now we can access radius from, e.g., the main method in FigureMain: Circle circle = new Circle( 10, 10, 10 ); circle.radius = 27; much like structs in C The same goes for methods: we can define private methods for use only inside the class Federico Pecora Java for Interfaces and Networks Lecture 1 49 / 52

62 Object Orientation in Java Overriding and Overloading Overriding: a method in a derived class has the same signature as a method in the superclass used to give a specific definition to a method in the derived class Bird.eat(String) overrides Animal.eat(String) Federico Pecora Java for Interfaces and Networks Lecture 1 50 / 52

63 Object Orientation in Java Overriding and Overloading Overloading: a method which has the same name but different signature of another method in the same class used to give provide different functionality depending on the specified parameters public void DrawShape(int x1, int y1, int x2, int y2) { /* draw a rectangle */ } public void DrawShape(int x1, int y1) { /* draw a line */ } Federico Pecora Java for Interfaces and Networks Lecture 1 50 / 52

64 Object Orientation in Java Constructors One or more methods with the same name of the class No return type! If no constructors are defined, a default constructor with no arguments that does nothing is provided by default Multiple constructors overloading public Figure (int x, int y) { /*... */ } public Figure () { /*... */ } Federico Pecora Java for Interfaces and Networks Lecture 1 51 / 52

65 Object Orientation in Java Introduction Thank you! Federico Pecora Java for Interfaces and Networks Lecture 1 52 / 52

Java for Interfaces and Networks (DT3029)

Java for Interfaces and Networks (DT3029) Java for Interfaces and Networks (DT3029) Lecture 1 Introduction and Basics Federico Pecora federico.pecora@oru.se Center for Applied Autonomous Sensor Systems (AASS) Örebro University, Sweden Image source:

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

15CS45 : OBJECT ORIENTED CONCEPTS

15CS45 : OBJECT ORIENTED CONCEPTS 15CS45 : OBJECT ORIENTED CONCEPTS QUESTION BANK: What do you know about Java? What are the supported platforms by Java Programming Language? List any five features of Java? Why is Java Architectural Neutral?

More information

Java for Interfaces and Networks (DT3010, HT10)

Java for Interfaces and Networks (DT3010, HT10) Java for Interfaces and Networks (DT3010, HT10) More Basics: Classes, Exceptions, Garbage Collection, Interfaces, Packages Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se

More information

Computer Science II (20073) Week 1: Review and Inheritance

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

CMSC 331 Second Midterm Exam

CMSC 331 Second Midterm Exam 1 20/ 2 80/ 331 First Midterm Exam 11 November 2003 3 20/ 4 40/ 5 10/ CMSC 331 Second Midterm Exam 6 15/ 7 15/ Name: Student ID#: 200/ You will have seventy-five (75) minutes to complete this closed book

More information

Computer Science II (20082) Week 1: Review and Inheritance

Computer Science II (20082) Week 1: Review and Inheritance Computer Science II 4003-232-08 (20082) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Syntax and Semantics of Formal (e.g. Programming) Languages Syntax

More information

CS260 Intro to Java & Android 03.Java Language Basics

CS260 Intro to Java & Android 03.Java Language Basics 03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance

More information

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

(800) Toll Free (804) Fax   Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days Course Description This course introduces the Java programming language and how to develop Java applications using Eclipse 3.0. Students learn the syntax of the Java programming language, object-oriented

More information

Introduction to Java

Introduction to Java Introduction to Java Module 1: Getting started, Java Basics 22/01/2010 Prepared by Chris Panayiotou for EPL 233 1 Lab Objectives o Objective: Learn how to write, compile and execute HelloWorld.java Learn

More information

CS 11 java track: lecture 1

CS 11 java track: lecture 1 CS 11 java track: lecture 1 Administrivia need a CS cluster account http://www.cs.caltech.edu/ cgi-bin/sysadmin/account_request.cgi need to know UNIX www.its.caltech.edu/its/facilities/labsclusters/ unix/unixtutorial.shtml

More information

Brief Summary of Java

Brief Summary of Java Brief Summary of Java Java programs are compiled into an intermediate format, known as bytecode, and then run through an interpreter that executes in a Java Virtual Machine (JVM). The basic syntax of Java

More information

Selected Questions from by Nageshwara Rao

Selected Questions from  by Nageshwara Rao Selected Questions from http://way2java.com by Nageshwara Rao Swaminathan J Amrita University swaminathanj@am.amrita.edu November 24, 2016 Swaminathan J (Amrita University) way2java.com (Nageshwara Rao)

More information

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java Goals Understand the basics of Java. Introduction to Java Write simple Java Programs. 1 2 Java - An Introduction Java is Compiled and Interpreted Java - The programming language from Sun Microsystems Programmer

More information

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Lecture Notes Chapter #9_b Inheritance & Polymorphism Lecture Notes Chapter #9_b Inheritance & Polymorphism Inheritance results from deriving new classes from existing classes Root Class all java classes are derived from the java.lang.object class GeometricObject1

More information

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application Last Time v We created our first Java application v What are the components of a basic Java application? v What GUI component did we use in the examples? v How do we write to the standard output? v An

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries 1 CONTENTS 1. Introduction to Java 2. Holding Data 3. Controllin g the f l o w 4. Object Oriented Programming Concepts 5. Inheritance & Packaging 6. Handling Error/Exceptions 7. Handling Strings 8. Threads

More information

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber Chapter 10 Inheritance and Polymorphism Dr. Hikmat Jaber 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the

More information

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8 Today... Java basics S. Bowers 1 of 8 Java main method (cont.) In Java, main looks like this: public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Q: How

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA

B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE

More information

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail. OOP in Java 1 Outline 1. Getting started, primitive data types and control structures 2. Classes and objects 3. Extending classes 4. Using some standard packages 5. OOP revisited Parts 1 to 3 introduce

More information

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine Homework 6 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 21, 2013 Question 1 What is the output of the following C++ program? #include #include using namespace

More information

Core JAVA Training Syllabus FEE: RS. 8000/-

Core JAVA Training Syllabus FEE: RS. 8000/- About JAVA Java is a high-level programming language, developed by James Gosling at Sun Microsystems as a core component of the Java platform. Java follows the "write once, run anywhere" concept, as it

More information

Programming Language Concepts: Lecture 2

Programming Language Concepts: Lecture 2 Programming Language Concepts: Lecture 2 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 2, 19 January 2009 Classes and

More information

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class. Name: Covers Chapters 1-3 50 mins CSCI 1301 Introduction to Programming Armstrong Atlantic State University Instructor: Dr. Y. Daniel Liang I pledge by honor that I will not discuss this exam with anyone

More information

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2 CITS2200 Data Structures and Algorithms Topic 2 Java Primer Review of Java basics Primitive vs Reference Types Classes and Objects Class Hierarchies Interfaces Exceptions Reading: Lambert and Osborne,

More information

An overview of Java, Data types and variables

An overview of Java, Data types and variables An overview of Java, Data types and variables Lecture 2 from (UNIT IV) Prepared by Mrs. K.M. Sanghavi 1 2 Hello World // HelloWorld.java: Hello World program import java.lang.*; class HelloWorld { public

More information

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours Java for Programmers Course (equivalent to SL 275) 36 Contact Hours Course Overview This course teaches programmers the skills necessary to create Java programming system applications and satisfies the

More information

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program

More information

Developed in the beginning of the 1990 s by James Gosling from Sun Microsystems. Formally announced in a major conference in 1995.

Developed in the beginning of the 1990 s by James Gosling from Sun Microsystems. Formally announced in a major conference in 1995. JAVA Developed in the beginning of the 1990 s by James Gosling from Sun Microsystems. Formally announced in a major conference in 1995. Java programs are translated (compiled) into Byte Code format. The

More information

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles, Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design

More information

(2½ hours) Total Marks: 75

(2½ hours) Total Marks: 75 (2½ hours) Total Marks: 75 N. B.: (1) All questions are compulsory. (2) Makesuitable assumptions wherever necessary and state the assumptions mad (3) Answers to the same question must be written together.

More information

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

Lecture 1: Overview of Java

Lecture 1: Overview of Java Lecture 1: Overview of Java What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed for easy Web/Internet applications Widespread

More information

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003 Outline Object Oriented Programming Autumn 2003 2 Course goals Software design vs hacking Abstractions vs language (syntax) Java used to illustrate concepts NOT a course about Java Prerequisites knowledge

More information

CS2141 Software Development using C/C++ C++ Basics

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

25. Generic Programming

25. Generic Programming 25. Generic Programming Java Fall 2009 Instructor: Dr. Masoud Yaghini Generic Programming Outline Polymorphism and Generic Programming Casting Objects and the instanceof Operator The protected Data and

More information

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++ Crash Course in Java Netprog: Java Intro 1 Why Java? Network Programming in Java is very different than in C/C++ much more language support error handling no pointers! (garbage collection) Threads are

More information

S.E. Sem. III [CMPN] Object Oriented Programming Methodology

S.E. Sem. III [CMPN] Object Oriented Programming Methodology S.E. Sem. III [CMPN] Object Oriented Programming Methodology Time : 3 Hrs.] Prelim Question Paper Solution [Marks : 80 Q.1(a) Write a program to calculate GCD of two numbers in java. [5] (A) import java.util.*;

More information

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Language Features. 1. The primitive types int, double, and boolean are part of the AP Language Features 1. The primitive types int, double, and boolean are part of the AP short, long, byte, char, and float are not in the subset. In particular, students need not be aware that strings are

More information

Crash Course Review Only. Please use online Jasmit Singh 2

Crash Course Review Only. Please use online Jasmit Singh 2 @ Jasmit Singh 1 Crash Course Review Only Please use online resources @ Jasmit Singh 2 Java is an object- oriented language Structured around objects and methods A method is an action or something you

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html

More information

Assumptions. History

Assumptions. History Assumptions A Brief Introduction to Java for C++ Programmers: Part 1 ENGI 5895: Software Design Faculty of Engineering & Applied Science Memorial University of Newfoundland You already know C++ You understand

More information

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology Java vs C++ 1 1 Department of Computer Science Poznan University of Technology 2012.10.07 / OOP Laboratory Outline 1 2 3 Outline 1 2 3 Outline 1 2 3 Tabular comparizon C++ Java Paradigm Procedural/Object-oriented

More information

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism IST311 Advanced Issues in OOP: Inheritance and Polymorphism IST311/602 Cleveland State University Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition

More information

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Tools : The Java Compiler. The Java Interpreter. The Java Debugger Tools : The Java Compiler javac [ options ] filename.java... -depend: Causes recompilation of class files on which the source files given as command line arguments recursively depend. -O: Optimizes code,

More information

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA 1. JIT meaning a. java in time b. just in time c. join in time d. none of above CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA 2. After the compilation of the java source code, which file is created

More information

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University Lecture 2 COMP1406/1006 (the Java course) Fall 2013 M. Jason Hinek Carleton University today s agenda a quick look back (last Thursday) assignment 0 is posted and is due this Friday at 2pm Java compiling

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

The Sun s Java Certification and its Possible Role in the Joint Teaching Material The Sun s Java Certification and its Possible Role in the Joint Teaching Material Nataša Ibrajter Faculty of Science Department of Mathematics and Informatics Novi Sad 1 Contents Kinds of Sun Certified

More information

Selected Java Topics

Selected Java Topics Selected Java Topics Introduction Basic Types, Objects and Pointers Modifiers Abstract Classes and Interfaces Exceptions and Runtime Exceptions Static Variables and Static Methods Type Safe Constants Swings

More information

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2) Summary: Chapters 1 to 10 Sharma Chakravarthy Information Technology Laboratory (IT Lab) Computer Science and Engineering Department The University of Texas at Arlington, Arlington, TX 76019 Email: sharma@cse.uta.edu

More information

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS PAUL L. BAILEY Abstract. This documents amalgamates various descriptions found on the internet, mostly from Oracle or Wikipedia. Very little of this

More information

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. CMSC 131: Chapter 28 Final Review: What you learned this semester The Big Picture Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. Java

More information

Index COPYRIGHTED MATERIAL

Index COPYRIGHTED MATERIAL Index COPYRIGHTED MATERIAL Note to the Reader: Throughout this index boldfaced page numbers indicate primary discussions of a topic. Italicized page numbers indicate illustrations. A abstract classes

More information

Midterm assessment - MAKEUP Fall 2010

Midterm assessment - MAKEUP Fall 2010 M257 MTA Faculty of Computer Studies Information Technology and Computing Date: /1/2011 Duration: 60 minutes 1-Version 1 M 257: Putting Java to Work Midterm assessment - MAKEUP Fall 2010 Student Name:

More information

OBJECT ORIENTED PROGRAMMING TYm. Allotted : 3 Hours Full Marks: 70

OBJECT ORIENTED PROGRAMMING TYm. Allotted : 3 Hours Full Marks: 70 I,.. CI/. T.cH/C8E/ODD SEM/SEM-5/CS-504D/2016-17... AiIIIII "-AmI u...iir e~ IlAULAKA ABUL KALAM AZAD UNIVERSITY TECHNOLOGY,~TBENGAL Paper Code: CS-504D OF OBJECT ORIENTED PROGRAMMING TYm. Allotted : 3

More information

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content Core Java - SCJP Course content NOTE: For exam objectives refer to the SCJP 1.6 objectives. 1. Declarations and Access Control Java Refresher Identifiers & JavaBeans Legal Identifiers. Sun's Java Code

More information

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors Outline Overview history and advantage how to: program, compile and execute 8 data types 3 types of errors Control statements Selection and repetition statements Classes and methods methods... 2 Oak A

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes. a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must

More information

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created. + Inheritance + Inheritance Classes that we design in Java can be used to model some concept in our program. For example: Pokemon a = new Pokemon(); Pokemon b = new Pokemon() Sometimes we need to create

More information

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline Java Intro 3 9/7/2007 1 Java Intro 3 Outline Java API Packages Access Rules, Class Visibility Strings as Objects Wrapper classes Static Attributes & Methods Hello World details 9/7/2007 2 Class Libraries

More information

We are on the GUI fast track path

We are on the GUI fast track path We are on the GUI fast track path Chapter 13: Exception Handling Skip for now Chapter 14: Abstract Classes and Interfaces Sections 1 9: ActionListener interface Chapter 15: Graphics Skip for now Chapter

More information

The Java programming environment. The Java programming environment. Java: A tiny intro. Java features

The Java programming environment. The Java programming environment. Java: A tiny intro. Java features The Java programming environment Cleaned up version of C++: no header files, macros, pointers and references, unions, structures, operator overloading, virtual base classes, templates, etc. Object-orientation:

More information

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini 22. Inheritance Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Inheritance Object-oriented programming

More information

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini 24. Inheritance Java Fall 2009 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Superclasses and Subclasses Inheritance

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

Full file at Chapter 2 - Inheritance and Exception Handling

Full file at   Chapter 2 - Inheritance and Exception Handling Chapter 2 - Inheritance and Exception Handling TRUE/FALSE 1. The superclass inherits all its properties from the subclass. ANS: F PTS: 1 REF: 76 2. Private members of a superclass can be accessed by a

More information

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017 Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 11: Inheritance and Polymorphism Part 1 Instructor: AbuKhleif, Mohammad Noor Sep 2017 Instructor AbuKhleif, Mohammad

More information

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Atelier Java - J1. Marwan Burelle.  EPITA Première Année Cycle Ingénieur. marwan.burelle@lse.epita.fr http://wiki-prog.kh405.net Plan 1 2 Plan 3 4 Plan 1 2 3 4 A Bit of History JAVA was created in 1991 by James Gosling of SUN. The first public implementation (v1.0) in 1995.

More information

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix PGJC4_JSE8_OCA.book Page ix Monday, June 20, 2016 2:31 PM Contents Figures Tables Examples Foreword Preface xix xxi xxiii xxvii xxix 1 Basics of Java Programming 1 1.1 Introduction 2 1.2 Classes 2 Declaring

More information

The Essence of OOP using Java, Nested Top-Level Classes. Preface

The Essence of OOP using Java, Nested Top-Level Classes. Preface The Essence of OOP using Java, Nested Top-Level Classes Baldwin explains nested top-level classes, and illustrates a very useful polymorphic structure where nested classes extend the enclosing class and

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More information

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing Java Programming MSc Induction Tutorials 2011 Stefan Stafrace PhD Student Department of Computing s.stafrace@surrey.ac.uk 1 Tutorial Objectives This is an example based tutorial for students who want to

More information

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B 1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these 2. How many primitive data types are there in Java? A. 5 B. 6 C. 7 D. 8 3. In Java byte, short, int and long

More information

Course Outline. Introduction to java

Course Outline. Introduction to java Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

Java for Interfaces and Networks (DT3010, HT11)

Java for Interfaces and Networks (DT3010, HT11) Java for Interfaces and Networks (DT3010, HT11) Networking with Threads and Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces

More information

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy CSCI 334: Principles of Programming Languages Lecture 18: C/C++ Homework help session will be tomorrow from 7-9pm in Schow 030A instead of on Thursday. Instructor: Dan Barowy HW6 and HW7 solutions We only

More information

Chapter 1 Introduction to Java

Chapter 1 Introduction to Java What is Java? Chapter 1 Introduction to Java Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows,

More information

Fall 2017 CISC124 9/16/2017

Fall 2017 CISC124 9/16/2017 CISC124 Labs start this week in JEFF 155: Meet your TA. Check out the course web site, if you have not already done so. Watch lecture videos if you need to review anything we have already done. Problems

More information

Kickstart Intro to Java Part I

Kickstart Intro to Java Part I Kickstart Intro to Java Part I COMP346/5461 - Operating Systems Revision 1.6 February 9, 2004 1 Topics Me, Myself, and I Why Java 1.2.*? Setting Up the Environment Buzz about Java Java vs. C++ Basic Java

More information

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32 CMPT 115 C tutorial for students who took 111 in Java Mark G. Eramian Ian McQuillan University of Saskatchewan Mark G. Eramian, Ian McQuillan CMPT 115 1/32 Part I Starting out Mark G. Eramian, Ian McQuillan

More information

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple

More information

Programming. Syntax and Semantics

Programming. Syntax and Semantics Programming For the next ten weeks you will learn basic programming principles There is much more to programming than knowing a programming language When programming you need to use a tool, in this case

More information

F I N A L E X A M I N A T I O N

F I N A L E X A M I N A T I O N Faculty Of Computer Studies M257 Putting Java to Work F I N A L E X A M I N A T I O N Number of Exam Pages: (including this cover sheet( Spring 2011 April 4, 2011 ( 5 ) Time Allowed: ( 1.5 ) Hours Student

More information

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748 COT 3530: Data Structures Giri Narasimhan ECS 389; Phone: x3748 giri@cs.fiu.edu www.cs.fiu.edu/~giri/teach/3530spring04.html Evaluation Midterm & Final Exams Programming Assignments Class Participation

More information

Compaq Interview Questions And Answers

Compaq Interview Questions And Answers Part A: Q1. What are the difference between java and C++? Java adopts byte code whereas C++ does not C++ supports destructor whereas java does not support. Multiple inheritance possible in C++ but not

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

CSC Java Programming, Fall Java Data Types and Control Constructs

CSC Java Programming, Fall Java Data Types and Control Constructs CSC 243 - Java Programming, Fall 2016 Java Data Types and Control Constructs Java Types In general, a type is collection of possible values Main categories of Java types: Primitive/built-in Object/Reference

More information

PROGRAMMING FUNDAMENTALS

PROGRAMMING FUNDAMENTALS PROGRAMMING FUNDAMENTALS Q1. Name any two Object Oriented Programming languages? Q2. Why is java called a platform independent language? Q3. Elaborate the java Compilation process. Q4. Why do we write

More information

Programming Language Concepts: Lecture 2

Programming Language Concepts: Lecture 2 Programming Language Concepts: Lecture 2 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2011 PLC 2011, Lecture 2, 6 January 2011 Classes and

More information

Getting started with Java

Getting started with Java Getting started with Java by Vlad Costel Ungureanu for Learn Stuff Programming Languages A programming language is a formal constructed language designed to communicate instructions to a machine, particularly

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

CS 11 java track: lecture 3

CS 11 java track: lecture 3 CS 11 java track: lecture 3 This week: documentation (javadoc) exception handling more on object-oriented programming (OOP) inheritance and polymorphism abstract classes and interfaces graphical user interfaces

More information

CMSC 341. Nilanjan Banerjee

CMSC 341. Nilanjan Banerjee CMSC 341 Nilanjan Banerjee http://www.csee.umbc.edu/~nilanb/teaching/341/ Announcements Just when you thought Shawn was going to teach this course! On a serious note: register on Piazza I like my classes

More information

Answer1. Features of Java

Answer1. Features of Java Govt Engineering College Ajmer, Rajasthan Mid Term I (2017-18) Subject: PJ Class: 6 th Sem(IT) M.M:10 Time: 1 hr Q1) Explain the features of java and how java is different from C++. [2] Q2) Explain operators

More information