A few small programs of Javaapi Lang

Source: Internet
Author: User

/*write a method to count the number of digits, the number of spaces, the number of lowercase English letters, and the number of uppercase letters in a string. */classCensus {String str; intnum; intCHS; intupcase; intlowcase;  Publiccensus () {} PublicCensus (String str) { This();  This. str =str; }     Public intGetChar () {Char[] ch =Str.tochararray ();  for(inti = 0; i < ch.length; i++) {            if(Character.isletter (Ch[i])) {CHS++;                    }; }        returnCHS; }     Public intGetnum () {Char[] ch =Str.tochararray ();  for(inti = 0; i < ch.length; i++) {            Charc =Ch[i]; if(Character.isdigit (Ch[i])) {num++;        }; }        returnnum; }     Public intgetupcase () {Char[] ch =Str.tochararray ();  for(inti = 0; i < ch.length; i++) {            Charc =Ch[i]; if(Character.isuppercase (Ch[i])) {upcase++;        }; }        returnupcase; }     Public intgetlowcase () {Char[] ch =Str.tochararray ();  for(inti = 0; i < ch.length; i++) {            Charc =Ch[i]; if(Character.islowercase (Ch[i])) {Lowcase++;        }; }        returnlowcase; } @Override PublicString toString () {return"str=" + str + ", number of numbers =" + This. Getnum () + ", number of characters =" + This. GetChar ()+ ", number of uppercase letters =" + This. Getupcase () + ", number of lowercase letters =" + This.    Getlowcase (); }            } Public classcensustest{ Public Static voidMain (string[] args) {Census a=NewCensus ("78uijkiiorey228");                System.out.println (A.tostring ()); }}

Calculates the distance between a circle's area and perimeter, and the center point to the origin.

classPoint {//defining the Point class    Doublex; Doubley; DoubleZ;  PublicPoint () {} PublicPoint (DoubleXDoubleYDoublez) { This();  This. x =x;  This. y =y;  This. z =Z; } @Override PublicString toString () {return"Point [x=" + x + ", y=" + y + ", z=" + z + "]"; }     Public DoubleDistanceofo () {returnMATH.SQRT (x*x+y*y+z*z); }} Public classpointtest{ Public Static voidMain (string[] args) {Point a=NewPoint (15,19,23);         System.out.println (A.distanceofo ()); //output Double type point to Origin distanceSystem.out.println (String.Format ("%.2f", (A.distanceofo ())));//output two digits after the decimal digit    }}
classCircleextendspoint{//defines a subclass the circle parent class is a point    Doubleradius;  PublicCircle () {} PublicCircle (DoubleXDoubleYDoubleZDoubleradius) {        Super(x, y, z);  This. Radius =radius; }     Public DoubleGetperimeter () {//Calculate Perimeter        return(math.pi*radius*2); }     Public DoubleGetarea () {return(Math.pi*radius*radius);//Calculate Area    }        } Public classCircletest { Public Static voidMain (string[] args) {Circle a=NewCircle (10,20,32,25); System.out.println (a.tostring ()+ "Circle area of Coordinate point ==>" +A.getarea ()); System.out.println (a.tostring ()+ "Coordinate point circumference length ==>" +A.getperimeter ()); }}

Calculate a large number factorial using one line of code

ImportJava.math.BigInteger;ImportJava.util.Scanner; Public classFactorial { Public StaticBigInteger FAC (intN) {        return(n = = 1?)NewBigInteger ("" + 1):NewBigInteger ("+n"). Multiply (FAC (n-1))); }     Public Static voidMain (string[] args) {Scanner a=NewScanner (system.in); System.out.println ("Please enter a number"); intSCR =A.nextint (); SYSTEM.OUT.PRINTLN (SCR+ "factorial = =" +FAC (SCR)); }}

A few small programs of Javaapi Lang

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.