In this tutorial you will be using the BlueJ IDE to develop java classes. BlueJ is ...
this program, you can later change which Java version BlueJ uses. Execute.
Object Oriented Programming
Java Tutorial 1 Charlie Abela Department of Artificial Intelligence
[email protected]
Charlie Abela
Tutorial 1 In this tutorial you will be using the BlueJ IDE to develop java classes. BlueJ is a Java™ development environment specifically designed for teaching at an introductory level. It was designed and implemented by the BlueJ team at Deakin University, Melbourne, Australia, and the University of Kent at Canterbury, UK.
1.
Introduction to BlueJ
Installation on Windows is a straight forward process. The distribution file is called bluejsetup-xxx.exe, where xxx is a version number. For example, the BlueJ version 2.0.0 distribution is named bluejsetup-200.exe. You might get this file on disk, or you can download it from the BlueJ web site at http://www.bluej.org. Execute this installer. The installer lets you select a directory to install to. It will also offer the option of installing a shortcut in the start menu and on the desktop. After installation is finished, you will find the program bluej.exe in BlueJ’s installation directory. The first time you launch BlueJ, it will search for a Java system (JDK). If it finds more than one suitable Java system (e.g. you have JDK 1.4.2 and JDK 1.5.0 installed), a dialog will let you select which one to use. If it does not find one, you will be asked to locate it yourself (this can happen when a JDK system has been installed, but the corresponding registry entries have been removed). The BlueJ installer also installs a program called vmselect.exe. Using this program, you can later change which Java version BlueJ uses. Execute vmselect to start BlueJ with a different Java version. The choice of JDK is stored for each BlueJ version. If you have different versions of BlueJ installed, you can use one version of BlueJ with JDK 1.4.2 and another BlueJ version with JDK 1.5. Changing the Java version for BlueJ will make this change for all BlueJ installations of the same version for the same user.
1.1 Keep it Simple BlueJ projects, like standard Java packages, are directories containing the files included in a project. After starting BlueJ, use the Project – Open... menu command to select and open a project. 1. The window might not look exactly the same on your system, but the differences should be minor.
Tutorial 1
Java Tutorials©
2
Charlie Abela
Figure 1: BlueJ interface
2. Preferences related to the editor and other options can be set from here
Figure 2: BlueJ preference settings
3. To create a new project, select Project – New... from the menu. A file selection dialogue opens that lets you specify a name and location for the new project. Try that now. Set the name of the new project to tutorial_1. After you click OK, a directory will be created with the name you specified, and the main window shows the new, empty project.
Tutorial 1
Java Tutorials©
3
Charlie Abela
4. Lets try out BlueJ by creating a new class in a tutorial_1. Select the New Class... menu item or toolbar button. This is only possible while you have the project open. A dialog will appear to let you enter a name for the new class: You have to enter a name, and choose one of the options “Class”, “Abstract Class”, “Interface”, "Applet", “Unit Test” or Enum.
Figure 3: Creating a new class
The name must be a valid Java identifier. In short, it must be a word that consists only of letters, digits and the underscore (_). No spaces or other characters are allowed. The first character may not be a digit. Choose a name such as “HelloWorld”. “Class”, “Abstract Class”, “Interface”, "Applet", “Unit Test” or “Enum” options only determine what kind of skeleton is used for the initial source of the class. It is not a final choice: you could change your mind later, and replace the complete source code of, say, a class with code for an interface. The environment would recognise that the class is now an interface and treat it accordingly. Of course, this works the other way around as well. 5. Once the class has been created, the Editor can be accessed.
Figure 4: Editing code
Tutorial 1
Java Tutorials©
4
Charlie Abela 6. With the creation of each class, some code will automatically be created. This can be edited as required.
Figure 5: Using the editor
7. To permanently set the coding preferences, such as brackets style (inline or not), default variable declarations and methods, we can edit BlueJ’s underlying templates which are found it the following directory: home\BlueJ\lib\english\templates\newclass. Each template related to classes, applets etc can be edited separately. 8. Edit the code in the HelloWorld class and enter only the following statements. public class HelloWorld{ public static void main(String[] args){ System.out.println("Hello World"); } }
9. To compile this code, just hit the compile button. This will automatically save and compile the code. If no errors are detected then the message: Class compiled – no syntax errors, will be displayed at the bottom of the editor
Tutorial 1
Java Tutorials©
5
Charlie Abela
Figure 6: Compiling code
10. To execute the Class rt. click and choose void main(String[] args)
Figure 7: Executing classes
11. The output can be seen through the Terminal window. If the terminal window does not launch automatically: Click on View, Choose Terminal.
Figure 8: Checking Output in Terminal
Tutorial 1
Java Tutorials©
6
Charlie Abela 12. Appropriate documentation can be generated automatically. A browser window will be launched and the documentation displayed as a web page
Figure 9: Generate JavaDoc
Figure 10: View Java documentation
2.
Problems
The following coding problems are related to the basics of the java language: topics covered include, variables (declaration and initialisation), constants, data types, numeric type casting, operator precedence and associativity, input from console.
Tutorial 1
Java Tutorials©
7
Charlie Abela
2.1
Problem Set 1. Write a program CalorieCalc.java, that accepts a person’s weight and displays the number of calories the person needs in one day. Use the formula: calories = bodyweight * 24
2. Write a class Pythagoras.java, that reads the lengths of two sides of a right -angled triangle a and b. The program should then calculate and display the length of the hypotenuse, using the formula :
h = √(a2 + b2)
3. Write a program, CorrectChange.java, which takes in a purchase price and a tendered amount and then displays the change in euros, 50 euro cent, 20 euro cent and 10 euro cent coins.
Purchase Price:
Euro 34.80
Amount Tendered:
Euro 40.00
Your change:
Euro
5.20
5
one euro coin(s)
0
50 cent coin(s)
1
20 cent coin(s)
0
10 cent coin(s)
4. A JavaCoffee outlet runs a catalogue business. It sells one type of coffee beans. The company sells the coffee in 2kg bags only and the price of a single bag is Euro 5.50. When a customer places an order, the company ships the order in a box. The boxes come in three sizes, large (hold 20 bags), medium (hold 10 bags) and small (hold 5 bags). The cost of packaging varies according to box size: large (Euro 2), medium (Euro 1.5) and small (Euro 1). E.g. the cost of 52 bags will be shipped in two large, one medium and one small, boxes. Write a program, JavaCoffee.java, that computes the total cost of an order and displays the output as below.
Tutorial 1
Java Tutorials©
8
Charlie Abela
Number of bags ordered: 52 – Euro 286 Boxes used: 2 large
-
Euro 4.00
1 medium
-
Euro 1.50
1 small
-
Euro 1.00
Total cost is:
Euro 292.50
Vat @ 18% :
Euro
Total:
Euro 345.15
52.65
5. Write a program, LoanCalculator.java, which calculates both monthly and total payments for a given loan amount, annual interest rate and loan period. The user is expected to enter three values: loanAmount, interestRate and loanPeriod. The program will compute the monthly and total payments and then outputs the results. See example output below. Amount
Rate
Loan
Monthly
Annual
10000
10
10
132.151
15858.088
15000
7
15
134.824
24268.363
6. Write a program, TicketBooth.java, which computes the total ticket sales for a concert. There are three types of seatings: A, B and C. The program accepts the number of tickets sold and the price of a ticket for each of the three types of seats. The total sales are computed and result outputted. 7. Implement a program, Ticket.java. The program should perform the following operations: i. Read from the keyboard the first and the last name of a passenger. ii. Read from the keyboard the names of the departure city and country. iii. Read from the keyboard the names of a destination city and country. iv. Display the ticket's full details, including the pricing details of the ticket (assume that one price fits all and that Vat is 18% and taxes are a fixed amount)
Tutorial 1
Java Tutorials©
9