0% found this document useful (0 votes)
23 views

Presentation For Object Oriented Programming

The document discusses object-oriented principles of polymorphism including method overloading and overriding. It also covers using an integrated development environment like Eclipse to write, compile, and run Java programs.

Uploaded by

susuhaha0101
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Presentation For Object Oriented Programming

The document discusses object-oriented principles of polymorphism including method overloading and overriding. It also covers using an integrated development environment like Eclipse to write, compile, and run Java programs.

Uploaded by

susuhaha0101
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

1.

3 The Java Programming Language


Additional slides
Object - Oriented (OO) Principles – POLYMORPHISM (Overriding vs Overloading)

*****
Signature consists of 2 parts of
method
• NAME
• ARGUMENT

1. Overloading is about the same method having


different signatures.
2. Overriding is about the same method, and
same signature but different classes
connected through inheritance.
3. Overloading is an example of compiler-time
polymorphism and overriding is an example
of run-time polymorphism.
4. Polymorphism means that same operation
exists in many classes.
5. Each operation has the same meaning but
1 ***** carries out the operation in its own unique
Each polymorphic Method Must Have the Same Signature way (different way)
19.10.2023 23:15
1.4 Programming Environment
Programming Environment

❑ There are many free programming tools available for Java


▪ We will use Eclipse as Integrated Development Environment
(IDE)
• Download and set up Eclipse IDE 2023-09 from Eclipse site.
– https://www.eclipse.org/?gclid=EAIaIQobChMIuNSqo9-
9gQMVCORRCh3HeAKSEAAYASAAEgL9MvD_BwE
❑ Components of an IDE:
▪ Source code editor helps programming by: NOTICE USEFUL LINKS :
For downloading and setting up tools and
running java samples
• Listing line numbers of code • JDK
• Eclipse
• Color lines of code (comments, text…) • Java Tutorials
W3SCHOOLS internet page is highly
• Auto-indent source code recommended
https://www.w3schools.com/ ORACLE
▪ Output window TUTORIALS java/java_getstarted.asp
https://docs.oracle.com/javase/tutorial/
2 ▪ Debugger JAVAtPOINT TUTORIALS:
https://www.javatpoint.com/java-tutorial
STACKOVERFLOW:
19.10.2023 23:15 https://stackoverflow.com/
1.4 Programming Environment
An Example IDE

⚫ Many IDEs are designed specifically for Java programming, but we chose
3
ECLIPSE IDE and will use for labs.
19.10.2023 23:15
1.4 Programming Environment
Your First Program

❑ Traditional ‘Hello World’ program in Java

❑ We will examine this program in the next section


▪ Typing it into your IDE would be good practice!
▪ Be careful of spelling
▪ JaVa iS CaSe SeNsItiVe
4
19.10.2023 23:15
▪ Java uses special characters, e.g. { } ( ) ;
1.4 Programming Environment
Text Editor Programming

❑ You can also use a simple text editor such


as Notepad to write your source code
❑ Once saved as HelloPrinter.java, you
can use a console window to:
1) Compile the program
2) Run the program

Compile

Output Execute

5
19.10.2023 23:15
1.4 Programming Environment
Source Code to Running Program

❑ The compiler generates the .class file which


contains instructions for the Java Virtual machine
❑ Class files contain ‘byte code’ that you cannot edit
▪ D:\temp\hello>Type HelloPrinter.class
▪ ╩■║╛ 2 ↔ ♠ ☼ ► ↕ ‼ ¶ § ▬☺ ♠<init>☺ ♥()V☺
♦Code☺ ☼LineNumberTable☺ ♦main▬([Ljava/lang/String;)V☺
▪ Hello, World! elloPrinter.java♀ ↨♀ ↑ ↓☺
6
19.10.2023 23:15
1.4 Programming Environment
Organize your work

❑ Your ‘source code’ is stored


in .java files
❑ Create one folder per program
▪ Can be many .java files
❑ Be sure you know where your
IDE stores your files!
❑ Backup your work!

7
19.10.2023 23:15
1.5 Analyzing your First Program
First Program

1: Declares a ‘class’ HelloPrinter


-- Every Java program has one or more classes.
3: Declares a method called ‘main’
-- Every Java application has exactly one ‘ main’ method
-- Entry point where the program starts
8 5: Method System.out.println outputs ‘Hello, World!’
19.10.2023 23:15 -- A statement must end with a semicolon (;)
1.5 Analyzing your First Program
Syntax 1.1: The Java Program

❑ Every application has the same basic layout


▪ Add your ‘code’ inside the main method

9
19.10.2023 23:15
1.5 Analyzing your First Program
Calling Java Library methods

❑ Line 5 shows how to ‘call’ a ‘method’


from the Java API: System.out.println
▪ Code that somebody else wrote for you!
▪ Notice the dots (periods)
▪ Parenthesis surround the arguments that you
‘pass’ to a method
▪ We are passing a String “Hello World”
• Note the double quotes which denote a String inside
▪ You can also print numerical values
10
19.10.2023 23:15
• System.out.println(3 + 4);
1.5 Analyzing your First Program
Getting to know println

❑ The println method prints a string or a


number and then starts a new line.
System.out.println("Hello"); Hello
System.out.println("World!”); World!

System.out.print("00"); 007
System.out.println(3+4);
A method is called by specifying
11 the method and its agruments
19.10.2023 23:15
1.6 Errors
Common Error 1.1

❑ Omitting Semicolons
▪ In Java, every statement must end in a semicolon.
Forgetting to type a semicolon is a common error. It
confuses the compiler, because the compiler uses the
semicolon to find where one statement ends and the
next one starts. For example, the compiler sees this:
System.out.println("Hello")
System.out.println("World!");

▪ As this:
System.out.println("Hello") System.out.println("World!");
▪ It doesn’t understand this statement, because it does
12 not expect the word System following the closing
19.10.2023 23:15 parenthesis after Hello.
1.6 Errors
Errors

❑ The Two Categories of Errors:


1) Compile-time Errors
• Syntax Errors
– Spelling, Capitalization, punctuation
– Ordering of statements, matching of braces/parenthesis…
• Correct first error listed, then compile again
2) Run-time Errors
• Logic Errors
• Program runs, but produces unintended results
• Program may ‘crash’
13
19.10.2023 23:15
1.6 Errors
Syntax Errors

❑ What happens if you


▪ Misspell a word: System.ou.println
▪ Don’t Capitalize a word system.out.println
▪ Leave out a word void
▪ Forget a Semicolon after ("Hello, World!")
▪ Don’t match a curly brace? Remove line 6
14
19.10.2023 23:15
❑ Try it to see what error messages are generated
1.6 Errors
Logic Errors

❑ What happens if you


▪ Divide by Zero System.out.println(1/0);
▪ Mis-spell output ("Hello, Word!")
▪ Forget to output Remove line 5
❑ Programs will compile and run
15 ▪ The output may not be as expected
19.10.2023 23:15
1.7 Problem Solving: Algorithm Design

❑ Algorithms are simply plans


▪ Detailed plans that describe the steps to solve
a specific problem
❑ You already know quite a few
▪ Calculate the area of a circle
▪ Find the length of the hypotenuse of a triangle
❑ Some problems are more complex and
require more steps
▪ Calculate PI to 100 decimal places
16
▪ Calculate the trajectory of a missile
19.10.2023 23:15
1.7 Problem Solving: Algorithm Design
Text Problem to Algorithm

❑ Given the problem:


▪ You put $10,000 into a bank account that earns 5 percent
interest per year. How many years does it take for the
account balance to be double the original?
❑ How would you solve it?
▪ Manual method
• Make a table
• Add lines until done
▪ Use a spreadsheet!
• Write a formula
17 – Per line, based on line above
19.10.2023 23:15
1.7 Problem Solving: Algorithm Design
Text Problem to Algorithm Steps

▪ You put $10,000 into a bank account that earns 5 percent


interest per year. How many years does it take for the
account balance to be double the original?
❑ Break it into steps
Start with a year value of 0 and a balance of $10,000
Repeat the following while the balance is less than
$20,000
Add 1 to the year value
Multiply the balance by 1.05
(5% increase)

18
19.10.2023 23:15 Report the final year value as the answer
1.7 Problem Solving: Algorithm Design
Text Problem to Pseudocode

❑ Pseudocode
▪ Half-way between natural language and a
programming language
Pseudocode is an informal
❑ Modified Steps description of a sequence of
▪ Set the year value of 0
steps for solving a problem

▪ Set the balance to $10,000


▪ While the balance is less than $20,000
• Add 1 to the year value
• Multiply the balance by 1.05
▪ Report the final year value as the answer
19 ❑ This can be translated into Java fairly easily
19.10.2023 23:15
1.7 Problem Solving: Algorithm Design
Algorithm Defined

❑ An algorithm describes a
sequence of steps that is:
▪ Unambiguous
• Do not require ‘assumptions’
• Uses precise instructions
▪ Executable
• Can be carried out in practice
▪ Terminating
• Will eventually come to an end

20
19.10.2023 23:15
1.7 Problem Solving: Algorithm Design
Steps: Algorithm to Pseudocode

You have the choice of buying two cars. One is more fuel efficient than the
other, but also more expensive. You know the price and fuel efficiency (in miles
per gallon, mpg) of both cars. You plan to keep the car for ten years. Which car
is the better deal?

1. Determine the inputs and outputs


From the problem statement:
price1, price2, mpg1, mpg2…
2. Break down the problem into smaller tasks
‘Calculate total cost’ for each car
3. Describe each subtask as pseudocode
21 total cost = purchase price + operating cost
19.10.2023 23:16 4. Test your pseudocode with example input
Summary: Computer Basics

❑ Computers execute very basic instructions in


rapid succession.
❑ A computer program is a sequence of instructions
and decisions.
❑ Programming is the act of designing and
implementing computer programs.
❑ The central processing unit (CPU) performs
program control and data processing.
❑ Storage devices include memory and secondary
22 storage.
19.10.2023 23:16
Summary: Java

❑ Java was originally designed for programming


consumer devices, but it was first successfully
used to write Internet applets.
❑ Java was designed to be safe and portable,
benefiting both Internet users and students.
❑ Java programs are distributed as instructions for a
virtual machine, making them platform-
independent.
❑ Java has a very large set of libraries. Focus on
learning those parts of libraries that you need for
23 your programming projects.
19.10.2023 23:16
Summary: Java

❑ Set aside some time to become familiar with the


programming environment that you will use for
your class work.
❑ An editor is a program for entering and modifying
text, such as a Java program.
❑ Java is case sensitive. You must be careful about
distinguishing between upper and lowercase
letters.
❑ The Java compiler translates source code into
class files that contain instructions for the Java
24 virtual machine.
19.10.2023 23:16
Summary: Java

❑ Classes are the fundamental building blocks of


Java programs.
❑ Every Java application contains a class with a
main method. When the application starts, the
instructions in the main method are executed.
❑ Each class contains declarations of methods.
Each method contains a sequence of instructions.
❑ A method is called by specifying the method and
its parameters.
❑ A string is a sequence of characters enclosed in
25 quotation marks.
19.10.2023 23:16
Summary: Errors and Pseudocode

❑ A compile-time error is a violation of the


programming language rules that is detected by
the compiler.
❑ A run-time error causes a program to take an
action that the programmer did not intend.
❑ Pseudocode is an informal description of a
sequence of steps for solving a problem.
❑ An algorithm for solving a problem is a sequence
of steps that is unambiguous, executable, and
terminating.
26
19.10.2023 23:16

You might also like