Java程序辅导

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

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

The aims of this labsheet are to:

  1. familiarise yourself with the MacOSX environment, including both the GUI environment and the underlying Unix operating system,

  2. familiarise yourself with a development environment that you will use to write Java code, and

  3. refresh your memory of Java by completing a couple of simple exercises.

Exercises

  1. Getting Started with MacOSX

    As stated in the lectures, the only supported environment for this unit is the MacOSX operating system. To ensure you are familiar with this environment, work through the reference tutorial: . The rest of the unit will assume you are familiar with the concepts introduced in this tutorial.

  2. Programming Conventions

    When you leave University and work for a company as a programmer, you will be required to follow the programming conventions specified by that company. This provides the company with uniformity across different coders and makes their systems easier to maintain. Similarly, all code submitted for assessment in this unit must comply with the unit's . Code that does not follow the conventions will lose marks. If you have not already read these conventions, you should do so now.

  3. Choose a Development Environment

    How you choose to develop your Java programs is up to you. The choice of development environment (or IDE) can be a personal one, with some people adamant one approach is superior to others. In this unit you can complete your work in whcih ever development environment you choose. However, you are required to be familiar with two prescribed development environments:

    • The Eclipse IDE
    • The Unix command line interface

    The setup in CSSE Lab 2.01 provides a number of options, including Eclipse (a free, cross-platform IDE), Xcode and the Unix command-line using standard Unix editors, among others. Each has its advantages and disadvantages, so take the time to explore each option and choose which is best for you. Some people prefer the "prettiness" of a GUI IDE; others prefer the simplicity of the command-line. In general, a GUI IDE is much better for large projects with many dependencies, whilst the command line environment is better for quick prototyping and scripting.

  4. Weiss, Exercise 1.19

    Write a program to print out all pairs of positive integers (a,b), such that a < b < 1000 and (a2+b2+1)/(ab) is an integer.

    Recall, your program must contain the method:

    public static void main(String[] args)

    in order for the Java JVM to know what method to first call when your program is executed.

    Write your method using the command line first. You should write the file using an editor such as Emacs, Vim or Nano. Emacs should be available in the Applications folder, and you can run vim or nano just by typing vim or nano at the command line.

    You can compile your code from the Unix command-line (terminal) using the javac compiler:

    csse2100% javac Q4.java

    and execute it with the java interpreter:

    csse2100% java Q4

    Remember, the name of the file must match the name of the (principal) class defined within the file. So, in the example above, the file Q4.java must contain a class called Q4. When executed in this manner, output produced by your program will appear in the terminal window.

    Next, try to compile and run the code in Eclipse. Open Eclipse in the Applications Folder. You may be asked to nominate a workspace, in which case specify a directory where your work will be saved. Then select new Java class from the top menu. You will be given a number of options, but make sure that the package is left as default. This will generate some source code for you, and you can then complete the class. To run, press the green button with the arrow on it.

  5. Generalise your solution to question 4 by allowing the user to enter a positive integer, n at the command line, and then printing out all pairs of positive integers (a,b), such that a < b < n and (a2+b2+1)/(ab) is an integer.

    The list of words the user that types after java Q4 is passed to the main method as an array of Strings, so

    csse2100% java Q4 10 100 1000

    will call the method Q4.main({"10","100","1000"}). We can turn the first argument from a String to an integer by using the command Integer.parseInt(args[0]).

    You can make the similar modification in Eclipse. To add arguments to the command line, you need to change the action associated with the green arrow. Right click (or control-leftclick) the class name and select Run As and then Run Configurations. This will allow you to specify command line arguments.

  6. What happens if the user enters something that is not a number? We would like top throw an IllegalValue exception that is defined in the CITS2200 package. To do this you will have to

    1. Import the CITS2200 package into your file. You can download the to your working directory (or whereever you like). Add the line import CITS2200.IllegalValue; to the top of your class.
    2. Catch the NumberFormatException and throw an IllegalValue exception. Check the first workshop for instructions on how you might do this.
    3. Make sure that the CITS2200.jar file is accessible in your IDE. This is different for each IDE, so you should try to get it to work in each IDE. At the command line you just need to specify the classpath when compiling and running with the flag javac -cp [path-to-jar]CITS2200.jar:. YourClass.java

      In Eclipse, you need to add the jar. Write click the class and select Configure Build Path. Then add a user defined library. You can then add the jar file to the new library.


The University of Western Australia

School of Computer Science and Software Engineering

University information

This Page