/*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