concordia university department of electrical & …m_altaha/teaching...concordia university...
TRANSCRIPT
Concordia University
Department of Electrical & Computer Engineering
Introduction to Telecommunication Networks
ELEC 463
Mohammad Altahat
Lab 1: Introduction to Java Programming on Eclipse
1. Objectives:
1- Introduction to Java Programming.
2- Introduction to Graphical Interfaces.
2. Lab Experiment:
In this lab, we are going to learn the programming basics for Java. Java is a high-level
programming language originally developed by Sun Microsystems and released in 1995. It runs
on different platforms including Windows, MacOS, and Linux.
Java is object oriented language, where everything is an object. It is simple and platform
independent which means it is not compiled into platform specific machine. It is compiled into
independent byte code that is interpreted by the Java Virtual Machine on any platform it is being
run on.
The tools needed for this lab are:
- Windows XP/7 or higher, or Linux operating system.
- Java JDK installed.
- Eclipse development environment.
2.1.First Java Program (Hello World)
This simple code prints the sentence “Hello World”. public class helloworld {
public static void main(String[] args) {
System.out.println("Hello World"); } }
Using Eclipse to write, compile, and run java programs
Eclipse is an integrated development environment (IDE) used in computer programming, and
is the most widely used Java IDE. It contains a base workspace and an extensible plug-in
system for customizing the environment. Eclipse is written mostly in Java and its primary use
is for developing Java applications, but it may also be used to develop applications in other
programming languages.
To implement the Hello World program in Eclipse you need to follow these steps:
a. Open Eclipse, then select from the menu: File → New → Java Project.
b. Enter "helloworld" as the project name. Keep rest of the settings as shown in the following
screenshot and then click Finish. Eclipse IDE will generate the java project.
c. Right click on “src” folder and select from context menu New → Package.
d. Write “elec463.helloworld” in the 'Name' field and click "Finish" button.
e. Right click on “elec463.helloworld” package and select from context menu New → Class.
f. Write “HelloWorld” in the “Name” field and select the check-box for “public static void
main(String[] args)” to automatically write the main function.
g. Click “Finish” button. Eclipse will generate a java class and open the java editor as
shown below.
h. Type the code “System.out.println("Hello World");” inside the main function.
i. To run the code: Right click on “HelloWorld.java” and select from context menu “Run As”
→ “Java Application”.
j. The output will be shown in the console window of Eclipse.
2.2.Programming Basics Tutorial
1. Decision Making:
a. if statement:
if (Boolean_expression) {
// Statements will execute if the Boolean expression is true
}
Example:
b. if else statement:
if (Boolean_expression) {
// Executes when the Boolean expression is true
} else {
// Executes when the Boolean expression is false
}
c. switch statement:
switch (expression) {
case value :
// Statements
break; // optional
case value :
// Statements
break; // optional
// You can have any number of case statements.
default : // Optional
// Statements
}
2. Loops
a. while loop
while (Boolean_expression) {
// Statements
}
b. for loop
for (initialization; Boolean_expression; update) {
// Statements
}
3. Functions
modifier returnType nameOfMethod (Parameter List) {
// method body
}
2.3.Basic GUI in Java
1- Frame
A Frame is a top-level window with a title and a border. Add this code in the main
function to have an empty frame window.
JFrame frame = new JFrame(); frame.setBounds(100, 100, 500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("GUI Application" );
frame.getContentPane().setLayout(null); frame.setVisible(true);
2- Label
Label is used to display a single line of read only text. The text can be changed by the
application but a user cannot edit it directly. Add the following code to have a label in the
previous frame and set its text to “Welcome to ELEC 463”.
JLabel label = new JLabel("");
label.setBounds(20, 20, 200, 20); label.setFont(new Font("Times", Font.BOLD, 16)); label.setHorizontalAlignment(SwingConstants.LEFT); label.setVerticalAlignment(SwingConstants.CENTER); frame.getContentPane().add(label);
label.setText("Welcome to ELEC 463");
3- Text Field
Text Field is a text component that allows editing of a single line text. Add the following
code to have a text field in the previous frame.
JTextField textfield= new JTextField("");
textfield.setFont(new Font("Times", Font.BOLD, 14)); textfield.setBounds(20, 70, 150, 20);
frame.getContentPane().add(textfield);
4- Button
Button is an implementation of a push button. This component has a label and generates
an event when pressed. It can also have an Image.
JButton button = new JButton("Click");
button.setBounds(20, 120, 100, 23); frame.getContentPane().add(button);
To execute something when clicking the button, add this code. This will add the text you
type in the text field to the label text previously created.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { label.setText("Welcome " + textfield.getText() + " to ELEC 463"); } });
You need to import these two libraries:
java.awt.event.ActionEvent
java.awt.event.ActionListener
5- Text Area
Text Area provides a component that displays multiple lines of text and optionally allows
the user to edit the text. This code adds a text area to the previous frame window and
write some lines of text in it.
JTextArea textarea = new JTextArea(); textarea.setBounds(20, 150, 300, 300); frame.getContentPane().add(textarea);
textarea.setText("Hello" + '\n'); textarea.append("Welcome to ELEC 463 Lab \n");
textarea.append("Good luck");