Java first Experiment--Caesar password

Source: Internet
Author: User

Course: Java Programming Class: 1352 Name: Albert Wong Study No.: 20135215

Score: Instructor: Lou Jia Peng Experimental Date: 2015.4.15

Experiment level: Preview degree: Experiment time: 19:30~22:30

Instrument Group: Compulsory/Elective: Elective experiment number: 1

Lab name: Familiarity with the Java development environment

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

Ii. contents of the experiment

(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)

5. Realize the function of all prime numbers between the 1-n integers and test them.

Third, the experimental process

Experiment One,

Development of Java programs under 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 20135215 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 20135218 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 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}

Experiment Two,

Programming with Eclipse. Note To save the established class to the package folder. Write the development process. Focus on debugging the program, click Window->Open Perspective->Debug Open Debug View.

Set breakpoints, and in front of where you want to set breakpoints, double-click the section that precedes the number of rows to set breakpoints quickly.

debugging shortcut keys are F11, single-step debugging, enter the function shortcut key is F5, do not enter the function shortcut key F6, general debugging do not enter the function, when the function problem, then enter the function.

To debug at a specified location, use the Resume function of the shortcut key for F8.

You can view the value of a variable at this time by moving the mouse over the variable.

Conditional breakpoints, which are typically used in loop statements. Right-click to the left of the statement line you want to debug, and select Breakpoint Properties...”, Enter the required loop condition.

Package LJP;

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<100;i++)

Sum+=i;

SYSTEM.OUT.PRINTLN (sum);

}

public static int Add (int augend,int addend)

{

int sum=augend+addend;

return sum;

}

}

Experiment Three,

Caesar Password

Package Lala;

Import Java.util.Scanner;

public class Lala

{

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)

{

Lala mj=new Lala ();

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

MJ.MJ ();

}

}

    1. Click the Run button (or use the CTRL+F11 shortcut key) to see the results of the run in the console
    2. Here we practice the debugger, first we open the Debug view by clicking Window->open perspective->debug
    3. The debugger first sets breakpoints and runs in one step . Setting a breakpoint is simple, and then clicking the Debug button (or using the F11 shortcut) to start debugging the Java program, we can see that the program stays 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, The value of I is not 5, and we run the program to line 5th with one-step operation: Step Into( quick F5) and stepover(fast F5), these two single-step functions do not differ when running statements, when executing a function call statement, step into will jump into the function implementation, step over will execute the function directly, In practical use we prefer step over, only the function execution error, indicating that the program problem in the called function, then come back through the step into enter 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.1 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, what 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 the 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 , for example, we pay more attention to sum at this time, and each step can see the sum change, set the conditional breakpoint, We right-click on the left of line 11th and select Breakpoint Properties ...

Steps

Take

Percentage

Demand analysis

20%

Design

30%

Code implementation

30%

Test

10%

Analysis Summary

10%

Java first Experiment--Caesar password

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.