Java Lab Report One

Source: Internet
Author: User

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

Second, the experimental steps

Development of Java program under "experiment One" command line

    1. First, double-click the Xface terminal icon on your desktop.
    2. Open the terminal (like cmd under Windows) and then run the shell program automatically.
    3. Enter the CD Code command to enter the code directory.
    4. Enter the mkdir 20135308 command to set up the experimental directory, you can use the LS command or the dir command to view the established directory situation.
    5. Enter the CD 20135308 command into the experiment directory, enter mkdir EXP1 to create the first experiment directory, and then enter the CD EXP1 into the experiment directory, you can enter the PWD command to view the current working path.
    6. Enter Gedit Hello.java, edit Hello.java, and enter the following code:

1.1 package LJP;

2.2 Import Java.util.Scanner;

3.3 public class hello{

4.4 public static void main (string[] args) {

5.5 System.out.println ("Input your first name, please:");

6.6 Scanner s = new Scanner (system.in);

7.7 String name = S.next ();

8.8 System.out.println ("Hello" + name + "!");

9.9}

10

7. Note the first line of the code, which affects how we compile the code using JAVAC.

Note the second line of code, when we use the class in the Java class Library, we import the related class with import (which can be temporarily understood as the feature of include in C)

8. Enter javac-d. Hello.java command to compile the code, enter the Java LJP. The Hello command runs the program. The results are as follows:

Java program Development and debugging under "Experiment Two" eclipse

    1. Enter ECLIPSE commands on the command line and enter or click the Eclipse EE icon on your desktop to open eclipse.
    2. In Eclipse, click File->new-> Java Project to create new Java projects.
    3. Enter the project name Hellolc and click the Finish button to complete the new project.
    4. In Eclipse, click File->new->class to create a new Java class.
    5. According to the Java Code specification, enter the package name LC, the class name Hellolc, and tick the Automatically generate main function option, and then click the Finish button.
    6. Enter the following code:


1 package LJP;
2 public class Hellojdb {
3 public static void main (string[] args) {
4 int i = 5;
5 int j = 6;
6 int sum = Add (i, j);
7 System.out.println (sum);
8
9 sum = 0;
Ten for (i=0; i<; i++)
sum + = i;
12
SYSTEM.OUT.PRINTLN (sum);
14}
15
public static int Add (int augend, int addend) {
sum int = augend + addend;
return sum;
19}
20}

7. Click the Run button (or use the CTRL+F11 shortcut key) to see the results of the run in the console.

8. Practice the debugger below and first open the Debug view by clicking Window->open perspective->debug.

9. The debugger first sets breakpoints and one-step operation. setting breakpoints is easier. Then click the Debug button (or use the F11 shortcut key) to start debugging the Java program, we can leave the program on line 4th. Note that at this point the 4th line of code is not executed, we can put the mouse on the variable name to see the value of the variable, at this time I value is not 5, we run through a single step to the program to the 5th line, one-step operation there are two: Step Into (Fast F5) and step Over (fast F5), There is no difference between the two steps in running the statement, when executing a function call statement, step into will jump into the function implementation, step over will directly execute the function, in practice we prefer to use Step over, only the function execution error, indicating that the program problem in the called function, Then come back through the Stepinto into the function to debug. We click the Step over icon (or F6) and the program stops at line 5th, when I see the value of the variable i, I see I equals 5.

10. Step execution efficiency is slow, if we feel that the 6th line to the 10th line of code is no problem, want to let the program run directly to line 11th, how to do? First we set a breakpoint on line 11th and then click the Resume icon (shortcut F8), and the program runs to line 11th.

11. One-Step execution efficiency is slow also leads to another problem, such as a cycle in the middle of a problem? You can then resolve the issue through conditional breakpoints. When debugging a loop code, there is a value that we pay particular attention to when we look at the value of the variable in the variable tag, such as when we are paying attention to sum at this time, we can see how the sum changes with each step of the execution.

To set a conditional breakpoint, we click the left mouse button on line 11th and select Breakpoint Properties ....

12. We set the condition "I==50", click F8, we can see that I is 50.

"Experiment three" exercises

Import Java.util.Scanner;

public class Fibonacci {

public static Voidmain (string[] args) {

Scanner Scanner = Newscanner (system.in);

System.out.println ("Pleaseinput this Fibonacci N:");

int n = scanner.nextint ();

int sum = 0;

for (int i = 1; I <=n; i++) {

Sum +=fibonacci (i);

}

System.out.println ("Thesum is:" +sum);

}

Recursive implementation method

public static Intfibonacci (int n) {

if (n <= 2) {

return 1;

}else{

Returnfibonacci (n-1) + Fibonacci (n-2);

}

}

Recursive implementation method

public static intfibonaccinormal (int n) {

if (n<=2) {

return 1;

}

int n1 = 1, N2 = 1, sn= 0;

for (int i = 0; I <n-2;i++) {

sn = n1 + n2;

N1 = n2;

N2 = SN;

System.out.println (Fibonacci ())

}

return SN;

}

}

Iii. Summary

This is my first time on the network to complete the experiment, feel very fresh, see Lou Teacher spent two days for our carefully written experiment guidance very moved, also inspired, the teacher for us so dedicated, I will certainly learn java.

This experiment is still not too difficult, I have been following the guidance of the teacher operation, see the key points highlighted difficulties, I will remember in the heart, feeling learned a lot of new knowledge. But I also encountered some problems in the experiment.

    1. In the experiment one 1, I entered the program after the terminal can not find the file address, will not be able to run the Hello World program, then I turned off all the pages, reopen the terminal, without opening the editing software, enter the path just saved the file can run the program, which I still do not understand what the reason.
    2. In experiment two, I used Eclipse to write the program for the first time, I didn't know how to use this software, I feel that the software is really easy to use. Setting a conditional breakpoint in cycle is successful when you enter I=50, and then the i=50 in the yellow box on the right side disappears when you refresh the interface.
    3. Experiment three is according to the number of subjects selected, I followed the number 5 principle of the first choice of the third experiment. In this program, it requires the user to enter an integer variable, so I used the first few lessons to practice the application of scanner to achieve keyboard input. This program, I modeled the previous use of C language to write the Fibonacci sequence of ideas, in the implementation of the first two and assigned to the next number of query the next book related code.

The experiment because of the speed problem, I almost every minute because of disconnection to re-refresh, wasted a lot of experimental time, but feel in this experiment, even though most of the basic content of the operation, but the teacher warmly reminded the focus is really I did not notice the place, lay a good foundation, Proficiency is a cornerstone of writing Java programs later.

Java Lab Report One

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.