Java Lab report One: Familiarity with the Java development environment

Source: Internet
Author: User

Experimental requirements:

1. Compiling and running a simple Java program using the JDK

2. Edit, compile, run, and debug Java programs using Eclipse

Experimental content

(i) Java program Development under command line

(ii) Development and commissioning of Java programs under Eclipse

(iii) exercises (implemented via command line and Eclipse two, practice debugging under Eclipse)

1, the implementation of Caesar password, and test.

(a):

  1. First double-click the icon on the desktop Xface终端 , as shown in:

  2. Open the terminal (like cmd under Windows) and then run the shell program automatically, as shown in:

  3. Enter cd Code the command into the code (note C to capitalize) directory as shown in:

  4. Enter the mkdir 20135100 command to set up the experimental directory, note that the directory name must be its own school number (study number to be reflected in the experimental report), you can use ls commands or dir commands to view the established directory situation. As shown in the following:

  5. Enter the cd 20135100 command into the experimental directory, and then enter a similar to create the mkdir exp1 first experiment directory, and then enter cd exp1 into the experiment directory, you can enter the pwd command to view the current working path, as shown in:

  6. Input gedit Hello.java ( Note to save the code in the EXP1 directory ) or vim Hello.java (it is recommended that you take some time to learn vim) edit the Hello.java and enter the following code:

    1 package LJP;2 import Java.util.Scanner; 3 public class Hello{4 public static  void main (string[] args) {5 system. Out.println ( "Input your first name, please:");  6 Scanner s = new Scanner (System. in); 7 String name = S.next (); 8 System. out.println ( "Hello" + name +  "!"); 9} 10}              

Notice 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 to import the related classes with import (you can temporarily understand the features of the Include in C), the import path can view the JDK Help documentation, for example, we used the scanner class, The path is shown in the red box.
7. Enter javac -d . Hello.java command compile code, enter java ljp.Hello command to run
Program. The results are as follows:

Note: When you have a package in your code, you must add the- d parameter when you compile your code with JAVAC, and you must prefix the name of the packet when you run the code.

(b):

1. In Eclipse, click File->New->Class New Java class as shown in:

2, according to the Java Code Specification input package name ljp (lowercase, can be customized), the class name HelloJDB (the first letter of the word capital), and tick the Automatically generate main function option, and finally click the Finish button, as shown in:

3. Enter the following code: ""

      1. 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}

4. Click the Run button (or use the Ctrl+F11 shortcut key) to see the results of the run in the console. As shown in the following:

5, the following we practice debugging programs, first we click Window->Open Perspective->Debug to open the Debug view, as shown in:

6, the debugger first will set breakpoints and single-step operation .

Setting breakpoints is simple, double-clicking on the left.

Debug: Then click the Debug button (or use the F11 shortcut key) to start debugging the Java program, and we can see that the program stays on line 4th, as shown in:

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, the value of I is not 5, we run the program through a single run To line 5th, single-step operation there are two: Step Into(fast F5) and Step Over (shortcut F5), these two single-step operation function in the run statement, no difference, when executing the function call statement, Step Into will jump into the function implementation, Step Over will directly execute the function, the actual use of our first use Step Over , only function execution error, Explains the program problem in the called function, and then comes back Step Into to debug by entering the function. We click Step Over on the icon (or F6 ), the program stops at line 5th, then look at the value of the variable I, you will see I equals 5.

7, single 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 the breakpoint on line 11th and then click the Resume icon (shortcut F8 ), and the program runs to line 11th.

(c): Implement the Caesar password and test it.

Code:

Package LJP;
Import Java.util.Scanner;

public class Kaisa
{
void MJ ()

{

Scanner in = new Scanner (system.in);

System.out.print ("Select operation (1. Encrypt 2. Decrypt):");

int N=in.nextint ();

if (n = = 1)

{

System.out.print ("Please enter the string to be encrypted:");

String str = In.next ();

String jm= "";

int key = 3;//Caesar password encryption, backward shift 3-bit

for (int i = 0;i < Str.length (); i++)

{

char C = Str.charat (i);

if (c >= ' a ' &&c <= ' z ')

{

if (c>= ' x ' &&c<= ' z ')

{

c-=26;

C+=key;

}

Else

{

C+=key;

}

}

else if (c >= ' A ' &&c <= ' Z ')

{

if (c>= ' X ' &&c<= ' Z ')

{

c-=26;

C+=key;

}

Else

{

C+=key;

}

}

JM + = C;

}

System.out.print ("The encrypted string is:" +JM);

System.out.print ("\ n Enter any build to continue, 0 End Program:");

N=in.nextint ();

if (n==0)

{

System.out.print ("Thanks for using this program, Welcome to use again!") ");

}

Else

{

THIS.MJ ();

}

}

else if (n = = 2)

{

System.out.print ("Please enter the string to be decrypted:");

String str = In.next ();

String jm= "";

int key = -3;//Caesar password decryption, forward shift 3 bit

for (int i = 0;i < Str.length (); i++)

{

char C = Str.charat (i);

if (c >= ' a ' &&c <= ' z ')

{

if (c>= ' a ' &&c<= ' C ')

{

c+=26;

C+=key;

}

Else

{

C+=key;

}

}

else if (c >= ' A ' &&c <= ' Z ')

{

if (c>= ' A ' &&c<= ' C ')

{

c+=26;

C+=key;

}

Else

{

C+=key;

}

}

JM + = C;

}

System.out.println ("Decrypted string:" +JM);

System.out.print ("\ n Enter any build to continue, 0 End Program:");

N=in.nextint ();

if (n==0)

{

System.out.print ("Thanks for using this program, Welcome to use again!") ");

}

Else

{

THIS.MJ ();

}

}

Else

{

System.out.print ("Please enter 1 or 2, the other characters are invalid!") \ n Enter any build to continue, 0 End Program: ");

N=in.nextint ();

if (n==0)

{

System.out.print ("Thanks for using this program, Welcome to use again!") ");

}

Else

{

THIS.MJ ();

}

}

}

public static void Main (string[] args)

{

Kaisa mj=new Kaisa ();

SYSTEM.OUT.PRINTLN ("****** Welcome to use Caesar password ******");

MJ.MJ ();

}

}

Operation Result:

The problems and solutions encountered in the experiment.


During the operation of the experiment two, when the breakpoint is set, the teacher's operation is not understood. So the internet Baidu after the discovery, just need to simple in the line of the desired breakpoint on the left double-click on the breakpoint can be set.

During the compilation of experiment three, many problems are encountered.

First, forget the principle of Caesar's password. Baidu after the internet to understand: it is a replacement of encryption technology, the clear text of all the letters in the alphabet on the back (or forward) by a fixed number of offsets after being replaced redact. For example, when the offset is 3, all the letters A will be replaced with D,b to E, and so on.

Secondly, the first time to write the code independently, found that the need for the knowledge of insufficient reserves. On the one side of the book, while the Internet search function method, the final compilation success.

Experimental Harvest

Through this experiment, I learned how to write Java files in the command line, learned to debug programs in Eclipse, and most importantly, had a deeper understanding of programming in the process of experimenting and solving problems. In the process of the third experiment, we will look at the content of video learning, apply it, implement it by the way of sub-parent class, and separate the different functions. Or a real hands-on operation to learn knowledge.

Java Lab report One: Familiarity with the Java development environment

Related Article

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.