First, the contents of the experiment
1. Compiling and running a simple Java program using the JDK
2. Edit, compile, run, and debug Java programs using Eclipse
Requirements:
Complete the experiment and write the experiment report
Statistics PSP Time
Second, the experimental process
Compile and run a simple Java program using the JDK
1. After entering the lab environment, open the Xfce terminal. It acts like a cmd command line to open and execute certain programs.
2. Enter CD Code->mkdir 20135216->cd 20135216->mkdir expel1->cd expel1->gedit Hello.java A series of steps in the directory "20135216\ Expel1\ "To create a Hello Java file. At this point, it pops up.
Figure 1 Command-line environment
3. Enter the following code in the Gedit
Package LWR;
Import Java.util.Scanner;
public class hello{
public static void Main (string[] args) {
System.out.println ("Input your first name, please:");
Scanner s = new Scanner (system.in);
String name = S.next ();
System.out.println ("Hello" + name + "!");
}
}
4. Debug program
Initially, the program cannot run.
Figure 2 Command-line error
After consulting the classmate, found the input execution statement in-D. Hello.java error, should be-D. Hello.java. After the modification, the program runs normally.
Figure 3 Command-line run results
5. Summary
1) The first line of code means to create a package (named LWR), which affects how the code is compiled with Javac . The second line of code, meaning to use the classes in the Java class Library, import the relevant classes with imports;
2) mkdir: Create a new Folder/wenjian
Dir, pwd: Lists the files in the current File/folder
3) When you have a package in your code, you must add the-D parameter when you use Javac to compile the code, and you must prefix the code with the name
Use Eclipse to edit, compile, run, and debug Java programs
- Enter the eclipse command on the command line that is already open and return to open eclipse.
- In Eclipse, click File->new-> Java Project to build a project called Hello, and in accordance with File->new->class, create a package name LWR, class name Hello, and tick the Automatically generate main function option, and then click the Finish button.
- Enter the following code
Package LWR;
public class Hellojdb {
public static void Main (string[] args) {
int i = 5;
int j = 6;
int sum = Add (i, j);
SYSTEM.OUT.PRINTLN (sum);
sum = 0;
for (i=0; i<; i++)
{
sum + = i;
}
SYSTEM.OUT.PRINTLN (sum);
}
public static int Add (int augend, int addend) {
int sum = augend + addend;
return sum;
}
}
Using the Run button, you can see the results of the run below
Figure 4 Eclipse Run results
- Debugging
Set breakpoints and single-step operations according to instructions
Figure 5 Eclipse Debugging 1
Figure 6 Eclipse Debugging 2
Figure 7 Eclipse Debugging 3
- Summarize
1) Click the Debug button (or use the F11 shortcut key) to start debugging the Java program.
2) Place the mouse over the variable name to see the value of the variable.
3) There are two types of steps: Step Into (Fast F5) and step Over (fast F5), which are no different when running the statement, when executing a function call statement, step into will jump into the function implementation, step over will execute the function directly, We prefer to use step over in practical use.
4) package name must be lowercase, class name First Capital letter
5) Set the conditional breakpoint: First right click on the left, select Breakpoint Properties ... As in this program, you can set the value of I. After setting, you can view the value of I.
Exercise: Implement arithmetic and test, try the command line and Eclipse two ways to implement
1. Command line implementation
Import java.util.*;//is similar to a library of functions in C that calls the scanner function
public class Scannerdemo
{
public static void Main (string[] args)
{
Give tips to input numbers
SYSTEM.OUT.PRINTLN ("Please input the first number:");
Scanner S1 = new Scanner (system.in);
Double i1 = s1.nextdouble ();
SYSTEM.OUT.PRINTLN ("Please input the second number:");
Scanner s2 = new Scanner (system.in);
Double i2 = s2.nextdouble ();
SYSTEM.OUT.PRINTLN ("Please input op");
Scanner s3 = new Scanner (system.in);
String op = s3.next ();
System.out.println ("Op as bellow:");
Char z = op.charat (0);
if (z = = ' + ')//The input symbol is judged. Returns "Wrong op" if it is not one of the +-*/
{
System.out.println (I1+I2);
}
else if (z = = '-')
{
System.out.println (I1-I2);
}
else if (z = = ' * ')
{
System.out.println (I1*I2);
}
else if (z = = '/')
{
System.out.println (I1/I2);
}
Else
{
System.out.println ("wrong op!");
}
}
}
Debug program
Figure 8 Command-line debugging errors
Because when I started looking for a network encyclopedia, I used
Char z = op.charat (0);
if (z.equal ('-'))
program, resulting in a compilation error. After using a simple
Char z = op.charat (0);
if (z = = '-'), compile and run without error
Figure 9 Command-line debug results
2.eclipse implementations
Package LWR;
import Java.util.Scanner;
Public class Caculation
{
Public Static void Main (string[] args)
{
double i1,i2;
System. out. println ("Please input the first number:");
Scanner S1 = new Scanner (System. in);
I1 = S1.nextdouble ();
System. out. println ("Please input the second number:");
Scanner s2 = new Scanner (System. in);
I2 = S2.nextdouble ();
System. out. println ("Please input op");
Scanner s3 = new Scanner (System. in);
String op = s3.next ();
System. out. println ("Op as bellow:");
char z = op.charat (0);
if (z = = ' + ')
{
System. out. println (I1+I2);
}
Else if (z = = '-')
{
System. out. println (I1-I2);
}
Else if (z = = ' * ')
{
System. out. println (I1*I2);
}
Else if (z = = '/')
{
if (I2 = = 0.000000)
System. out. println ("I2 cannot is 0!");
Else
System. out. println (I1/I2);
}
Else
{
System. out. println ("wrong op!");
}
System.out.println (I1);
}
}
Debug program
Figure Ten Eclipse errors
Cause: The class name is inconsistent with the file name
Diagram one Eclipse debug success
Figure 1 Eclipse Debug Results
Figure 2 Eclipse Debug Results
Third, the experimental problem
See the experimental process recorded:
- Incorrect input format when practicing command lines
- Debugging steps when you practice eclipse is unclear
- Arithmetic blindly reference network data when writing program, do not match with existing program
Iv. Harvest of Experiments
This experiment is familiar with the Java development environment of the exercise, for future development has a role. In the experiment, we should not only pursue the correct steps, but also pursue the understanding of the steps, deepen the mastery of knowledge from practice, and be able to use knowledge flexibly.
Experiment a Java development environment familiarity-LIU Weihan