Computer Science Notes
Computer Science Notes
Lecturer Charles Thevathayan ([email protected]) Office location: 14.10.12 Head Tutor Rodney Cocker Contact him for any Admin matters (coursework, absence, etc)
Course Content Developed by: Charles Thevathayan, Caspar Ryan, Craig Hamilton and Peter Tilmanis
COSC1284 Overview
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. Introduction, programming languages, SE lifecycle Data Types, Decisions, Loops, Arrays Writing simple classes Inheritance Polymorphism Abstract classes and Interfaces Introduction to GUI Introduction to Event Driven Programming Exception handling and Files Generic Classes Revision
Programming Techniques
One of the most important subject in CS as it is prerequisite for many others. Practical subject - requires at least 8 -10 hours of programming practice per week. Assessment 1. 40% Exam 2. 60% Coursework Programming Assignments 40% Mid-semester Test 20% Note: You need to pass both components ( 1 and 2).
Resources
Prescribed: Programming Techniques Student Notes (2014), Department of Computer Science, RMIT Daniel Liang, Introduction to Java Programming, Ninth Edition, Prentice Hall (ISBN 978-0-13-293652-1 Recommended Reference: Absolute Java, by Walter Savitch Java Concepts, by Cay HorstMann There are numerous texts in Java On-line Materials WebLearn Quiz Copy of these Slides, detailed notes
Introduction to the course Origin of Java Types of Computer Languages Compiling and Interpreting Components of a Java Program Classes and Objects Using the Java Docs to view the Class details Creating and Manipulating objects Packages in Java Algorithms Programming Style Programming Errors
Week 1
Origins of Java
Originated as part of a Research Project Embedding networking capabilities and Cross platform portability
10
What are the programming skills currently most sought after by employers? __________
11
Machine language
Languages that a computer can directly understand. Differs from Machine to Machine. Example: 0010 0000 0011 0111 1100 0100 1100 0100
Memory location
12
Assembly language
One level higher than machine languages
Specified in terms of basic operations and symbolic names.
13
Each compiler produces a translation for a specific computer. Same source program can be run in another computer if it is compiled by the appropriate compiler.
14
15
Traditional Steps:
Compiling, Linking and Executing
Standard way of producing and creating an application 1.Create the program. Int add(int x, int y)
{ int z = ...
2. Traditional compilers translate source code into machine language directly. Int add(int x, int y) 01100 1001010
{ int z = ... 00010 1110101
Executable file
16
Compiler (javac)
bytecode
The Java interpreter reads the bytecodes (loads the necessary library bytecodes) and executes it on a specific machine.
bytecodes
Interpreter (java)
Hello World
Unlike machine code Java bytecode is not tied to any particular machine making it architecture neutral.
17
Java compiler
Java bytecode
Java interpreter
Bytecode compiler
Machine code
Give one benefit and one drawback of Java over other high level languages such as C or Pascal. _____________________________________________
18
19
Console application
20
21
22
/* My first Java program prints the famous Hello World! The tradition says all great programmers must do this*/ public class HelloWorld { static int year; // current year public static void main(String[] args){ .. } }
23
24
public class HelloWorld { int year; public static void main(String[] args) { .. } }
25
What will be the output when we compile and interpret the code below ?
public class HelloThere { public static void anotherMethod( ) { System.out.print("There "); } public static void main(String[] args) { System.out.print("Hello "); } }
(A) (B) (C) (D) Hello There Hello There There Hello
26
What will be the output now ? (note the additional line in main() )
public class HelloThere { public static void anotherMethod( ) { System.out.print("There "); } public static void main(String[] args) { System.out.print("Hello "); anotherMethod(); } }
(A) (B) (C) (D) Hello There Hello There There Hello
Terminating statements
Each statement must ends with a semicolon ';
System.out.println("Hello");
System.out.println("World");
Hello World
The effect is still the same with two statements in the same line as in: (but considered poor style !)
System.out.println(); System.out.println();
28
12 Sydney rd
67 Yale ct
82 Betula av
String
String Hello
String World
String Universe
29
classes
30
This method causes the details of the Rectangle object to be printed in the terminal.
java.awt.Rectangle[x=10,y=5,width=20,height=30]
31
Rectangle Object
Rectangle
x=10, y=5, width=20, height=30
32
rect1
Rectangle Object
(initial state)
x=10, y=5, width=20, height=30
Rectangle
rect1
Rectangle
width=20,
height=30
33
Time to pause.
1. Which of the following is true? I. A class can have many objects (or instances) II. An object (or instance) can have many classes 2. How do we create an object (or instance) of a specific class? ______________________________________
35
Another Quiz
What will be the output of the program below?
public class RefTest { public static void main(String args[]) { String s; // a String reference s = new String("Apple"); s = new String("Orange"); s = new String("Banana"); System.out.println("s is now referring to " + s); } }
____________________________________________________ Conclusion: At any one time, a reference can refer to ________ (single/multiple) object(s).
36
Ans:
Rectangle r1 = new Rectangle(10,10,5,10); r1.translate(20,30); String s1 = new String(" Monday "); s1.translate(20,30); // // // // A B C D
Ans:
37
String Hello
object
aMessage
reference
As String objects are commonly used, they need not be created explicitly using the operator new. Hence the 3rd line can be replaced with: String aMessage = Hello";
38
System.out
40
Using packages
In the last program we specified that we are using the Rectangle class of the java.awt package. import java.awt.Rectangle; All classes in standard library are placed in packages: java.awt, java.io, To import all classes use: import java.awt.*; Package java.lang is automatically imported - contains String,System, ... You can place your own classes in packages too - to avoid name clash.
Frame java.awt package Rectangle
Menu
myown.graphics package
Ellipse Rectangle
Parabola
Square
41
Analyze
Refine the requirements.
Design
How to do it ?
Use Algorithms, classes, Object Interactions
Write a program to find the roots (real) of a quadratic equation of the form: ax2 + bx + c = 0.
Understand specification
Implement
Code the programs
Test
Run program with different inputs and verify results
Document
Make it maintainable
Document the program
Analyse
Design
42
2a
disc
Refine Step 2
2a
43
44
Programming style
Java does not impose a particular style but there are rules most Java programmers follow. One statement per line
System.out.println(Welcome to RMIT); System.out.println(Welcome to CS);
Comments help clarify the code Use blank lines to separate logical sections Use meaningful identifiers but not verbose
45
46
47
Choosing Identifiers
Identifiers should be meaningful, but not verbose. Any combination of letters, digits, dollar signs '$' underscore characters '_', not beginning with a digit, is legal. Legal: Total, lastWord, TaxCalculation Illegal: 3rdAmmendment, you too, you#too Avoid: s1, theFirstOfTheStudentsInTheClass Acceptable : student1, stud_1, firstStudent
49
// missing semicolon
50
51
instead of
r1 = (-b + Math.sqrt(disc)) / (2*a);
52
Exercise
A. The program below written to find average of two weights has 3 syntax (compilation) errors and 1 logical error. Identify and correct them.
public class Add { public static void main(String args[]) { double weight1 = 85.5 double weight2 = 92.3
B. What changes are needed to find average of 3 weights 85.5,92.3 and 78.4 ?____________________________
C. What changes are needed to find average of other weights without having to change the program ? _____________
53
54
55
56