Download Introduction to object oriented programming in JAVA and more Study notes Object Oriented Programming in PDF only on Docsity!
Introduction to object
oriented programming in
JAVA
Computer Applications in Power Systems – Advance course EH Dr. Arshad Saleem Lecture material contribution from: John Lewis and William Loftus. Java Software Solutions, foundation of program design
contents
- Installation and setup
- OOP architecture revisit
- Programming in JAVA
- Working together
- Hello World – the first program
- Declaring variables and assignments
- Implementing basic structures
- Conditions and continuations
- Implementing inheritance in JAVA
- Working individually/ in groups
- Implementing a class diagram
- Implementing class relationships
- Implementing methods
- Implementing use cases
- HomeWork
Java Program Structure
- A program is made up of one or more classes
- A class contains one or more methods
- A method contains program statements
- A Java application always executes the main method
HelloWorld.java
//HelloWorld, my first program in Java
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World...")
try{System.in.read();}
catch(Exception e){}
}// method main
}// class HelloWorld
Importing Packages
- Using a class from the Java API can be accomplished by using its fully qualified name: java.lang.System.out.println ();
- Or, the package can be imported using an import statement , which has two forms: import java.applet.*; import java.util.Random;
- The java.lang package is automatically imported into every Java program
Java Applets
- A Java applet is a Java program that is intended to be sent across a network and executed using a Web browser
- A Java application is a stand alone program
- Applications have a main method, but applets do not
- Applets are derived from the java.applet.Applet class
- See Confucius.java and No_Parking.java
- Links to applets can be embedded in HTML documents
Variables
- Multiple variables can be declared on the same line: int total, count, sum;
- Variables can be initialized (given an initial value) in the declaration: int total = 0, count = 20; float unit_price = 57.25;
- See Piano_Keys.java
Assignment Statements
- An assignment statement takes the following form: variable-name = expression ;
- The expression is evaluated and the result is stored in the variable, overwriting the value currently stored in the variable
- See United_States.java
- The expression can be a single value or a more complicated calculation
Constants
- When appropriate, constants are better than variables because: - they prevent inadvertent errors because their value cannot change
- They are better than literal values because:
- they make code more readable by giving meaning to a value
- they facilitate change because the value is only specified in one place
BasicMath.java
Circumference is: 37. Area is: 113. class BasicMath { public static void main (String[] args) { final double PI = 3.14159; int radius = 6; double area, circum; circum = PI * radius * 2; area = PI * radius * radius; System.out.println("Circumference is: " + circum); System.out.println("Area is: " + area); try{System.in.read();} catch(Exception e){} }//method main }//class BasicMath
Input and Output
- The Java API allows you to create many kinds of streams to perform various kinds of I/O
- To read character strings, we will convert the System.in stream to another kind of stream using: BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in), 1);
- This declaration creates a new stream called stdin
Echo.java
import java.io.*; class Echo { public static void main (String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in),1); String message; System.out.println ("Enter a line of text: "); message=stdin.readLine(); System.out.println ("You entered: "" + message + """); } }
The if Statement
condition false statement true
The if-else Statement
condition statement true false statement