Java programming for the first time: source code Calculator

Source: Internet
Author: User

Objective:(1) to get started with a language, you need to write at least 20,000 lines of code. So, let's write a program that calculates the number of lines of code, motivating ourselves to move forward. (2) understand the role of code reuse and refactoring.
goal (Progressive relationship, difficulty from small to large):Target 1: Given a source code file (. cs,. java), output the total number of rows, empty lines, number of comments, lines of code in the file. Target 2: Enter a parameter from the command line (specify a directory or file), output the number of specified type files (. cs,. java) in the directory, the total number of rows per file, the number of empty lines, the number of comment lines, and the number of lines of code. Target 3: Enter two parameters from the command line (the first parameter: Specify a directory or file; the second parameter: Specify a type file), output the number of files of the specified type in the directory, the total number of rows per file, the number of empty lines, the number of comment lines, and the number of lines of code.

Scoring Method ( supplement-new ):
(1) Complete Goal 1, the program can run, and get results, count 80 points;
(2) Complete Goal 2, the program can run, and get results, count 90 points;
(3) Complete Goal 3, the program can run, and get results, count 100 points;
Mutual evaluation students, according to their own understanding, floating up and down 10 points. For example, if the comments are complete and the code is refined, add points. When multiple goals are completed, only the scores that are the highest score are calculated for this job.

methods and Steps (refer to target 1 for example):(1) Interpreting test instructions: What results do you want to get? The mind simulates its process--given a file, you count the total number of rows, the number of empty lines, and so on. (2) Target decomposition: What are some of the core steps? --Open a file, read a line of the file, determine the type of the line, the end of the file, the result output (output to the screen, or output to a file). (3) code writing: did not learn to write code (JAVA,OR CS), Baidu or Google search. --What are we searching for? How do you ask questions? (C # Statistics C, C + +, C # lines of code) which sites might be more trustworthy? (N sites a programmer needs to know)(4) Commissioning and testing: Is there an error in our code? Debugging and testing to see the foundation. --the work of the test engineer. What if the file name you entered does not exist? How do I judge a file that exists but is not a specified type? ...(5) Summary and improvement: what are the experiences and deficiencies? Write down, on the one hand remind yourself, on the other hand, share in the blog, also help others. For example: What problems are encountered, how to solve them? Command line way good, modify into a version of the interface, code rewrite more? Is it possible to try?
(1) Complete Goal 1, the program can run, and get results, count 80 points;
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\\workspaces\\myeclipse 8.5\\tyh\\src\\iotest\\ Staticscodeingline.java ");//note 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]]*$")) {Whiteli nes++; } else if (Line.startswith ("/*") &&!line.endswith ("*/")) {commentlines++; comment = true;} else if (true = = com ment) {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 ("Blank lines:" +white Lines); System.out.println ("Number of comment lines:" +commentlines); System.out.println ("Lines of Code:" +normallines); Br.close (); br = NULL; } catch (IOException e) {e.printstacktrace ();}} } }}

Java programming for the first time: source code Calculator

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.