Java程序辅导

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

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

Computer Science Courses

Related sites

Introductory Programming In Java

Lab 1

Lab 1

Getting started with the computing environment:
Your first Java program

Objectives

This lab has two objectives:

  1. Introduce you to the computing environment — Ubuntu Linux,
  2. and start writing your first Java programs.

Logging in, taking a look around

Find somewhere to sit in front of a computer screen!

Hopefully the computer will be switched on, unlocked, and … working! (Wave to a tutor if not.)

Now log on into your computer account — use your ANU UniID and password as the user name and the password.

Ubuntu Linux is a version of the open-source Linux (a version of Unix) operating system. For first time users, it may look a little clunky and old fashioned in comparison with Windows 10 or Max OSX. We like using Linux for teaching in the School of Computer Science for a number of reasons. One of these is that (arguably) Linux exposes students to how computers work in a more seamless way than the other operating systems. If you would like to find out more about Ubuntu Linux then you can refer to the : This is very detailed, up to date and provides demos and videos. As a part of this lab, you might want to explore this documentation web site, particularly the

When you log in you will see a desktop. There should be a terminal icon there that you can then left-click on to open a command-line window;if the front panel doesn't contain the terminal icon, then click on the Menu icon on the left, and choose

System → Terminal.

The command line window allows you to do all sorts of things to your computer (within your permissions). It is not the only way of using the labs, but we will teach you how to use it for a start. You can find a (very) small subset of useful command-line commands (also called system utilities) in the .

Using the command line you will soon be able to do things like:

Playing with Atom Editor

In order to create your first Java program you need to start up a text editor. We recommend that you launch the Atom text editor:

Try using the short-cut tricks; their list can be found in Atom's menu:

	Edit -> Preferences -> Packages -> language-java -> Settings
in the section Snippets.

Use them (or just by plain typing) to write or modify a text of a program like HelloWorld.java, then save it on the disk (choosethe directory well, see ), compile and run the program.

This can be done on the command line (you need to change into the directory where the program files are saved).

  1. When you launch Terminal, the command-line interface will be in your home directory. If you created the directory structure as advised , and you saved your new program in comp6700/labs/lab1, you need to change into that directory:
    cd comp6700/labs/lab1
  2. then by listing the current working directory:
    ls
    you will see the output containing a name (or more) of your file MyProgram.java
  3. which can compile and run using the commands javac and java as described above.

Play with the command line and Atom for a while until you feel that you canget around with these tools. Atom's use for programming and its development is documented in the which may find useful.

If you prefer something less basic, and you know how to useit, here we can offer you an IDE of your taste, IntelliJ IDEA, or Eclipse, or Netbeans. An IDE (Integrated Development Environment) is a powerful developer tool which allows to perform all (integrated!) activities which are required inthe process of software creation. Launch them using the front panel (as described above), or by typing either of these commands on the:

in the command line and hitting the Return button(the ampersand symbol & at the end is important:try to start the applications without typing it and see what happens).

If you choose an IDE, prepare for a steep learning curve: we willprovide you with some moderate assistance if necessary, but most of the work to master it will be your own.

Editors (Atom, gedit and any other found on our lab computers), the command-line interface (Terminal), and the three IDEs mentioned earlier — all these tools will be available for your during mid-semester and final examinations.

Exercise One

Download the free book by Allen B. Downey, and do the Exercise 1.7 at the end of Chapter One, p.11-12: you have to type in (really, type it in, do not paste and copy!) the "Hello World" program (like it's presented in the lectures or in the Downey's book, p. 7). Compile (javac Hello.java) and run (java Hello) it. Then follow the advice of breaking the initial code (in Ex 1.3, ai), to make small changes, and try to compile the program again. Read and think through the meaning of the compile messages.

Exercise Two

Write a program that takes two integers and prints out their sum, difference, product and average.

Change the values of these integers by editing your program. Then make the program read the values of integers from a user as two command-line arguments (like we did in the lectures). The programexecution on the command-line will, therefore, looks like this:

% java MyProgram 3 58-2154
You should remember, that the command-line argument's type is String,and before they can be used as integers (which is the intended goal here), thesestrings must be converted to int types (the int typeis one of the primitive types used to represent integer numbers). By peaking(only so slightly) ahead of the course material, use the following"transformation" trick (which is the standard way to convert a valueof string type to the integer it is meant to represent; see more detailsin Hortsmann's book Ch. 1.5.4, or the in Java Tutorial):
int x = Integer.parseInt(args[0]);int y = Integer.parseInt(args[1]);

BTW, can you (do research if necessary) modify this program to make it printa well-formatted output, similar to this one?

% java MyProgram 10 2    x + y =  12    x - y =   8    x * y =  20(x + y)/2 =   5
(Hint: find out how the method printf() can be used; this method isa useful alternative to the more simpler ones print() andprintln()).

Exercise Three

Note You will make your life simpler if you reuse a previous program todo the next task which may be similar (like in the Exercise Two). Copy an existingsource file (on the Terminal this is done by the copy command:

> cp OldClass.java NewClass.java

and appropriate editing of the NewClass.java.)

It make a lot of sense to organise your home directory for theCOMP6700 (or COMP2140) studies. Given the content of our activities (checkthe labs and deliverables in the ),you can create a dedicated directory comp6700 (or, comp2140) in your Home Directory, with the followingstructure:

               comp6700             /    |    \            /     |     \           /      |      \  assignments     hw      labs   /      \       |       /  \ ass1     ass2   ...    lab1  ...

This can be done by merely using the window manager (on Linux, this application is called Files; launch it the same way you've launchedTerminal or Atom), or alternatively (andmore in the spirit of programming) by using the command-line tools likemkdir (again, refer to the guide, and/or ask your tutor to demonstrate).

Lab 1

 

Updated:  Sun 12 Jun 2016 17:27:37 AEST Responsible Officer:   JavaScript must be enabled to display this email address. Page Contact:  

+61 2 6125 5111
The Australian National University, Canberra
CRICOS Provider : 00120C       ABN : 52 234 063 906