Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Java Crash Course
Lab Exercises : Java Language Basics
Unix/Linux Basics
If you haven’t used Unix or Linux before, this section lists some basic commands to help you use the console.
Command Usage
mkdir name Makes a new directory (folder) with the give name.
Allows you organize your files, and keep related work together.
cd name Changes to the specified directory.
Use without specifying a name to go straight to your home directory.
cp source dest Copy the file source to the file dest.
rm name Remove the specified file. There is no undelete command, so use with care!
xemacs name Open the specified file with the xemacs text editor.
gvim name Open the specified file with the gvim text editor - NOT recommended unless
you have used Vi, Vim or gVim before!
Introduction to the Java language
Hello World
A simple exercise to get you familiar with compiling and running Java programs
1. Create a file named HelloWorld.java - note that Unix/Linux is case sensitive
2. Copy the Hello World program from your notes (slide 3)
3. Compile the program by typing javac HelloWorld.java in a console
4. Run the program by typing java HelloWorld in a console
Summing
1. Create a file named Summing.java
2. Create a class named Summing with a main method - use the HelloWorld class as your base
3. In the main method, write a for loop that
(a) loops from 1 to 20
(b) prints out the current number, and a running total
The first 4 lines of output will be:
1
1 1
2 3
3 6
4 10
Even Odd
1. Create a new file EvenOdd.java and class EvenOdd based on Summing
2. As well as printing the running totals, print out if the total is odd or not
The first 4 lines of output will be:
1 1 odd
2 3 odd
3 6 even
4 10 even
Switch
1. Create a new file Switch.java and class Switch based on EvenOdd
2. Using a switch statement, change the output of the first number to be 1st instead of 1, 2nd instead
of 2, etc.
3. Change the loop range to be 1 to 30 instead of 1 to 10
The first 4 lines of output will be:
1st 1 odd
2nd 3 odd
3rd 6 even
4th 10 even
Hello
1. Create a new file Hello.java and class Hello based on the Java Program Template (slides 24-26)
2. Add a method void printHello(String name) that prints “Hello ” followed by the parameter name
3. Loop through the args array parameter in go, calling printHello for each element in the array
Your program should be run as “java Hello FirstName SecondName ThirdName...”. For example, running
java Hello Richard would output Hello Richard.
Longest
1. Create a new file Longest.java and class Longest based on the Java Program Template
2. Add a method String findLongest(String[] array) that returns the longest String in array
3. The go method should print out the longest String in args array
Running java Longest which is the biggest word should output biggest.
2
Average Length
1. Create a new file Average.java and class Average based on the Java Program Template
2. The program should:
(a) Calculate the average String length of its’ command line arguments (as a floating point value,
not an integer!)
(b) Print out all of the Strings which are longer than the average length
Running java Average the average length is about five letters should print out
average
length
about
letters
Note: If we had used an integer value to store the average length, “about” would not have been longer than
average and so would not have been printed!
3