Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439


This lab has three main parts, and a summary.

  1. Windows 7

  2. Basic BlueJ

  3. EtchASketch

  4. Checkstyle

  5. Summary

This lab is not marked; however subsequent labs will build on the concepts introduced here. If you cannot complete any sections of this lab, please seek help as soon as possible.

Those with lab times before the lecture may find it useful to read the lecture notes in advance.

Carefully read the summary at the end of the lab sheet, and make sure that you understand it all. If there is anything, no matter how minor, that you are unsure about, then try and clear it up as soon as possible.




Windows 7

The environment in the laboratories is Windows 7. As we are using an Integrated Development Environment (or IDE), we will not need to know too much about Windows itself.

Note: The diagrams below are from Windows 2000, so may vary slightly from Windows 7.

The things that you will need to know are

  1. How to use Internet Explorer to view Web pages

    If you do not know how to use a Web browser, please see the lab demonstrator. Most students should have used some browser, probably Internet Explorer, before.

  2. The hierarchical structure of the system of folders (directories)

    Each student has a home directory, or folder, which is an area of disk memory where files created during the semester can be stored. The name of this home directory will match your user name. Each user's home area appears to the computer as a "virtual disk drive" which is called the H: drive or H: for short.

    You can open a window that displays the contents of your H: drive by opening (double-click with the mouse) the "My Computer" icon at the top-left corner of the screen, and then opening the icon labelled H:.

    You can create new folders inside this one, by using the menus on the window frame. Click on the File menu at the top-left, then hover your mouse over the New line until a sub-menu pops up, and then select the line Folder.

    When we talk about selecting items from menus and submenus, we use notation of the form

    File | New | Folder

    to refer to the item Folder on the sub-menu New on the menu File.

    The New Folder will first be created with the default name New Folder, which you should change to something sensible, such as CITS1200labs.

    If you are new to Windows you should spend some time browsing through the other menus to discover what functions they provide.

    Before leaving this section, double-click on the new CITS1200labs folder, and then create 8 subfolders called lab01, lab02, lab03, ... lab08 for your lab work.




    BlueJ

    Our first use of BlueJ will be to use a very simple program, and simply to construct, inspect and use some objects from a class defined by another programmer (i.e. the lecturer).

    1. Make sure that you have created the folder CITS1200labs and sub-folder lab01 in your home area as detailed above.

    2. Download and save the source code file into the lab01 folder

      (You do this by holding the mouse cursor over the link, and then clicking the right mouse button. From the pop-up menu that results, you should choose "Save Target As" or "Save Link As" and from the resulting menu select H:, then CITS1200labs and finally lab01 before clicking the button marked "Save".

    3. Start up BlueJ

      This is done by clicking on the BlueJ icon on the desktop; this may take some time as it has to do a significant amount of initialization before it is ready. Eventually the BlueJ window will appear. Try not to click on BlueJ again, or you will wait twice as long and end up with two copies.

    4. Select the Open Non BlueJ item from the Project menu

    5. In this dialog box, navigate through your folders until you have selected the folder called lab01 - make sure that it looks similar to the next picture. A BlueJ project consists of a folder which may contain one or more source code files, so make sure that you have selected the folder - do not try to select the individual file BankAccount.java

      If you succeed, then the BlueJ window should look like the next picture. Note a Windows security alert window may pop up, just tick the box that says 'For this program, don't show this message again' and then click OK.

      Each source code file contains the source for one class; in this case there is one class called BankAccount because you copied one file BankAccount.java into your lab01 folder.

    6. Compile

      the class BankAccount

      This is achieved by right-clicking on the representation in the class diagram, and choosing "Compile" from the pop-up menu.

    7. Create several BankAccounts

      Objects in Java are always created with the reserved word new. In BlueJ, you right-click on the compiled class, and then choose one of the options that starts with the word new.

      If the constructor requires any arguments, then BlueJ will prompt you for them. In this case, BlueJ asks you for two numbers, more precisely two int, and a String. You should type in the numbers and the String that you wish to store with this account.

      BlueJ also asks you for the name of the object that you have just created. It suggests a "default" name, but you can choose what you want, and you are free to enter any int or String that you like. Then press "OK".

      When the object is created, BlueJ puts it onto the object workbench; you can create more than one BankAccount and they will all be placed onto the object workbench.

      The class diagram shows the classes that have been defined by the programmer. The object workbench shows the objects that have been created by the user (that is, you).

    8. Inspect your objects

      What makes BlueJ unique is the facility for the user to interact with the objects that have been created. In particular, BlueJ allows the user to "look inside" an object and see what values it is currently storing.

      If you right-click on the object and choose "Inspect", then you can see the values inside the object, even the ones that the programmer has declared to be private!

    9. Make some mistakes!!

      Making mistakes is one of the best ways of learning about how computers and computer programs work.

      When you construct an object (by right-clicking on the class) you are prompted for three arguments. What happens if you type in something that is not sensible? Try and predict what will happen if you do the following things, and then do them and see if your prediction was correct.

      • new BankAccount("Bill Gates",-123,999123);

        (not a sensible value for the account number)

      • new BankAccount(Bill Gates,1234,10000);

        (forgot the quotes around the name)

      • new BankAccount('Bill Gates',1234,10000);

        (single quotes instead of double quotes)

      • new BankAccount(1000,1234,10000);

        (a number instead of a String)

      • new BankAccount("Bill Gates",1234.56,10000);

        (a number with a decimal point instead of an integer)

    10. Call the methods of the objects and see what effect they have have

      The methods of an object are the things that it can do. You can check what methods are available by right-clicking on the object. (Remember, you click on the class to create an object, and on the object to call a method.)

      If you right-click on the object called savings (or whatever name you gave your object) and then select the getBalance method, then you are causing the statement savings.getBalance(); to be executed. This syntax means that the target object is savings and the method call is getBalance().

      Try calling the different methods and seeing what happens. Do you always get a value returned? What is the difference between the methods that return a value and the ones that don't?

      Create two objects called savings and cheque, both with initial balances of 0, and do the following method calls, checking the balances of the objects after each call.

      • savings.deposit(1000);
      • cheque.deposit(500);

      • cheque.deposit(1500);

      • cheque.withdraw(200);

      • cheque.getBalance();

      • savings.withdraw(cheque.getBalance());

      Notice that the last task involved two method calls. The method call savings.withdraw(...) expects an int as an argument, but it does not care where that int comes from. One place we can get an int is from the return value of the method call getBalance(). Make sure that you understand what is happening here!




    EtchASketch

    This program allows us to play with a computer simulation of the child's game EtchASketch.

    1. Close the current project by using Project | Close

    2. Copy the files and into the lab01 folder.

    3. Open the lab1 folder again, this time using Project | Open Project.

      You only use Open Non BlueJ the first time that you open a folder, and from that time onwards BlueJ will recognise the folder as a BlueJ project, even if you have added new source files. You can see the different icon that is used for folders that BlueJ has used before, and ones that it does not know about.

      The class diagram shows that this program uses two classes, one called Canvas and one called EtchASketch. The arrow shows that the class EtchASketch uses the services of the class Canvas (just like the class Waiter uses the services of the class Chef in order to produce meals!).

      The BankAccount class is still in this project folder, but we will not be using it any more, so just move it (with the mouse) to one side of the class diagram. The BlueJ picture may place the icons of all the new classes on top of each other, so you may have to move them around to get a clear view of what is happening.

    4. Create an object of the class EtchASketch

      (If you start to have difficulties understanding what to do, then go back and re-read the BankAccount example, or speak to the demonstrator.)

      You will notice that as well as the object appearing on the object workbench, it also causes another window to appear, representing a drawing of an EtchASketch toy.

      (This window may initially appear hidden behind other windows, but you can click on it to bring it to the front.)

    5. Check the methods available

      When you have created an object - in this case, I have called it toy, you can check the methods by right-clicking on the object.

      You will notice that there are 9 methods, but only 5 different names! There is the method called clear, and two each with the names up, down, left and right.

    6. What do the methods do?

      Try one of the methods - say up - by just choosing the line marked void up() from the menu. This causes a short line to be drawn on the "Etch-A-Sketch" screen.

      The up() method simulates the effect of making a small turn on one of the knobs of the Etch-A-Sketch - a small line is drawn, and subsequent drawing occurs from the point where the line finished.

      Now try doing the following

      toy.up();
      toy.up();
      toy.left();
      toy.left();
      toy.down();

      We use the notation toy.up() to mean "Call the up method belonging to the object called toy." If we had more than one Etch-A-Sketch object, then the name would distinguish which object was to be used.

    7. Methods that take Arguments

      The methods up(), down() etc. all cause a line of fixed length to be drawn; this length was chosen by the programmer and cannot be changed by the user.

      However, the "duplicate" methods all permit the user to specify the length of the line to be drawn. Clear the screen on the toy object (by using the method clear()), and try calling the method described as void up(int).

      The presence of the word int in the brackets shows that this method requires the user to specify the value of one argument, and that this value must be of type int - that is, it must be something that is an integer (whole number). When you select this method, BlueJ knows that it requires an argument and so it produces a window where you can specify the value.

      Using this mechanism, try doing

      toy.up(50);
      toy.left(100);
      toy.down(200);

      and see what happens.

    8. Write some "EtchASketch programs"

      We can consider a sequence of up, left, down, right and clear calls to be a program in a very simple programming language.

      Try to write programs to achieve the following pictures.

      • A square of side 100

      • An L-shape

      • Your initial (people called "Xavier" can choose another letter)

      • A modernist logo

    9. Now test your programs using EtchASketch objects

      Did they all work first time? Would you like to be able to rename "sequences" of statements that you repeat often so that you don't have to type them in one by one?

    Programming Style

    In future labs and the project you will be using the Checkstyle plug-in in BlueJ. To get familiar with the system, try the provided You will first need to set up Checkstyle in BlueJ, then the exercise is at the end of the page. Sidebars provide links to documentation for Checkstyle rules. See also the lecture notes for the first Java workshop session.

    Summary

    Words in italics are important and/or technical terms that you should ensure you understand before completing this lab.

    The main focus in this lab was in the process of creating and using objects from classes defined by another programmer. In subsequent labs, we will consider how to define our own classes.

    The key points of this lab are:

    • Each Java source code file contains the definition for one class

    • BlueJ provides a view, called the class diagram of all the source code files (and thus classes) in one folder/directory

    • A class file must be compiled before it can be used

    • Objects can be created from the classes shown in the class diagram
    • BlueJ shows the created objects on the object workbench

    • The methods of an object are determined by the class to which it belongs

    • A program can create several objects of any one class; each of the objects has the same collection of methods

    • A method is called by using the syntax targetObject.methodName(arguments) where the targetObject is the name of the object, methodName is the name of the method, and arguments are values for any arguments that the method requires