Download presentation
Presentation is loading. Please wait.
1
Object-Oriented Software Engineering
Object-Orientation and Java
2
Object-Orientation & Java
Contents Getting Started A Little Bit of Syntax Differences between C and Java
3
Getting Started Goto: Download the Java Software Development Kit (SDK)
Download the Java Software Development Kit (SDK) Its free! Download the Documentation Also free! ( Buy JAVA In a Nutshell, by David Flanagan, Publ. O’Rielly It’ll cost you, sorry!
4
File extensions in Java
Source .class javac (compiler) Byte code JVM Java Virtual Machine Any Hardware (that supports the JVM)
5
What you get in the JDK appletviewer For running Applets javac
Compiles .java .class java Interprets a Java Class classes.zip The system provided classes src.zip Complete source for standard classes javadoc Generates Java HTML documents …
6
JDK Swing Basics Java JDK provides a collection of Foundation classes to allow easy implementation of GUI-s (Graphical User Interface) For this lecture borrowed heavily from: Top-Level Containers Applet Dialog Frame
7
JDK Swing Basics General-Purpose Containers Panel Scroll pane
Split pane Tabbed pane Tool bar
8
JDK Swing Basics Special-Purpose Containers Internal frame
Layered pane Root pane
9
Text field or Formatted text field
JDK Swing Basics Basic Controls Buttons Combo box List Menu Slider Spinner Text field or Formatted text field
10
JDK Swing Basics Uneditable Information Displays Label Progress bar
Tool tip
11
JDK Swing Basics Interactive Displays of Highly Formatted Information
File chooser Color chooser Text Tree Table
12
Object-Orientation & Java
Contents Getting Started Some Syntax Differences between C and Java
13
{ Defining a Class Account public class Account { public int number;
public double balance; public void credit(double x) { // do some sums } public void debit(double y) { // do checking then sums { members number balance fields credit debit methods
14
“Circle” Example public class Circle { } Circle radius circumference
area public static final double PI = ; public double radius; public double circumference() { return 2 * PI * radius; } public double area() { return PI * radius * radius;
15
The “Circle” class public class Circle { // A class field
public static final double PI= ; // A useful constant // A class method: just compute a value based on the arguments public static double radiansToDegrees(double rads) { return rads * 180 / PI; } // An instance field public double r; // The radius of the circle // Two instance methods: they operate on the instance fields // of an object public double area() { // Compute the area of the circle return PI * r * r; public double circumference() { // Compute the circumference return 2 * PI * r;
16
The “main” method public class Circle {
public double r; // The radius of the circle public double area() { // Compute the area of the circle return PI * r * r; } public double circumference() { // Compute the circumference return 2 * PI * r; public static void main(String[] args) { int input = Integer.parseInt(args[0]); Circle c = new Circle(); c.r = input; double result = c.circumference(); System.out.println(result); }
17
int input = Integer.parseInt(args[0]);
The “main” method int input = Integer.parseInt(args[0]); Integer Class Static Method Call to Integer Class parameters to method call
18
Put “main” in a new class?
public class MakeCircle { public static void main(String[] args) { int input = Integer.parseInt(args[0]); Circle c = new Circle(); c.r = input; double circum = c.circumference(); System.out.println(circum); double a = c.area(); System.out.println(a); }
19
An Association Circle radius circumference area MakeCircle main
creates instances of
20
Make Circle Execution java MakeCircle 34 Java Interpreter
Circle c = new Circle(); :MakeCircle <<creates>> main([“34”]) <<creates>> c :Circle c.r = input; r c.circumference(); circumference() area() c.area();
21
File extensions in Java
Source .class javac (compiler) Byte code JVM Java Virtual Machine Any Hardware (that supports the JVM)
22
The Circle example C:\Java contains C:\>cd Java Circle.java and
MakeCircle.java C:\>cd Java C:\Java>javac Circle.java C:\Java>javac MakeCircle.java C:\Java now also contains Circle.class and MakeCircle.class C:\Java>java MakeCircle 4 C:\Java>java MakeCircle 5
23
Object-Orientation & Java
Contents Getting Started A Little Bit of Syntax Differences between C and Java
24
Differences No Preprocessor No Global Variables
No analogues of #define, #include, #ifdef Constants are replaced by static final fields No Global Variables Avoids possibility of namespace collisions We will see later how you can make a constant or variable globally accessible
25
Java vs. C Well-defined primitive type sizes No pointers
Removes this as a platform dependency No pointers Although Java Classes and Arrays are reference types, these references are “opaque”. No “address of” or “dereference” operators This is not a handicap and eliminates an important source of bugs
26
Java vs. C Garbage Collection No goto statement
Objects are “tidied away” as soon as there are no further references to them So, no need to explicitly manage memory Eliminates memory leaks No goto statement Adds exception handling and labelled break and continue statements
27
Java vs. C Variable declarations anywhere Forward references
Java allows local variable definitions to be made anywhere in a method or block Good practice to group them, though Forward references Methods can be invoked before they are defined (we’ll see why it is important to be able to do this)
28
Java vs. C Method overloading No struct and union types
Multiple methods can be defined with the same name, so long as they have different parameter lists No struct and union types No enumerated types No bitfields No typedef No method pointers No variable-length argument lists
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.