第一题:分析以下需求,并用代码实现
1.键盘录入一个大字符串,再录入一个小字符串
2.统计小字符串在大字符串中出现的次数
3.代码运行打印格式:
请输入大字符串:woaiheima,heimabutongyubaima,wulunheimahaishibaima,zhaodaogongzuojiushihaoma
请输入小字符串:heima
控制台输出:共出现3次
/*
package day08.homework;
/*
1.键盘录入一个大字符串,再录入一个小字符串
2.统计小字符串在大字符串中出现的次数
3.代码运行打印格式:
请输入大字符串:woaiheima,heimabutongyubaima,wulunheimahaishibaima,zhaodaogongzuojiushihaoma
请输入小字符串:heima
控制台输出:共出现3次
*/
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
System.out.println("请输入大字符串:");
String strBig=new Scanner(System.in).next();
System.out.println("请输入小字符串:");
String strSmall=new Scanner(System.in).next();
int count=0;
int index;
for (int i = 0; i <strBig.length(); i++) {
//查找参数字符串在本字符串当中首次出现的索引位置。如果没有返回-1值。
//判断,如果返回值不为-1,执行循环体,计数器+1;
while ((index=strBig.indexOf(strSmall))!=-1){
count++;
//截取从(小字符串首字母位置加上小字符串长度)一直到字符串末尾,返回新的字符串
strBig=strBig.substring(index+strSmall.length());
}
}
System.out.println("共出现"+count+"次");
}
}
*/
第二题:分析以下需求,并用代码实现
定义String getStr()方法
功能描述:
获取长度为5的随机字符串
字符串由随机的4个大写英文字母和1个0-9之间(包含0和9)的整数组成
英文字母和数字的顺序是随机的
/*
package day08.homework;
import java.util.Random;
/*
定义String getStr()方法
功能描述:
获取长度为5的随机字符串
字符串由随机的4个大写英文字母和1个0-9之间(包含0和9)的整数组成
英文字母和数字的顺序是随机的
*/
public class Test2 {public static void main(String[] args) {
char[] ch2=getStr();
char[] ch3=new char[5];
//根据索引随机取出ch2中的字符,存放到新的字符数组ch3中,现在ch3中存放的是5个字母
Random random=new Random();
for(int i=0;i<ch3.length;i++){
char char1=ch2[random.nextInt(ch2.length)];
ch3[i]=char1;
}
//随机取出一个字母替换成数字
int index=random.nextInt(ch3.length);
int num=random.nextInt(9)+48;
ch3[index]=(char)num;
System.out.println(ch3);
}
public static char[] getStr(){
//先把大小写英文字母和数字存储到字符数组里,再随机取出5个字符
char[] ch1=new char[26];
int count=0;
for(char c='A'; c<='Z';c++){
ch1[count++]=c;
}
/*for(char c='a'; c<='z';c++){
ch1[count++]=c;
}*/
return ch1;
}
}
*/
第三题:分析以下需求,并用代码实现
要求:完成代码(按照标准格式写),然后在测试类中测试。
1.手机类Phone
属性:品牌brand,价格price
无参构造,有参构造
行为:打电话call,发短信sendMessage,玩游戏playGame
2.测试类
创建Phone类对象,调用Phone类中的方法
思考:假设所有的手机都有属性屏幕的尺寸(int size),而且假设所有手机的屏幕尺寸为6,应该如何实现?
/*
package day08.homework;
/*
1.手机类Phone
属性:品牌brand,价格price
无参构造,有参构造
行为:打电话call,发短信sendMessage,玩游戏playGame
*/
public class Phone {
private String brand;
private double price;
static int size;
public void call() {
System.out.println("打电话");
}
public void sendMessage() {
System.out.println("发短信");
}
public void playGame() {
System.out.println("玩游戏");
}
public Phone() {
}
public Phone(String brand, double price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public static int getSize() {
return size;
}
public static void setSize(int size) {
Phone.size = size;
}
}
*/
/*
package day08.homework;
/*
测试类
创建Phone类对象,调用Phone类中的方法
思考:假设所有的手机都有属性屏幕的尺寸(int size),而且假设所有手机的屏幕尺寸为6,应该如何实现?
*/
public class PhoneTest {
public static void main(String[] args) {
Phone ph=new Phone();
Phone.setSize(6);
ph.setBrand("小米");
ph.setPrice(1999.0);
System.out.println(ph.getPrice()+"的"+ph.getBrand()+"手机尺寸为"+Phone.getSize());
ph.call();
ph.sendMessage();
ph.playGame();
System.out.println("================");
Phone ph2=new Phone();
ph2.setBrand("苹果");
ph2.setPrice(8999.0);
System.out.println(ph2.getPrice()+"的"+ph2.getBrand()+"手机尺寸为"+Phone.getSize());
ph.call();
ph.sendMessage();
ph.playGame();
}
}
*/