The first job in Java programming: source code calculator,

Source: Internet
Author: User

The first job in Java programming: source code calculator,
Objective: (1) to get started with a language, you need to write at least 20 thousand lines of code. Then, we compile a program for calculating the number of lines of code to motivate ourselves to move forward. (2) understand the role of code reuse and reconstruction.
Target (progressive relationship, difficulty from small to large): Target 1: Given a source code file (. cs ,. java) to output the total number of lines, empty lines, comment lines, and code lines of the file. Objective 2: enter a parameter (specify a directory or file) from the command line and output the specified type file (. cs ,. java), the total number of lines in each file, the number of empty lines, the number of comment lines, and the number of code lines. Objective 3: enter two parameters from the command line (first parameter: Specify directory or file; second parameter: Specify type file ), output the number of files of the specified type in the directory, the total number of lines of each file, the number of empty lines, the number of lines of comments, and the number of lines of code.

Scoring Method (Supplement-new ):
(1) achieve Goal 1. The program can run and get the result. The score is 80;
(2) Goal 2 is completed. The program can run and the result is obtained, with a score of 90;
(3) achieve Goal 3. The program can run and get the result, which is 100 points;
Peer evaluation personnel can move up or down by 10 points according to their own understanding. For example, if the comments are complete and the code is refined, extra points will be added. After multiple targets are completed, only the highest score is calculated as the score of the current job.
 
Methods and steps (for reference, use objective 1 as an example): (1) What is the expected result? Simulate the process in your mind-after a file is specified, you can count the total number of rows and the number of empty rows. (2) Target decomposition: What are the core steps? -- Open a file; read a row of the file; judge the type of the row; judge the end Of the file; result output (output to the screen, or output to the file ). (3) code writing: I have never learned to write code (java, or cs), baidu or google search. -- What to search? How can I ask questions? (C # counts the number of lines in c, c ++, and C # Code) which websites may have more trustworthy content? (N websites that programmers need to know) (4) debugging and testing: Are there any errors in our code? Skills in debugging and testing. -- Test Engineer's work. What if the input file name does not exist? How can I determine if a file of the specified type exists ?...... (5) Summary and improvement: What experiences and shortcomings are there? Write it down and remind yourself. On the other hand, share it with others and help others. For example, what problems have you encountered and how have you solved them? Can I modify the command line to a version with an interface? Is there much code to be rewritten? Can I try it?
(1) achieve Goal 1. The program can run and get the result. The score is 80;

Package iotest; import java. io. bufferedReader; import java. io. file; import java. io. fileNotFoundException; import java. io. fileReader; import java. io. IOException; public class StaticsCodeingLine {private static int whiteLines = 0; private static int commentLines = 0; private static int normalLines = 0; /*** @ param args */public static void main (String [] args) {File f = new File ("C: \ Users \ Administrator \ Wor Kspaces \ MyEclipse 8.5 \ TYH \ src \ iotest \ StaticsCodeingLine. java "); // note the path sumCode (f);} private static void sumCode (File file) {BufferedReader br = null; boolean comment = false; try {br = new BufferedReader (new FileReader (file); String line = ""; try {while (line = br. readLine ())! = Null) {line = line. trim (); if (line. matches ("^ [\ s & [^ \ n] * $") {whiteLines ++;} else if (line. startsWith ("/*")&&! Line. endsWith ("*/") {commentLines ++; comment = true;} else if (true = comment) {commentLines ++; if (line. endsWith ("*/") {comment = false ;}} else if (line. startsWith ("//") {commentLines ++;} else {normalLines ++ ;}} catch (IOException e) {e. printStackTrace () ;}} catch (FileNotFoundException e) {e. printStackTrace ();} finally {if (br! = Null) {try {System. out. println ("null rows:" + whiteLines); System. out. println ("comment row:" + commentLines); System. out. println ("number of lines of code:" + normalLines); br. close (); br = null;} catch (IOException e) {e. printStackTrace ();}}}}}

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.