Laboratory 3
Before starting
Classes should start with a capital letter, while attribute, method, package and project names start with a lowercase letter.
Class, attribute, method and package are Java concepts, whereas project is an Eclipse concept.
Recall how to compile and run Java applications in the command line, and how to create and run jar files here.
Goals
Being able to run a simple java application and create the corresponding .jar file, both from the command line and the Eclipse environment.
Get familiar with Eclipse environment.
Exercise 1
Consider the canonical program “hello world” which prints to the terminal the message
Hello world!
a) Edit and execute this canonical program in the Eclipse environment:
- Create a Java project named lab3.
- Create a Java package named hello.
- Create a Java class named HelloWorld.
- Add to the HelloWorld class a main method that prints the required string to the terminal.
- Execute this canonical program within the Eclipse environment.
c) Create a .jar file with the program and execute it.
Goals
Understand the main java primitives:
- classes, attributes and methods
- constructors
- inheritance from Object: toString, equals, hashCode
Exercise 2
A complex number is a number that can be expressed in the form a+bi, where a and b are real numbers. In this expression, a is the real part and b is the imaginary part of the complex number.
Complex numbers are added by adding the real and imaginary parts, that is, (a+bi) + (c+di) = (a+c) + (b+d)i.
They are subtracted by subtracting the real and imaginary parts, that is, (a+bi) - (c+di) = (a-c) + (b-d)i.
The multiplication of two complex numbers is defined as (a+bi) (c+di) = (ac-bd) + (bc+ad)i.
a) In a package named lab3, define a class Complex to represent complex numbers. A Complex object should be immutable once created; the add, subtract and multiply routines return newly-created Complex objects containing the results.Additional exercise
As you know the LinkedList (generic) class is already provided in the java.util package. In order to understand how it works in Java, create your own package and implement a linked list of integers with the methods:- add(int value) which appends the specified value to the end of this list, and
- remove() which removes the head of this list.