Method Experiment Report

Source: Internet
Author: User
Tags readfile

Prime

ImportJava.util.*; Public classPrimenumbermanager {Private intoutput_count=0;//number of outputs per row    Private intMin = 3; Private intmax = 100;  Public voidCalculation () {//Number of primes in calculated rangeThe prime numbers between System.out.println (min+ "to" +max+ ") are:");  for(intI=min; i<=max; i++){            intsign=0;//Mark             for(intj=2; (J<math.sqrt (i)) && (sign==0); J + +){                if((I%J) ==0) { sign++; }            }            if(sign==0) {display (i); }} Output_count= 0;    System.out.println (); }         Public voidDisplayintNumber )        {System.out.print (number); Output_count++; if(Output_count = = 5) {//Line with five numbers, newlineSystem.out.println (); Output_count=0; }        Else{System.out.print (" "); }} @SuppressWarnings ("Resource")     Public voidSetminmax () {System.out.println ("Please enter two integers"); Scanner in=NewScanner (system.in); intMin =In.nextint (); intMax =In.nextint (); if(Max <min) {Max+=min; Min= max-min; Max= max-min; }         This. Min =min;  This. Max =Max;    Calculation (); }    }
Primenumbermanager.java
/**/Publicclass     primenumber {publicstatic  void  main (String [] args) {        new  Primenumbermanager ();        P.calculation ();        P.setminmax ();    }}
Primenumber

Palindrome

 Public classDetection {Private intSign=0; Private intlength; PrivateString str =NewString ();  Public voidsetstr (String str) {intN=0;  This. str =str; Length=str.length ();    Judge (n); }         Public voidJudgeintN) {        if((length-n) <=1) {//the string is either 0 or a single character Fu HuiwenSign=1; }        Else{            intFront =Str.charat (n); intback = Str.charat (length-n-1); if(Front = =Back ) {Judge (++N); }            Else{ Sign=0; }        }    }         Public voiddisplay () {if(sign = = 0) System.out.println ("The string does not palindrome"); ElseSystem.out.println ("The String Palindrome"); }    }
Detection.java
ImportJava.util.Scanner; Public classpalindrome { Public Static voidmain (String [] args) {Detection d=NewDetection (); @SuppressWarnings ("Resource") Scanner in=NewScanner (system.in); String Str=NewString (); System.out.println ("Please enter a string"); STR=In.next ();        D.setstr (str);    D.display (); }}
palindrome

Word frequency

ImportJava.io.*; Public classStatistics {PrivateString word= ""; Privatestring[] str =Newstring[500]; Private int[] Count =New int[500]; Private inti=0;//Count The number of existing words//Read File contents     Public voidreadFile (String filename) {file file=NewFile (filename); Reader Reader=NULL; Try{Reader=NewInputStreamReader (Newfileinputstream (file)); intTempchar;  while((Tempchar = Reader.read ())! =-1) {Extractword (Tempchar); //Pass each character} reader.close (); }        Catch(Exception e) {e.printstacktrace (); }    }        //Extracting Words     Public voidExtractword (intTempchar) {        if((tempchar>65&&tempchar<90) | | (tempchar>97&&tempchar<122)) ){//when Tempchar is a letter, appendWord + = (Char) Tempchar; }        Else if((!"". Equals (word))                                                {Statisticalword (); //StatisticsWord= ""; }    }        //Statistical Words     Public intStatisticalword () {intJ=0;  for(; j<i; J + + ){            if(Str[j].equals (Word)) {Count[j]++;//count The Times plus one                return0; }} Str[j]=word;//Add a Wordcount[j]++; I++; return0; }        //to display statistical information     Public voiddisplay () { for(intj=0; j<i; J + +) {System.out.println (Str[j]+ "\ T" +Count[j]); }    }    }
Statistics.java
 Public class Words {    publicstaticvoid  main (String [] args) {        = "c:/ File.txt ";         New Statistics ();        Sta.readfile (filename);        Sta.display ();    }}
Words.java

Method Experiment Report

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.