实验5 JAVA常用类

一.实验目的:

(1)掌握常用的String,StringBuffer(StringBuilder)类的构造方法的使用;

(2)掌握八种基本类型对应的包装类,以及他们之间的区别与联系。

(3)掌握字符串的比较方法,尤其equals方法和==比较的区别;

(4)掌握String类常用方法的使用;

(5)掌握字符串与字符数组和byte数组之间的转换方法;

(6)Date,Math, PrintWriter,Scanner类的常用方法。

二.实验内容

1编写一个类MyInteger,类中包含一个int数据成员保存这个对象表示的int值,包含一个无参构造方法,一个有参构造方法,包含isEven(),isOdd(),和isPrime()三个方法,返回boolean值表示该整数是否是偶数,奇数,素数。

程序源代码

public class MyInteger {
// int数据成员,保存这个对象表示的int值
private int value;

// 无参构造方法
public MyInteger() {
// 将value赋值为0
this.value = 0;
}

// 有参构造方法
public MyInteger(int value) {
this.value = value;
}

// 获取value的值
public int getValue() {
return value;
}

// 判断value是否是偶数
public boolean isEven() {
return value % 2 == 0;
}

// 判断value是否是奇数
public boolean isOdd() {
return value % 2 != 0;
}

// 判断value是否是素数
public boolean isPrime() {
// 如果value小于2,返回false
if (value < 2) {
return false;
}
// 从2开始遍历到value的平方根,如果value能够被遍历到的数整除,返回false
for (int i = 2; i <= Math.sqrt(value); i++) {
if (value % i == 0) {
return false;
}
}
// 如果没有被整除,返回true
return true;
}

// main函数
public static void main(String[] args) {
// 创建MyInteger对象,值为5
MyInteger myInt = new MyInteger(5);

// 调用isEven()方法,判断myInt是否是偶数
boolean isEven = myInt.isEven();
System.out.println("myInt是否是偶数:" + isEven);

// 调用isOdd()方法,判断myInt是否是奇数
boolean isOdd = myInt.isOdd();
System.out.println("myInt是否是奇数:" + isOdd);

// 调用isPrime()方法,判断myInt是否是素数
boolean isPrime = myInt.isPrime();
System.out.println("myInt是否是素数:" + isPrime);
}
}








程序运行结果贴图

 

2.二进制数转换为十六进制数

程序源代码

import java.util.Scanner;

public class BinaryToHex {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// 输入二进制数
System.out.print("请输入一个二进制数:");
String binary = scanner.nextLine();

// 将二进制数转换为十六进制数
String hex = Integer.toHexString(Integer.parseInt(binary, 2));

// 输出结果
System.out.println("十六进制数为:" + hex);
}
}












程序运行结果贴图

 

3.将十进制转换为二进制

程序源代码

import java.util.Scanner;

public class DecimalToBinary {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// 输入十进制数
System.out.print("请输入一个十进制数:");
int decimal = scanner.nextInt();

// 将十进制数转换为二进制数
String binary = Integer.toBinaryString(decimal);

// 输出结果
System.out.println("二进制数为:" + binary);
}
}


 

程序运行结果贴图

 

4. 一些网站设定了一些制定密码的规则。编写一个方法,检验一个字符串是否是合法的密码。假设密码规则如下:

(1)密码必须至少有8个字符。(2)密码只能包括字母和数字。

(3)密码必须至少有2个数字。

编写一个程序,提示用户输入密码,如果该密码符合规则就显示“Valid Password”,否则显示“Invalid Password”。

程序源代码

import java.util.Scanner;

public class PasswordChecker {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a password: ");
    String password = input.nextLine();

    if (isValidPassword(password)) {
      System.out.println("Valid password");
    } else {
      System.out.println("Invalid password");
    }
  }

  public static boolean isValidPassword(String password) {
    // Check if password is at least 8 characters long
    if (password.length() < 8) {
      return false;
    }

    // Check if password contains only letters and numbers
    for (int i = 0; i < password.length(); i++) {
      char ch = password.charAt(i);
      if (!Character.isLetterOrDigit(ch)) {
        return false;
      }
    }

    // Check if password contains at least 2 digits
    int numDigits = 0;
    for (int i = 0; i < password.length(); i++) {
      char ch = password.charAt(i);
      if (Character.isDigit(ch)) {
        numDigits++;
      }
    }
    if (numDigits < 2) {
      return false;
    }

    // If all checks pass, password is valid
    return true;
  }
}

程序运行结果贴图

5.使用下面的方法头编写一个方法,找出某个指定字符在字符串中出现的次数:public static int count(String str,char a)

例如,count(“Welcome”,’e’)返回2.编写一个测试程序 ,提示用户输入一个字符串,在该字符串后紧跟着一个字符,然后显示这个字符在字符串中出现的次数。

程序源代码

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = input.nextLine();
        System.out.print("Enter a character: ");
        char a = input.next().charAt(0);

        int count = count(str, a);
        System.out.println("The character '" + a + "' appears " + count + " times in the string.");
    }

    public static int count(String str, char a) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == a) {
                count++;
            }
        }
        return count;
    }

}

程序运行结果贴图

 

6. Java 提供了3 个日期类:Date、Calendar 和DateFormat。其中,Date 类主要用于创建日期对象并获取日期,Calendar 类可获取和设置日期,DateFormat 类用来设置日期的格式。Java 语言规定的基准日期为1970.1.1 00:00:00 格林威治(GMT)标准时间,当前日期是由基准日期开始所经历的毫秒数转换出来的。

程序源代码如下,手工输入,认真分析并运行程序,掌握java日期相关类的用法。

import java.util.*;

import java.text.*;

public class KY5_10

{

public static void main (String args[])

{

Date today = new Date(); //当前日期和时间

SimpleDateFormat sdf;

sdf= new SimpleDateFormat("yyyy 年MM 月dd 日hh 时mm 分ss 秒 a EEEEE");

System.out.println("当前日期和时间: "+sdf.format(today));

long hms=System.currentTimeMillis(); //当前时间的毫秒数

System.out.println("当前时间的毫秒数="+hms);

Date tomorrow = new Date(hms+24*60*60*1000);

System.out.println("明天是"+sdf.format(tomorrow));

Calendar now = Calendar.getInstance();

int year =now.get(Calendar.YEAR); //年份

int month=now.get(Calendar.MONTH)+1; //月份

int day = now.get(Calendar.DATE); //日期

System.out.print("今天是"+year+"年"+month+"月"+day+"日");

int week = now.get(Calendar.DAY_OF_WEEK); //星期

switch (week)

{

case 1: System.out.println(" 星期日");break;

case 2: System.out.println(" 星期一");break;

case 3: System.out.println(" 星期二");break;

case 4: System.out.println(" 星期三");break;

case 5: System.out.println(" 星期四");break;

case 6: System.out.println(" 星期五");break;

case 7: System.out.println(" 星期六");break;

}

}

}

• 编译并运行程序

程序运行结果贴图

 

7 Math 是一个最终类,含有基本数学运算函数。创建使用Math 类的应用程序,程序中使用如指数运算、对数运算、求平方根、三角函数、随机数等,可以直接在程序中加Math.前缀调用。

程序源代码

public class MathDemo {
    public static void main(String[] args) {
        // 求数字2的3次方
        double result1 = Math.pow(2, 3);
        System.out.println("2的3次方为:" + result1);
        // 求16的平方根
        double result2 = Math.sqrt(16);
        System.out.println("16的平方根为:" + result2);
        // 求π的值
        double result3 = Math.PI;
        System.out.println("π的值为:" + result3);
        // 求sin(π/2)的值
        double result4 = Math.sin(Math.PI / 2);
        System.out.println("sin(π/2)的值为:" + result4);
        // 生成0到1之间的随机数
        double result5 = Math.random();
        System.out.println("生成的随机数为:" + result5);
    }
}

程序运行结果贴图

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

+720

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值