`
guoyiqi
  • 浏览: 968095 次
社区版块
存档分类
最新评论

java试题

 
阅读更多
You should be comfortable with the content in the modules up to and including the module "Methods" for this project.

A reporter needs a security application that will encrypt their phone numbers in their contact list to protect their sources. The client program will provide a three digit encoding number that will be used to encode the separate parts of a phone number: area code, exchange and subscriber number.  The first digit will be used to encode the 3 digit area code.  The second digit will be used to encode the 3 digit exchange.  The third digit will be used to encode the 4 digit subscriber number. 

Use a simple encryption algorithm to make this easier. The Caesar cipher can be represented as C = (P + key) mod 10, where C is the encrypted value and P is the value being encrypted. The key is the encoding key derived from the security code that is provided by the client.

The corresponding deciphering formula to return the encoded phone number to its original state is:

P = (C - key) mod 10.

In Java, this might look like this (assuming variables previously defined as integers for p, c and key):

p = (c - key) % 10;

if(p<0)
  p= p + 10;

The if condition is necessary in Java because of the way the modulus operator (%) handles negative numbers.  When (C-key) produces a negative number, the Java modulus operator returns that negative number.  To counteract that, add 10 to the result. 

For more information and variations on this cipher, check out modulo encryption.(optional)

In Java, the % is the modulus operator, so you could use it in combination with the addition or subtraction operator for the variables, like this:

C =(P + key) % 10  

P =(C - key) % 10  //use an if condition next to test for a negative result

if(P < 0)

  P= P + 10;

Use this cipher for each individual digit in the phone number.  For example, the client provides this security code: 135.  The security code is broken down to three encoding keys: 1 (for the area code) 3 (for the exchange) 5 (for the subscriber number).  We will use an example phone number, such as 707-123-4567. 

?The area code will be encoded using the Caesar cipher as follows, using the first digit in the security code (1) as the encoding key on each of the numbers in the area code: 7, 0, 7: 
?C = (P + code) mod 10. 
?8 = (7 + 1) % 10
?1 = (0 + 1) % 10
?8 = (7 + 1) % 10
?The exchange will be encoded using the Caesar cipher as follows, using the second digit in the security code (3) as the encoding key on each of the numbers in the exchange:1, 2, 3: 
?C = (P + code) mod 10. 
?4 = (1 + 3) % 10
?5 = (2 + 3) % 10
?6 = (3 + 3) % 10
?The subscriber number will be encoded using the Caesar cipher as follows, using the third digit in the security code (5) as the encoding key on each of the numbers in the subscriber number: 4, 5, 6, 7: 
?C = (P + code) mod 10. 
?9 = (4 + 5) % 10
?0 = (5 + 5) % 10
?1 = (6 + 5) % 10
?2 = (7 + 5) % 10
The resulting phone number is therefore 818-456-9012.

Minimum Requirements:
?Create a separate class that contains the following methods (5 points) Note: This class should be separate and apart from the public class that holds the main method.  You can either make a class that is not a public class or you could put it in a separate .java file: 
?A method that will accept a parameter as an integer array for the phone number and a parameter for the security code (you choose the data type for the security code).  Use one (or more) repetition structures to replace each number in the array with its encrypted version using C = (P + key) mod 10 with the appropriate digit in the security code as described above. Note: You should make the replacement in the original array. (5 points)
?Create a method that will accept an encrypted phone number as an integer array and the decoding key.  Use one (or more) repetition structures to return it to its plain text (decrypted) version using P = (C - key) mod 10 and the conditional:  
?if(P < 0)     P= P + 10;
?Note: You should make the replacement in the original array. (5 points)
?Call the methods of this class in the main method with at least one sample phone number.  Display the phone number in main before and after calling each method (5 points).
Important Note: You can get the phone number from the user at the console or with a GUI if you prefer to do that, but it is not required.  You can put the phone number in the code when you create the array instead if you would rather do it that way.  Regardless of how you get the phone number, it must be put in an array and the array must be passed to another method that accepts an array as a parameter.  You should not get the phone number and process it in the same method
You should be comfortable with the content in the modules up to and including the module "Input Output" for this project.

For this project, download the text file weblog.txt 
Note: To download this file, right click on the link and select SAVE AS.

This file is an Apache web log taken from the web server for St. Mary's University. When a visitor goes to their web site, the visitor's browser makes a request to the web server to view a web page, which is a file residing on the server. Each time a page is requested by a browser, the web server records information about that request. This weblog.txt holds the details of some of those requests. See below for a list of fields with an example:

Web Log Example 

This file does not include all possible information that could be collected by a web server. The full description of the apache log file format can be found here: http://httpd.apache.org/docs/2.2/logs.html

For this project, you can use each line of the web log file as one string. An array of size 2990 will hold all of the lines in this file.

Minimum Requirements:

?Create a separate class that contains the following: (4 points). Note: This class should be separate and apart from the public class that holds the main method.  You can either make a class that is not a public class in the same file or you could make it public and put it in a separate .java file: 
?Create a method to read each line of the web log file. (4 points)
?Each line should then be stored in a private array created in the class, such that the first element of the array is the first line of the web log file. Because each element will hold an entire line from the file, the array should be declared as an array of strings (4 points).
?Create one more method to use the Arrays class sort method to sort the contents of the array and write the contents of the array to a file. The contents of the new file should look like the original input file once your program completes, but in sorted order (4 points).
?In the primary class for your package, create a main method that calls all of your methods (4 points).
You should be comfortable with the content in the modules up to and including the module "Arrays" for this project.

Create a class called consultCo that holds a private class called employee that contains the name, pay rate and social security number of an employee of a consulting firm called consultCo. The consultCo class should also have an array that holds all of the objects of all of the employees in the array.  This will be an array of employee objects.

Here is a logical diagram of this array of objects:

 



You will also need methods to satisfy the following requirements:

Minimum Requirements:

?An add method to hire a new employee.  This method should create a new object with the employee information (name, payrate and social security number) and then it should add that new object to the array.  You are not required to update an employee s data if the employee exists. (5 points)
? A report method that will display the name, pay rate and social security number of all employees. (5 points).
?A raise method that will give all employees a 10% raise. (5 points).
?You will also need a main method that creates calls the method(s) you created to meet the requirements of this project (5 points).
Notes: 

You will need at a minimum two (2) classes for this project: the public class that contains the main method and a class that is a private class that is a member of another class.  If you would like to include more than 2 classes, that is fine but not required.

The Module Lab Activities "Lookup!" and/or "Lookup! With GUI" (a GUI is great but not required) can be extremely helpful for this project.  Just by following the lab activity (and the video demos if needed), you can make significant progress toward completing this assignment.
 
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics