Java Fundamental period - Lecture 3

From OO Concepts to Java Programming

Summary of the last lecture

We have seen what is the UML notation, and applied it for class diagrams
It is now time to produce Java Code !

Summary of the last lecture

Who could define the 4 main concepts related to class relationship we've seen the last time?

Summary of the last lecture (2)

Our First Java Program : discovering Eclipse and Java

For this lecture we will need the Eclipse IDE

Using Eclipse

Eclipse global Overview :

Using Eclipse : Perspectives

As previously said, the views can be organized to increase your productivity
A certain arrangement of those views is called a Perspective

Using Eclipse : Projects

Eclipse has several project creation assistants
For now we will use the Java Project assistant
Let's begin a new Java Project !

Using Eclipse : Create a Java project

Notice that you can have other project creation assistant, depending on what perspective you are

Java : From Object Concepts to Java

We will create our first Java Class

package fr.tbr.exercises;

public class JavaSyntaxDemo {
     private String demoVersion = "1.0_DEV";

     //Constructor
     public JavaSyntaxDemo() {

     }

     public String getDemoVersion(){
        return this.demoVersion;
     }

}

Code analysis

Code analysis (2)

Exercise

Remember the bank system? Create each class in eclipse
  • Each "text" typed field should be represented as a String
  • Each "numeric" typed field should be represented as a double
  • Do not represent links between classes, the goal is only to create each classes
  • Code help: String and double instantiations
    
    String message = "Hello World";
    double amount = 10.2;
                    

Exercise (2) : Class diagram

Different kinds of Type

In Java, you can meet two "kinds of type"

Different kinds of Type: Primitive types

Type Description Range
byte

a signed byte

-128 to 127

short

This is a two-bytes signed integer, it defaults to 0 if not initialized

-32,768 to 32,767

int

This is a signed integer, it defaults to 0 if not initialized

-2,147,483,648 to 2,147,483,647 (inclusive)

Different kinds of Type: Primitive types (2)

Type Description Range
long The long is a signed integer with a wider range. The declaration of a long is a bit different, you should do it as described below
						
long primitiveLong = 1222222333335555l;
						
					
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Different kinds of Type: Primitive types (3)

Type Description Range
float

The float is a signed floating point number

1.40129846432481707e-45 to 3.40282346638528860e+38

double

The double is a type aiming at covering a very large range of values. However, its usage is discouraged since it is addressed on a 8 bytes format. If you use large tables of values, prefer float (if the range is ok for that)

4.94065645841246544e-324d to 1.79769313486231570e+308d

Other

Different kinds of Type: Primitive types (4)

Type Description Range
boolean

the boolean in Java can have two values true or false, which are reserved keywords of the language

true or false
char

2 bytes, unsigned, Unicode, char are used to represent characters, but are not directly compatible with integers or Strings

0 to 65,535

Numerical operators

There are several numerical operators :

Numerical operators (2)

There are several numerical operators :

Exercise