Download Practical activity 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Week 2
Fundamental concepts of Object-Oriented Programming
See the course presentation
The first Java program
class FirstProgram{
public static void main(String[ ] arg) {
System.out.print(“Hello world!”);
}
}
Java programs are collections of classes (in this particular case, we have only one class). To
describe a class, you have to use the following syntax:
class class_name {
// class interior
}
Every program, no matter if is procedural or object-oriented, has a starting point. In C
language you had the main() function, in Java language you will use a root class which
contain a method with the following prototype:
public static void main(String[ ] arg)
The public static void keywords mean the Java virtual machine (JVM) interpreter
can call the program's main method to start the program (public) without creating an
instance of the class (static), meaning an object, and the program does not return data to
the Java VM interpreter (void) when it ends. We will talk more about public and
static next week. The parameter of the main’s method is an array which can be used to
pass arguments to the program when running it from the command line.
This simple program will print on the screen the argument of the print() method. The
prefix System.out is the name of a predefined object used when we want to print
something on the screen. Due to the fact that the methods are part of a class, it is mandatory
when calling a method, to specify the name of the class (if it is a static method) or the name
of the object which owns it:
class’_name.method’s_name(list_of_parameters)
object’s_name.method’s_name(list_of_parameters)
A Java program can have two types of classes:
— user defined classes;
— predefined classes, built in Java language, which form the so called API –
Application Programming Interface.
The print()/println() methods can receive as parameter a constant String or an
expression which has a String result, or values of primitive types (which will be
1
automatically converted to String type). Println() method can be used without any
parameter, in which case marks the end of the current line and goes to a new line:
System.out.println( );
From the source file to the executable file
The easiest way to write a simple program is with a text editor (Notepad, Textpad, etc.). So,
using the text editor of your choice, create a text file with the source text, and be sure to name
the text file name.java. Java programs are case sensitive, so if you type the code in
yourself, pay particular attention to the capitalization.
Let’s say that your first program is:
FirstProgram.java.
A program has to be converted to a form the Java VM can understand so any computer with a
Java VM can interpret and run the program. Compiling a Java program means taking the
programmer-readable text in your program file (also called source code) and converting it to
bytecodes, which are platform-independent instructions for the Java VM.
The Java compiler is invoked at the command line on DOS shell operating systems as
follows:
javac source_file_name.java
meaning in our case:
javac FirstProgram.java
If the compilation is error-free, will result a number of files having .class extension equal
with the number of classes in the source file. In this particular case will result a single file
named FirstProgram.class.
Once your program successfully compiles into Java bytecodes, you can interpret and run
applications on any Java VM, or interpret and run applets in any Web browser with a Java
VM built in such as Mozilla Firefox or Internet Explorer. Interpreting and running a Java
program means invoking the Java VM byte code interpreter, which converts the Java byte
codes to platform-dependent machine codes so your computer can understand and run the
program.
The Java interpreter is invoked at the command line on DOS shell operating systems as
follows:
java root_class_name
in our case:
java FirstProgram
At the command line, you should see:
Hello world!
Let’s see how can we pass arguments to a program when invoking the interpreter. Suppose a
program which presents a student’s first name and last name:
class Presentation{
2
public static void main(String[ ] arg) {
System.out.println(“I
am
the
student
“+arg[0]+”
“+arg[1] +” “+”from English section, ETC”);
}
}
After the compilation, at the command line you should write:
java Presentation Dan POPESCU
The program received two arguments (Dan and POPESCU), which are the first two elements
of arg array. Like in C language, in Java the elements of an array are index starting from 0.
If the program does not receive the expected two arguments, the running will be stopped with
the following message: IndexOutOfBoundsException. In these situations, it is always
indicated to verify in the program if the user provided the necessary number of arguments:
class Presentation{
public static void main(String[ ] arg) {
if (arg.length < 2)
System.out.println(“Wrong number of arguments!!”);
else
System.out.println(“I am the student “+arg[0]+”
“+arg[1] +” “+”from English section, ETC”);
}
}
The expression arg.length generates the number of elements from arg array. As you can
see, the parameter of println() method is an expression built by applying the “+”
operator on String operands. If in C language the printf() can have a variable number
of parameters, in Java language the print()/ println() methods can have ONLY ONE
parameter. So, when we want to print more than one value, we have to concatenate them into
a unique parameter.
Problem
Create a Java program able to compute and print on the screen the perimeter and area of a
rectangle. The values for length and width are given as arguments when running the program.
Indication:
A program can receive only parameters of type String, as arguments via arg array. In
order to use these parameters to compute something, you have to convert them from String
to Integer or Double. The conversion can be made using the expression:
Type x = Type.valueOf(arg[i])
3