0% found this document useful (0 votes)
5 views31 pages

Java Object-Oriented Programming Guide

This document provides an introduction to object-oriented programming logic in Java. It explains what Java is, how to program using object orientation, and how to create and run simple Java programs using the NetBeans IDE.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views31 pages

Java Object-Oriented Programming Guide

This document provides an introduction to object-oriented programming logic in Java. It explains what Java is, how to program using object orientation, and how to create and run simple Java programs using the NetBeans IDE.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Logic in Java

Object-Oriented
Booklet

Material developed by
COTI Informatics

Av. Rio Branco, 185 - Sala 901 - Rio de Janeiro / RJ


Tel. 21-2262-9043 /contato@[Link]
Java Object-Oriented Logic Handbook

Who We Are
The Coti Informática school is now a factory for inserting
students in the job market.

Coordinated by Professors Edson Belém and Fernanda


Nunes at Coti Informatics offers courses in the areas of
Programming, Database, Systems Analysis and
Web Design.

Our courses have as their main objective the


specialization and the full preparation of our students
for the Job Market. The credibility with
students, and the quality of education are concerns
constants that make Coti Informática a reference
in Teaching Programming, Web Design, Analysis of
Systems and Databases.

Coti Informatics.
Here the student is treated as a student.

In this booklet you will


learn about...
• What is Java and why has it become a widely used language.

• What it's like to program in an Object-Oriented way.

• Develop your first programs in Java.

• Understand concepts of Object-Oriented Programming such as Encapsulation and Polymorphism.

At the end of the lessons you


will be able to...
• Create and execute programs in the Java language.
Good
Studies
• Develop programs using the paradigm

Object Oriented

• Write programs that use Encapsulation,

Inheritance and Polymorphism

COTI COMPUTER SCIENCE E-LEARNING 2


Java Object-Oriented Logic Handbook

Lesson 1

Introduction
toJava
A lot is being said these days about Java and the whole revolution.
what he who provoking no way of life of
programmers, analysts and internet users. But what is
Java, after all? What are its origins and purposes?
Where does all this success come from?

Java is an Object-Oriented language (means a language oriented towards execution of


classes), its creation was carried out by the company Sun, and it was developed from the language
C++. The biggest boom of Java in the market is its use on the web, 80% of its applications are
developed for the internet, where today worldwide has adopted it becoming the largest and most
most used programming language in the world.

Additional Comments

The reason to use Java? Although it was made for the web, it has
robustness to work with 3D, mathematical applications, networks, can create
compilers and even local applications like java swing.

Important Knowledge

To use Java, it is first necessary to install the JDK, and then the IDE that you will use.
for example Netbeans or Eclipse.

The moment I install the jdk (which is the compiler, it also installs the virtual machine
Java JVM, which is its interpreter, runs the .class files. The JVM is independent.
platform because it sits on a layer above the OS, as it does not affect the System
Any platform can run it (Windows, Linux, Mac).

The IDE is used to assist in programming, it has code compilation within it.
line when the data is entered showing the error or not, has the code test, and
it has a debugger to check where and how the error occurred.

3
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Workbook

Understanding Java better

• Java is compiled, the source code is protected as it has a


.java file what is the class with the program, and when
compiled it creates a .class file, the client will only be able to have
access to the .class, where java is with the software factory
(who develops)

• The largest branch of Java in Brazil is for work in


web, website creation.

• Java is completely Object-Oriented, facilitating the


use of Unified Modeling Language (UML)

• Java was accepted by academia (Universities) and by


commerce (Companies).

• Java is very large, for learning due to its branches and frameworks.
constant developments.

• FrameWorks (These are programs that will help you put together a project using a)
pattern pronto como Struts que irá trabalhar bem com o MVC

• The most widely used IDE in the world is Eclipse, although Netbeans has thousands of.
followers just like me Professor Belém from Coti Informatics.

• The job market makes extensive use of Java, so a great programmer will not be left behind.
never unemployed (we are in the year 2010).

Why is Java platform-independent?

Java is a platform-independent language because the


Java programs are compiled into an intermediate form
de código denominada bytecodes que utiliza instruções e tipos
fixed-size primitives, big-endian ordering and a
standardized class library.

Bytecodes are like a machine language intended


to a single platform, the Java Virtual Machine (JVM Java
Virtual Machine), a bytecode interpreter.

A JVM can be implemented for any platform, thus we have that the same program
Java can be run on any architecture that has a JVM.

4
COTI COMPUTER SCIENCE E-LEARNING
Workbook on Object-Oriented Logic in Java

Building your
first application
Java in the NetBeans IDE

Installation of necessary software

First you should install the JDK (Java Development Kit) and then the IDE.
Integrated Development Environment you wish to use to develop your programs

Follow the steps below:

1. Obtaining and installing the JDK:

Access the website[Link] it


and install the application

COTI INFORMATICS E-LEARNING 5


Booklet on Logic in Object-Oriented Java

2. Obtaining and Installing NetBeans

Access the site[Link] and select the option of


download the IDE for Java distribution

3. Creating a first program in NetBeans

Select the File / New Project option

Choose Java / Java Application

Put the name of the project as Example, then select the location that
Do you want to save the project.

Also uncheck the option to create the main class, as this will be done in
followed in the next steps.

Your project will be displayed on the left side, the directory you will use to create your
source code files (Classes) are called 'Source Code Packages'.

COTI IT E-LEARNING 6
Java Object-Oriented Logic Notebook

To create a Class, it is necessary to first create a package. Packages are


used to store Classes (java files) to create a package click with the
Right in source code packages and choose the new option / java package.

Put the name of the class package

To create a Class inside the package, right-click on the package and


select new / java class

Enter the name of the Student Class (First letter in uppercase)

A file scope will be displayed with the main to consider it as a Class.

Define two attributes in the Class (for testing only, as the concepts of Object Orientation
Objects will be exemplified further on.

package class;

public class Student {

private String name;


private int age;

public void recebeDados(String n, int i){


name = n;
idade = i;
}

public void printData(){


[Link]("Nome..: " + nome);
[Link]("Age: " + age);
}
}

To run the Student Class, it will be necessary to create a main Class.


(Main call). To do this, right-click on the lesson package and select again
new / java class

Put the name of the Main Class

7
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Workbook

For this class to be used to run the application, it is necessary to create


a method called main. This method is used by the compiler to run your
application.

Later, the use of this method will be discussed in more depth, for now,
just create it as follows:

To make the Student Class become an object, type the code below:

Through the created object 'a', we can use the attributes defined in the Student Class.
example assign value to these:

To print the values of the Class, the command [Link]() was used in
method printData(). To run the program, right-click and then
Run File

The result below will be displayed:

Ready! You are equipped to follow the rest of this material using the IDE
NetBeans

COTI COMPUTER SCIENCE E-LEARNING8


Java Object-Oriented Logic Workbook

Lesson 2

Mainconceptsof
object-oriented
Java is an object-oriented language and, with that,
it is not possible to develop any program without following
such paradigm.

What do Classes, Attributes, and Methods mean?

Classes are programs that have an uppercase name, which accompanies the name of the
file, when the class is public, the class represents the business rule that its name will
mention, for example: If the class is Person, you cannot do mathematical programs
inside the Person class, the name must have cohesion with the programming, within the class
person you must have programs that will represent the name

public class Person{

private String name;


private String email;

public String getName(){


return name;
}

public void setName(String name) {


[Link] = name;
}

public String getEmail(){


return email;
}

public void setEmail(String email) {


[Link] = email;
}
}

Notes on the Person class:

Attributes
It has two attributes, which are global 'variables' of
a class" all the subprograms (methods) will
visualize the name and the email, they are private,
In other words, other classes will not see them, they are
closed to other classes, but will always be visible to
own class

COTI IT E-LEARNING 9
Java Object-Oriented Logic Workbook

Methods
They are subprograms of the classes, setName(parameter), getName(), setEmail(parameter),
getEmail(). Every method has parentheses, always emphasize this.

Notice that in the set method it is always created with void, which means no return, and the
method getNome() it was created with the type of attribute the name was created which was String.

Attributes (Variables)

private String name


Identifying qualifier attribute

Qualifiers
Qualifiers can be (public, protected, private, default (blank))

public - is a qualifier that will represent open for all classes,


any class when the attribute

protected - means to protect the attribute in a package that represents the


directory where the classes are.

default(empty) - It also means to protect the attribute in a


package but it is more restrictive than protected, it protects even the class.
with inheritance.

private - Means closed to all classes, no class will have


access to the private type attribute,

Commentary is represented by //
Example:

private int age;


The age will be closed to other classes.

protected String name;


the name is protected in the package (all classes in the package can see it)

char letter;
The letter is of the closed default type in the package.

10
COTI COMPUTER SCIENCE E-LEARNING
Workbook of Logic in Object-Oriented Java

Identifiers
First the primitives:

Integers
byte - from -2 raised to 7 to 2 raised to 7 - 1 (Example -128 to 127)
short - less than 2 raised to the power of 15 up to 2 raised to the power of 15 - 1

int -2 raised to 31 up to 2 raised to 31 - 1


long - from -2 raised to 63 to 2 raised to 63 - 1

Reais or floating
float - minus 2 raised to 31 to 2 raised to 31
double -2 raised to 63 to 2 raised to 63

Text
String represents any type of character, it is not a primitive type because
it is a class that comes from the [Link] package; which is already internal to java

char represents a character

Boolean
boolean- represents true or false

Global Type
Object - Class represents any type of data, can be number, String,
data, any type of desired class.

private String name="belém";


private int age=37;
private char sexo=’m’;

Operators

Operator Meaning Operator Category


+ Soma Arithmetic
- Subtraction Arithmetic
* Multiplication Arithmetic
/ Division Arithmetic
% Module Arithmetic
== Equality Relational
!= Inequality Relational
< Minor Relational
> Greater Relational
<= Less Than or Equal To Relational
>= Greater Than or Equal To Relational
= Assign Value of Attribution
&& AND Logical
|| OR Logical
! NOT Logical
^ XOR Logical

COTI IT E-LEARNING 11
Java Object-Oriented Logic Workbook

Observation:

In Java, we do not use attributes directly. They usually


are represented by methods, notice that whenever I declare them
I use private, so that no class can see it.

The representation methods are:

public String getName(){


return name;
}
public void setName(String name){
[Link] = name;
}

Note that...

The getNome() method has been declared as String because it will return the attribute
Name that was declared as String, the getter is of the same type as the Attribute, it

represents the attribute itself (It is the Output method, the return)

The setName(parameter) method is the input method that means I will


pass a name to the class attribute

Example:
[Link] = parameter;
class attribute name receives the parameter.

public static void main(String[] args)

It is the method that starts the execution of the Java program. It calls all other methods of
other classes. It is the method that is initially seen by Java Local and it is suggested that it be the

the first method to be looked for when reading a Java program, just like the 'Main' class
It should be the first class to be sought.

To execute Person, a Class is required that contains a standard method that


represents the execution of a program written in Java. This method is called main.
Example:

COTI IT E-LEARNING 12
Java Object-Oriented Logic Workbook

public class Teste{

public static void main(String args[]){

Class Person becoming object

Person p = new Person();

Data entry in the attributes of the Class

[Link]("Belem");
[Link]("belem@[Link]");

To print the data of Person use the code below:

[Link]([Link]());
[Link]([Link]());

}
}

Note that within the 'main' method, nothing is performed.


calculation operation.

Calculations or database operations, etc. can


Yes, they should be done in the 'main', but they SHOULD NOT, as it would break the

object-oriented model.

According to this guideline, there should be a small number of

instructions, if possible these instructions should be limited to

method calls from other classes.

It's as if the 'main' were a boss. The boss can perform tasks, but it shouldn't be done by those who

You must make your subordinates, in this case, the methods of other classes.

Remember that Java is 'case sensitive', meaning that the distinction between uppercase and lowercase letters matters.

Difference. "Main" and "main" are distinct things, the first is the main class and the second is a method.

principal.

COTI INFORMÁTICA E-LEARNING13


Java Object-Oriented Logic Workbook

Iunderstandbetter.

osobjetos

An object is like a clone or a copy of the class. In the reference program, see the
Following instructions: Person p = new Person(). Onew(new, refers to a new

object) calls the constructorPessoa() while creating an object

based on the Person class. All you need to understand is that it is like a copy
from the Person class and therefore have all the attributes and methods derived from the Person class.

Let's suppose a book that has blank spaces to be filled in. No one is going to use it.
the original book also because making an original book takes a lot of work, for this reason, it goes to print the

original mold for making copies.

The original book written directly by the author is the class, the copy is the object.

If the book is a copy, as is the case, it has blank spaces to be filled in by the students,
each copy may take on a different form, as different students will fill in the
blank spaces (as if each copy assumes a new state) and each student will do what
You can read with your copy, as long as the original book offers this resource, you can read only
the chapters that interest you and do the exercises that are available that the teacher
sent. Likewise, the object has an original form coming from the class, but it can be
used as desired by the user.

The most accurate definition of an object is: the memory location where it is stored.

State of the Class. However, we understand that this definition is not very assimilable for a

beginner student in Java. If it becomes easier to understand by this definition or by another definition,
make yourself at home.

COTI COMPUTER E-LEARNING 14


Java Object-Oriented Logic Guide

Lesson 3

Encapsulationand
inheritance

Encapsulation and Inheritance are two characteristics that


define Object Orientation as a paradigm
flexible and consistent to model the real world. Let's
get to know them better in this lesson

Creating your
first application
Java in IDE Eclipse

To download Eclipse, visit the following website:

[Link]
o/SR2/[Link]

It is not necessary to install Eclipse. Just unzip it and run it.


of the file [Link]

15
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Workbook

Upon opening the eclipse, you will need to specify which working directory to be used by the IDE.
This directory is called workspace and will store the projects and configurations made.
no Eclipse

To create a new project, select the option file / new / java project

Para criar um novo pacote, clique com o direito em src e depois em new / package coloque o
class name

Right-click on the package lesson / new / class and name it Person

Create two private attributes 'name' and 'age'

16
COTI INFORMATION TECHNOLOGY E-LEARNING
Java Object-Oriented Logic Workbook

To encapsulate the attributes, right-click on the class then go to source / generate getters
and setters. Check all the attributes that will be encapsulated and click ok

packageclass;

public classPessoa {

privateString name;
private intidade;

public String getName() {


return name;
}

public void setName(String name) {


[Link] = name;
}

public int getAge() {


return identity
}

public void setAge(int age) {


[Link] = idade;
}

To create the Main Class, give a right over the package class of after new / class

When creating the Class, you can already check the option to include the static void main method as
below:

COTI INFORMATICS E-LEARNING17


Java Object-Oriented Logic Handbook

Execute the Pessoa class in Main as shown below:

18
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Handbook

packageclass;

public class Main {

public static void main(String[] args) {

Person p = new Person();


[Link]("Edson Belem");
[Link](50);

[Link]([Link]());
[Link]([Link]());
}
}

To execute the Main Class, right-click on it and then run as / java


application

The result below will be displayed:

What does Encapsulation mean?

Although it is possible to declare attributes as public,


it is not convenient in terms of security. For this reason, it is

it is necessary for the variables to remain private


classes.

But how then will the other classes be able to

Use the variables if they are private?

The solution is to create methods that receive the values of the variables or send values to
variables and just work with them. In addition, methods can be handled and modified
without fundamentally changing the properties of the variable. The methods begin to represent
the attributes as if they had a "power of attorney" from them. Two types of methods are created to
the encapsulation one that starts with 'get' which would be responsible for displaying the values of

attribute and 'set' would be responsible for receiving new values and passing them to the attribute.

19
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Workbook

The main objective of encapsulation is to protect the attributes of a Class, that is, access.
the attributes should be done indirectly through methods that perform the input and
output of data for the attributes.

See the example:

public class Employee {


public String name;
public double salary;
}

By turning the class into an object, its attributes (declared as public) become accessible and
therefore, without any type of protection against any type of invalid data. Note:

Employee f = new Employee();


[Link] = “joao@[Link]”;
[Link] = 200.0;

Note that there is no coherence in the data passed to the Class attribute.
Here is the example of the same Class in an encapsulated form:

public class Employee{

Attributes qualified as private


private String name;
private double salary;

Input methods
public void setName(String name){
[Link] = nome;
}

public void setSalary(double salary) {


[Link] = salary;
}

public String getName(){


return name;
}

public double getSalary() {


return salary;
}
}

20
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Handbook

Important notes!

Note that the attributes were declared with qualifiers


private. This means that your access is only allowed inside
from the Funcionario class itself.

For each attribute declared as private, the following were written

input and output methods of data referred to by


get and set convention.

The methods declared with the set prefix are used to perform data input into the
attributes in such a way that their value is assigned indirectly.

The methods declared with the get prefix are used to return or output the
attribute values.

In this way, any type of data input or output validation rule could
can be easily implemented in the set and get methods, thereby making the attributes protected and
ensuring the integrity and consistency of the information stored by the object of the class.

In this way, the use of the Class would be done as follows:

Employee f = new Employee();


[Link]("Joao");
[Link](2000.0);

//or for returning and printing values

[Link]([Link]());
[Link]([Link]());

21
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Workbook

Inheritance

Inheritance is the mechanism by which a class obtains


characteristics (methods and attributes) of another class. The class
"child" that receives these characteristics is called a "subclass"
the class "parent" that passes these characteristics is called
superclass

The use of inheritance is directly related to reuse of


code. It is said that a class can extend or inherit another
Class when, in terms of data modeling, is able to-
to conjugate the verb 'to be' among these classes. For example:

Analyst is an Employee (So Analyst can be considered


employee inheritance

Department has Employee (In this case, it does not constitute inheritance, the demonstration of

relationships that use the verb "to have" will be shown in another class.

Inheritance is the first and most common type of relationship existing between classes. We can
represent it as follows:

public class Employee {

private String name;


private double salary;

public void setName(String name){


[Link] = name;
}

public void setSalary(double salary){


[Link] = salary;
}

public String getName() {


return name;
}

public double getSalary(){


return salary;
}

COTI INFORMATICS E-LEARNING22


Java Object-Oriented Logic Handbook

public class Analyst extends Employee {

private String type;

public void setType(String type){


[Link] = type;
}

public String getTipo(){


return type;
}
}

public class Programmer extends Employee {

private String environment;

public void setEnvironment(String environment){


[Link] = environment;
}

public String getAmbiente(){


return environment;
}
}

The representation of these classes in the form of a diagram is in accordance with a tree, where

We can assert that Employee is a superclass and Analyst and Programmer are
subclasses denote the concept of hierarchy (vertical relationship)

See, in the diagram below, the representation of the inheritance made:

COTI COMPUTER SCIENCE E-LEARNING 23


Java Object-Oriented Logic Handbook

Programmatically, inheritance is achieved through

of using the reserved word extends

Note: In Java, multiple inheritance is not allowed, that is, a child class
can only have 1 single parent class.

The execution of the Classes could be done as follows:

public class Teste{

public static void main(String args[]) {

Analyst a = new Analyst();

[Link]("Pedro");
[Link](4000.0);
[Link]("Senior");

Programmer p = new Programmer();

[Link]("Joao");
[Link](5000.0);
[Link]("Web");

Printing of object data


[Link]("Analyst: " + [Link]());
[Link]("Salary: " + [Link]());
[Link]("Type :

[Link]("Programmer: " + [Link]());


[Link]("Salary: " + [Link]());
[Link]("Environment: " + [Link]());

Note that the previous program creates objects for Analyst and
Programmer. Through inheritance, both share the same data (name and
salary) as they are inherited from the same class: Employee.

24
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Handbook

Inheritance: second example

Create a Client Class following the encapsulation pattern as below:

public class Client {

private String name;


private String email;

public String getNome() {


return name;
}

public void setName(String name) {


[Link] = name;
}

public String getEmail() {


return email;
}

public void setEmail(String email) {


[Link] = email;
}

Now, we will create a Person class inheriting from Client

public class IndividualPerson extends Client{

private String cpf;

public String getCpf() {


return cpf;
}

public void setCpf(String cpf) {


[Link] = cpf;
}

COTI INFORMATICS E-LEARNING25


Java Object-Oriented Logic Workbook

Execute on Main as follows:

public class Main {

public static void main(String[] args) {

Individual person pf = new Individual person();

[Link]("Sergio Mendes");
[Link]("[Link]@[Link]");
[Link]("1234567890");

Individual person......:
Email..............:
[Link]("CPF................: " + [Link]());

To practice ...
Develop the classes according to the diagram below and
demonstrate your execution in the Main

26
COTI COMPUTER SCIENCE E-LEARNING
Workbook of Logic in Object-Oriented Java

Lesson 4

Interfacesand
Polymorphism
The word Polymorphism comes from Greek and means many
forms. In programming, it is related to the technique of
put different methods with the same name for
to perform similar tasks. In this lesson we will learn
to effectively use this concept!

What are Interfaces?

In object-oriented programming, an interface is used as a means to standardize the implementation of


several Classes. In simple terms, an interface defines an obligation or contract that must be
to be followed by all the classes that implement them.

For example:

public interface Taxable {


public double calculateTax(double amount);
}

Note that methods declared in interfaces do not have a body, only their
declaration or signature.

It is said that a class implements an interface when it uses the operator


"implements". Unlike inheritance, a Class can implement multiple interfaces.
simultaneously, as long as I implement all the methods contained in the interfaces.
Example:

public class Product implements Taxable {


public double calcularImposto(double valor){
return value - (value * 0.18);
}
}

public class Service implements Taxable {


public double calculateTax(double value) {
return value - (value * 0.09);
}
}

27
COTI COMPUTER SCIENCE E-LEARNING
Java Object-Oriented Logic Workbook

Note that when the classes implement the interface, the method calculateTax becomes
if necessary. Thus, the interface has become a means of standardizing the use of
productAndServiceClasses

Through the use of interfaces, the execution of the program can be done in a way
more flexible, for example:

public class Teste{


public static void main(String args[]){
Taxable t = newProduct();
[Link]([Link](1000.0));
}
}

Or it could be executed like this:

public class Teste{


public static void main(String args[]){
Taxable t = newService();
[Link]([Link](1000.0));
}
}

Note that, as both classes implement the


same interface, the creation of the object can be done
using the name of the interface on the left and on the right

just defining the instance, that is, the type of taxable


what one wishes to create. This concept will be addressed from

more in-depth in a next class, but it is


called polymorphism.

COTI INFORMATICS E-LEARNING28


Java Object-Oriented Logic Manual

Interfaces: second example

public interface Financial

Method Signature (declaration)


public void calculateFinancing(double value);

/*
All classes that implement this interface must provide
body for the method calculateFinancing
*/
}

Now, let's implement the above interface:

public class Automobile implements Financialable {

public void calculateFinancing(double amount) {

[Link]("Value of the Car .: " + value);


Instalments in 12x ...:
}

Run your program in the main:

public class Main {

public static void main(String args[]){

Financial f = new Automobile();


[Link](30000.0);
}
}

Note that, through the reserved word implements, it executes the


obligation of the Class to implement what has been defined in
interface. The execution of the Automobile class can be done, as
demonstrated above, through the Financial interface

Financial f = new Automobile();

COTI INFORMATICS E-LEARNING29


Java Object-Oriented Logic Workbook

A execução do objetofcarrega o tipo Financiavel mas foi


initialized as Automobile, which implies that the execution of
calculateFinancing return the response of what has been implemented
in the Car Class. Setting up the use of polymorphism.
so we can say that the Car Class is being executed
as behavior for the Financial interface.

The term polymorphism comes from Greek and means many forms.
programming is related to the technique of placing methods
different ones with the same name to perform similar tasks
in principle (if poorly programmed they can do things
completely different).

To practice...

• Implement the diagram below:

Create an interface Shape with


a method getArea.

Create two classes


implementing Form:
Rectangle and Triangle, in these
classes programe o metodo
obtainArea according to the
type of calculation that should be
performed for each of them

Demonstrate the execution of the Rectangle and Triangle classes in the main.

through the use of polymorphism

COTI COMPUTER SCIENCE E-LEARNING 30


Java Object-Oriented Logic Workbook

Let's go
Practice

This is the moment when you,


student, must put in
practice what was addressed in the
previous lessons!

1. Crie um programa em Java que demonstre o uso de uma Classe Produto com
the attributes name, brand, and price. Use encapsulation for the attributes.

2. Create a Java program that implements the following Classes: User,


Student and Teacher. Create an inheritance relationship between the Classes.
demonstrate your execution.

3. Describe the following types of data:


a. int
b. float
c. long
short
e. double
f. boolean
g. char
h. string

4. Explain the difference between the qualifiers private, protected, package, and
public.

5. Create a program that demonstrates a Payment interface and two Classes


that implements: Employee and Intern. Create a method
obtainPayment and write it for each Class in the hierarchy. Demonstrate the
execution of the interface through the concept of polymorphism.

COTI IT E-LEARNING 31

You might also like