You are on page 1of 7

OOP in JAVA

Exercise 1: GUI- in JAVA

public class Authentification extends JFrame implements ActionListener {


JTextField login;

JPasswordField motpass= new JPasswordField ();

JButton connexion,annuler;

public Authentification(){
super("Identification"); //(1)

this.setPreferredSize(new Dimension(400,250));

JPanel pan1, pan2; //(2)


login=new JTextField();
connexion=new JButton("Connection");

connexion.setActionCommand("connexion");

connexion.addActionListener(this);
annuler=new JButton("Cancel");

annuler.setActionCommand("annuler");//(3)

annuler.addActionListener(this);

annuler.setPreferredSize(new Dimension(98,20));
//pan1 contient le nom de l'entreprise

pan1=new JPanel(new FlowLayout());

pan1.add(label);

//pan2 contient tous les autres champs


JLabel log=new JLabel("Username : ");

JLabel mot=new JLabel("Password : ");

pan2=new JPanel(new FlowLayout());


pan2.add(new JLabel(" "));

pan2.setLayout(new FlowLayout());
pan2.add(log);

pan2.add(login);
pan2.add(mot);

pan2.add(motpass);

pan2.add(connexion);

pan2.add(annuler);
this.getContentPane().add(pan3);

this.setResizable(false);

pack();
}
}

The following source code creates an interface to sign in to the system.

1. Explain the line: public class Authentification extends JFrame implements


ActionListener {
 This line declares a class named `Authentification`, which
extends the `JFrame` class and implements the
`ActionListener` interface.
 By extending `JFrame`, the `Authentification` class inherits
properties and methods of the `JFrame` class, allowing it to
function as a window or frame in a Java Swing application.
 By implementing `ActionListener`, the `Authentification`
class can handle events generated by user interactions with
components such as buttons.

2. What class allows creating the text input field in JAVA SWING? What variables
contain the input field for “Password” and “Login”?
 The `JTextField` class is used to create text input fields in
Java Swing.
 In the provided code:
 The `JTextField` variable `login` represents the input
field for the login username.
 The `JPasswordField` variable `motpass` represents the
input field for the password. `JPasswordField` is used
specifically for password input fields to mask the
entered characters.
2. What are the uses of the variables of line (2)?
 `login`: Represents the text input field where users enter
their username or login information.
 `motpass`: Represents the password input field where users
enter their password.
 `connexion`: Represents the button used to initiate the login
or connection process.
 `annuler`: Represents the button used to cancel or reset the
login process.

4. A panel in JAVA can have different layouts to present his components; state 04

layouts used in JAVA.


 `FlowLayout`: Arranges components in a left-to-right flow,
wrapping to the next row if necessary.
 `BorderLayout`: Divides the container into five regions:
North, South, East, West, and Center.
 `GridLayout`: Organizes components in a grid-like fashion
with specified rows and columns.
 `BoxLayout`: Arranges components in a single direction
(either horizontally or vertically).

5. In this example which layout is been used for the panels?


 In this example, the `FlowLayout` layout is primarily used
for the panels (`pan1` and `pan2`).

6. Draw the output of this source code


 Without the complete code and missing components such as the
definition of `label` and `pan3`, it is not possible to draw
the exact output. However, based on the provided code:
 The output would likely be a JFrame window titled
"Identification" containing text fields for username and
password, along with "Connection" and "Cancel" buttons.

7. What does the line (3), can it be applied on a text field instead of a button?
 Line (3) sets the action command for the "Cancel" button
(`annuler`). When this button is clicked, an action event
with the command "annuler" will be generated.
 This line cannot be directly applied to a text field because
text fields do not directly generate action events like
buttons do. However, text fields can be associated with
action listeners to respond to different types of events,
such as text input or focus changes.

8. Write the instruction to create a button with the text on it “Validate”.

 JButton validateButton = new JButton("Validate");


 This line of code creates a new JButton instance named `validateButton` with the
text "Validate" displayed on the button.

Exercise 2: General Knowledge

1. Explain the following: JAVA is an Object Oriented Programming Language


 Java is an object-oriented programming language because it
follows the principles of object-oriented programming
(OOP).
 This means that it organizes code into objects, which
encapsulate data and behavior, and interact with each
other through well-defined interfaces.
 Java supports features such as inheritance, polymorphism,
encapsulation, and abstraction, which are fundamental
concepts of OOP.

2. What library is used to build GUI in JAVA?


 The primary library used to build graphical user
interfaces (GUIs) in Java is called Swing. Swing provides
a set of components and classes that allow developers to
create rich, interactive GUI applications for desktop
platforms.
 JavaFX is another library that can be used for building
GUIs in Java, offering more modern features and
capabilities.

3. Describe the following characteristics concerning JAVA: Dynamic, robust, object


oriented.
 Dynamic: Java is considered dynamic because it supports
features such as dynamic memory allocation (garbage
collection), dynamic loading of classes (reflection), and
dynamic method invocation (runtime polymorphism). These
features enable flexibility and adaptability in Java
applications.
 Robust: Java is robust because it incorporates various
mechanisms to ensure reliability and robustness in program
execution. Features like strong type checking, exception
handling, automatic memory management (garbage
collection), and runtime bounds checking contribute to the
robustness of Java programs.
 Object-Oriented: As mentioned earlier, Java is object-
oriented, meaning it emphasizes the use of objects to
represent data and functionality. This allows for modular,
reusable, and maintainable code, as well as promoting
concepts such as encapsulation, inheritance, polymorphism,
and abstraction.

4. What are the popular JAVA programming IDE?


 Eclipse: A highly customizable IDE with extensive plugin
support and a large community.
 IntelliJ IDEA: Known for its intelligent code assistance
and productivity-boosting features.
 NetBeans: An open-source IDE with a comprehensive set of
tools for Java development.
 Visual Studio Code: While not specifically designed for
Java, it offers Java support through extensions and is
widely used by developers for its flexibility and
features.

5. How do we implement the inheritance in JAVA?


In Java, inheritance is implemented using the `extends` keyword. A
subclass (or derived class) can inherit attributes and methods from
a superclass (or base class) by specifying the superclass after the
`extends` keyword in the class declaration. For example:

class Animal {

void sound() {

System.out.println("Animal makes a sound");

class Dog extends Animal {

void sound() {

System.out.println("Dog barks");

In this example, the `Dog` class inherits from the `Animal` class,
and the `sound()` method is overridden in the `Dog` class to
provide a specific behavior for dogs.
Exercise 3
import java.io.*;

public class Employee {

// salary variable is a private static variable

private static double salary;


// DEPARTMENT is a constant

public static final String DEPARTMENT = "Development ";

int S = 0;
public static void main(String args[]) {
salary = 1000;

for (int i= 0; i<10 ; i= i+2) {

S += i;

}
System.out.println(DEPARTMENT + "average salary:" + salary);

1. What are the values of S and i at the end of the loop?


 The loop starts with ‘i’ initialized to 0, and it
increments by 2 in each iteration until ‘i’ is less than
10
 The loop performs addition of ‘i’ to the variable ‘S’ in
each iteration
 At the end of the loop, ‘S’ will contain the sum of even
numbers from 0 to 8 (inclusive), and ‘i’ will have the
value of 10

2. What are the meanings of the keywords private, public and static used in this code?
 private: it is an access modifier that restricts access to
the variable or method to within the same class. In this
code, “private static void double salary” declares a private
‘salary’ accessible only within the ‘Employee’ class
 public: it is an access modifier that allows access to the
variable or method from any other class. In this code,
‘public static final String DEPARTMENT = “Development;”
declares a public static final variable, ‘Department’
accessible from any class

 static: it is a keyword that indicates the variable or method


belongs to the class rather than to instances of the class.
In this code, “private static double salary salary;” and
‘public static final String DEPARTMENT = “Department”;’
declare static variables, ‘salary’ belong to its class

3. What are the attributes and methods of this JAVA class?


 Salary,
 Department and
 S

4. Declare the default constructor for this class


 Employee(){}

You might also like