java常见方法

创建一个直角三角形类实现IShape接口
class RTriangle implements IShape{
private double a,b;
public RTriangle(double a, double b) {
this.a = a;
this.b = b;
}
public double getArea() {
return 0.5ab;
}
public double getPerimeter() {
return a+b+Math.sqrt(aa+bb);
}

从抽象类shape类扩展出一个圆形类Circle
class Circle extends shape{
private double radius;
public Circle(double a) {
super();
this.radius = a;
}
public double getArea() {
return Math.PIradiusradius;
}
public double getPerimeter() {
return 2Math.PIradius;
}
}

设计矩形类
class Rectangle{
double width;
double height;
public Rectangle(double width1,double height1){
this.width=width1;
this.height=height1;
}
double getArea(){
return width*height;
}
double getPerimeter(){
return (width+height)*2;
}
}

股票类
class Stock {
String symbol;
String name;
Double previousClosingPrice;
Double currentPrice;
public Stock(String symbol,String name) {
this.symbol=symbol;
this.name=name;
}
public Double changePercent() {
return (currentPrice-previousClosingPrice)/previousClosingPrice; }
}

正五边形类
class RPentagon extends shape{
RPentagon(double side){
a=side;
}
public double getArea(){
return 0.25aaMath.sqrt(25+10Math.sqrt(5));
}
public double getPerimeter(){
return 5*a;
}
private double a;
}

BOOk类
class Book
{
private String name, zuozhe;
private int price, date;
public Book(String name, int price, String zuozhe, int date)
{
this.name = name;
this.price = price;
this.zuozhe = zuozhe;
this.date = date;
}
public int getPrice()
{
return this.price;
}

}

从抽象类拓展正n边形
class RegularPolygon extends shape{
private double a;
private int n;
public RegularPolygon(int n1,double a1){
this.a=a1;
this.n=n1;
}
public double getArea(){
return naa/(Math.tan(Math.toRadians(180/n))4);
}
public double getPerimeter(){
return n
a;}}

将学生对象按照学号升序
Scanner a = new Scanner(System.in);
String[] std=new String[5];
for(int i=0;i<5;i++){
std[i]=a.nextLine();
}
for(String stu:std){
String[] s = stu.split(“\s+”);
ar.add(new Student(s[0],s[1],Float.parseFloat(s[2])));
}
Collections.sort(ar,(x,y)-> (int) (Integer.parseInt(x.number)-Integer.parseInt(y.number)));
for(Student s:ar){
System.out.println(s);}}}

将学生对象降序排序
Scanner in = new Scanner(System.in);
Student[] temp = new Student[5];
for (int i = 0; i < 5; i++) {
temp[i] = new Student(in.next(),in.next(),in.nextFloat());}
for (int i = 0; i < 5; i++) {
for(int j=i;j<5;j++){
if(temp[j].score > temp[i].score){
Student a = temp[i];
temp[i] = temp[j];
temp[j] = a;}}
System.out.println(temp[i].toString());
}
}

大数整除
public class Main{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int count=0;
BigInteger j=new BigInteger(String.valueOf(Long.MAX_VALUE));
j=j.add(BigInteger.valueOf(1));
while(count<3){
if(j.mod(BigInteger.valueOf(n)).intValue()==0){
System.out.println(j.toString());
++count;
}
j=j.add(BigInteger.valueOf(1));
}
input.close();}}

验证手机号码
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.nextLine();
int len = s.length();
if(len != 11) {
System.out.println(“No”);
}
else {
char o = s.charAt(0);
if(o != ‘1’) {
System.out.println(“No”);
}
else {
boolean flag = true;
for(int i = 1; i < len; i++){
char n = s.charAt(i);
if(n > ‘9’ || n < ‘0’) {
System.out.println(“No”);
flag = false;
break;
}
}
if(flag) {
System.out.println(“Yes”);
} } }}}

找素数
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String sc = in.next();
BigInteger m = new BigInteger(sc);
int n = in.nextInt();
int i=0;
while(i<n){
if(isPrime(m)){
System.out.println(m);
i++;}m=m.add(BigInteger.ONE);}}
public static boolean isPrime(BigInteger num) {
return num.isProbablePrime(50);}}

电话号码同步
public class Main {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
String string;
String string2;
Set set=new TreeSet();
Set set2=new TreeSet();
Set set3=new TreeSet();
while(in.hasNextLine()){
string=in.nextLine();
if(string.equals(“end”)){
break;
}
else{
set.add(string);}}
while(in.hasNextLine()){
string2=in.nextLine();
if(string2.equals(“end”)){
break;
}
else{
set2.add(string2);}}
set3.addAll(set);
set3.addAll(set2);
Iterator iterator=set3.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}}}

简单计算器
public class Main {
public static void main(String[] args) {
String str=new String();Scanner input=new Scanner(System.in);
str=input.next();
if(str.indexOf(“/”)!=-1){
String[] ob=str.split(“/”,0);
BigInteger a=new BigInteger(ob[0]);
BigInteger b=new BigInteger(ob[1]);
a=a.divide(b);
System.out.println(a);}
if(str.indexOf(“-”)!=-1){
String[] ob=str.split(“-”,0);
BigInteger a=new BigInteger(ob[0]);
BigInteger b=new BigInteger(ob[1]);
a=a.subtract(b);
System.out.println(a);}
if(str.indexOf(" ”)==-1)
String ob=str.substring(0,str.indexOf(““));
String oa=str.substring(str.indexOf(”
”)+1);
BigInteger a=new BigInteger(ob);
BigInteger b=new BigInteger(oa);
a=a.multiply(b);
System.out.println(a);}
if(str.indexOf(“+”)!=-1){
String ob=str.substring(0,str.indexOf(“+”));
String oa=str.substring(str.indexOf(“+”)+1);
BigInteger a=new BigInteger(ob);
BigInteger b=new BigInteger(oa);
a=a.add(b);
System.out.println(a);}}}

标识符命名方式转换:
Camel2Snake
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char[] ch = new char[str.length()];
for(int x = 0;x < str.length();x++) {
ch[x] = str.charAt(x);
if(ch[x] >= ‘A’ && ch[x] <= ‘Z’) {
ch[x] += 32;
System.out.print(“_”+ch[x]);}
else{
System.out.print(ch[x]);}}}}

设计一个简单的英汉专业词典
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
HashMap stringHashMap = new HashMap<>();
stringHashMap.put(“encapsulation”,“封装性”);
stringHashMap.put(“inheritance”,“继承性”);
stringHashMap.put(“polymorphism”,“多态性”);
stringHashMap.put(“object”,“对象”);
stringHashMap.put(“constructor”,“构造方法”);
stringHashMap.put(“message”,“消息”);
stringHashMap.put(“multithreading”,“多线程”);
String key=scanner.next();
if(stringHashMap.get(key) ==null){
System.out.println(“抱歉!没有找到” + key);
}else {
System.out.println(key+" : "+stringHashMap.get(key));}}}

统计字符串中的字符
public class Main{
public static void main(String[]args){
Scanner in = new Scanner(System.in);
String arr = in.next();
int ans = 0;
arr = arr.toLowerCase(Locale.ROOT);
for(int i=0;i<arr.length();i++){
if(arr.charAt(i) <= ‘z’ && arr.charAt(i) >= ‘a’) ans++;}
System.out.println("The number of letters inside the string is: " +ans); }}

字符串替换
public class Main{
public static void main(String []args){
ArrayList ls =new ArrayList();
Scanner in=new Scanner(System.in);
int len=0;
ls.add(in.nextLine());
while(ls.get(len).compareTo(“end”)!=0){
len++;
ls.add(in.nextLine());
}
String a=in.nextLine();
String b=in.nextLine();
String []ss=new String[len];
for(int i=0;i<len;i++){
ss[i]=ls.get(i).replace(a,b);
}
for(int i=0;i<len;i++){
System.out.println(ss[i]);}
in.close();}}

查找成绩并折算后输出
import java.util.*;
public class Mai
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
Map<String,Double> A=new HashMap<String,Double>();
String name;Double score;
while(true)
{
name=scan.next();
if(name.equals(“noname”))
{
break;
}
score=scan.nextDouble();
A.put(name, score);
}
String aim=scan.next();
if(A.containsKey(aim))
{
System.out.printf(“%.2f”, A.get(aim)*0.21);
}
else
{
System.out.println(“Not found.”);
}
scan.close();
}
}

给定三角形的三边,求解三角形的面积
public class Main {
public static void main(String[] args)
{ try(Scanner sc = new Scanner(System.in)){
int a, b ,c;
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
if(a + b > c && a + c > b && b + c > a){
double p = (a + b + c) / 2.0;
double s = Math.sqrt(p*(p-a)(p-b)(p-c));
System.out.printf(“The area is: %.3f.”, s);}
else{System.out.println(“It can not be created a triangle.”);}}}

找出最大的对象
public class Main
{
public static void main(String[] args)
{
String[] arr = new String[5];
int[] info = new int[5];
try(Scanner sc = new Scanner(System.in))
{
String maxStr = “”;
int maxInteger = Integer.MIN_VALUE;
for(int i =0 ; i < 5; i++){
arr[i] = sc.nextLine();
maxStr = arr[i].compareTo(maxStr) > 0? arr[i] : maxStr;
}
for(int i = 0; i < 5; i++){
info[i] = sc.nextInt();
maxInteger = maxInteger > info[i] ? maxInteger : info[i];
}
System.out.println("Max string is " + maxStr);
System.out.println("Max integer is " + maxInteger);}
设计一个矩形类Rectangle并创建测试类
class Main {
public static void main(String[] args) {
Scanner a =new Scanner(System.in);
double a1 = a.nextDouble();
double a2 =a.nextDouble();
System.out.println(a1a2);
System.out.println(2.0
(a1+a2));}}

求解给定字符串的前缀
public class Main {
public static void main(String[] argc) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String s = sc.next();
String s1 = sc.next();
StringBuilder ans = new StringBuilder();
for(int i = 0 ; i < s.length() && i < s1.length() ;i++){
if(s.charAt(i) == s1.charAt(i)){
ans.append(s.charAt(i));
}else{break;}}
if(ans.length() != 0) {
System.out.println("The common prefix is "+ans);
}else{System.out.println(“No common prefix”);}}}}

求几何形状的面积之和
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Circle r1 = new Circle(sc.nextDouble());
Circle r2 = new Circle(sc.nextDouble());
Rectangle r3 = new Rectangle(sc.nextDouble(),sc.nextDouble());
Rectangle r4 = new Rectangle(sc.nextDouble(),sc.nextDouble());
DecimalFormat d = new DecimalFormat(“#.####”);// 保留4位小数
double tot = r1.getArea()+r2.getArea()+r3.getArea()+r4.getArea();
System.out.println("The total area is "+d.format(tot));}}
abstract class shape {// 抽象类
public abstract double getArea( );
public abstract double getPerimeter( );}
class Circle extends shape{
double r;
public Circle(double r){
this.r = r; }
public double getArea() {
return r * r * Math.PI;
}
public double getPerimeter() {
return 2 * Math.PI * r;}}
class Rectangle{
double width;
double height;
public Rectangle(double a,double b){
width = a;
height = b;}
public double getArea(){
return width * height;}
public double getPerimeter(){
return 2 * (width + height);}}

两个巨大素数(质数)的乘积
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
String l = in.next();
BigInteger sum = new BigInteger(s);
BigInteger a = new BigInteger(l);
BigInteger b = sum.divide(a);
System.out.print(b);}}

定义有静态成员的学生类Student(Java)
class Student{
int number;
String name;
float score;
static int count = 0;
static float sum = 0;
public Student(int number1, String name1, float score1){
number = number1;
name = name1;
score = score1;
count++;
sum += score;}
public static void average(){
System.out.printf(“sum is %.1f count is %d\naverage is %f”, sum, count, sum/count);}
public void print() {
System.out.printf(“number: %d name: %s score: %.1f count: %d\n”, number, name, score, count);}}

求阶乘factorial
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num = input.nextInt();
BigInteger n = BigInteger.ONE;
BigInteger m = BigInteger.ONE;
for (int i = 1; i <= num; i++) {
n = n.multiply(m);
m = m.add(BigInteger.ONE);}
System.out.println(n);} }

日期加减
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
GregorianCalendar gc=new GregorianCalendar();
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd”);
long millis=in.nextLong();
Long day=in.nextLong();
Long ms=day2460601000+millis; //将天数转换成毫秒数
gc.setTimeInMillis(millis);
System.out.println(sdf.format(gc.getTime()));//抽象类Calendar的getTime()方法返回一个Date对象
gc.setTimeInMillis(ms);
System.out.println(sdf.format(gc.getTime()));}}

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值