java实验三_JAVA实验三及总结

JAVA第五周作业

Java实验报告三

第一题

1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)

(1)、统计该字符串中字母s出现的次数。

(2)、统计该字符串中子串“is”出现的次数。

(3)、统计该字符串中单词“is”出现的次数。

(4)、实现该字符串的倒序输出。

实验代码

(1)统计该字符串中字母s出现的次数。

public class Letter {

public static void main(String args[]) {

String str="This is a test of Java";

int count=0;

char c[]=str.toCharArray();

for(int i=0;i

if(c[i]=='s') {

count++;

}

}

System.out.println(count);

}

}

运行结果

756ae73c75635b18ec0c2d9a376692b1.png

实验代码

(2)、统计该字符串中子串“is”出现的次数。

public class Character {

public static void main(String args[]) {

String str="This is a test of Java";

int count=0;

int i=0;

while(str.indexOf("is",i)!=-1) {

count++;

i=str.indexOf("is",i)+1;

}

System.out.println(count);

}

}

运行结果

12bd4853e6c3dc4acc01b6d7e4eebf9b.png

实验代码

(3)、统计该字符串中单词“is”出现的次数。

public class Word {

public static void main(String args[]) {

String str="This is a test of Java";

int count=0;

int i=0;

while(str.indexOf(" is ",i)!=-1) {

count++;

i=str.indexOf(" is ",i)+1;

}

System.out.println(count);

}

}

运行结果

636963cce93179b1388939c9462efa2b.png

实验结果

(4)、实现该字符串的倒序输出。

public class ReverseString {

public static void reverseString (String str){

StringBuffer stringBuffer = new StringBuffer (str);

System.out.print(stringBuffer.reverse());

}

public static void main (String args[]){

String originalString="This is a test of Java";

reverseString (originalString);

}

}

(此代码借鉴于一位CSDN大佬的StringBuffer方法,在自己的理解下进行了更改并理解了代码。)

import javax.swing.JOptionPane;

public class ReverseString {

public static void reverseString (String str){

StringBuffer stringBuffer = new StringBuffer (str);

System.out.print(stringBuffer.reverse());

}

public static void main (String args[]){

String originalString;

originalString = JOptionPane.showInputDialog("Please input a String: ");

reverseString (originalString);

}

}

(此代码为CSDN上大佬的代码)

运行结果

25292e1d73e13af09f449a122cde2c31.png

2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。

76d9ff51e91615fd67308c94e895a89c.png

实验代码

import java.util.*;

public class Change {

public static void main(String args[]) {

Scanner jj = new Scanner(System.in);

String y = jj.next();

int i=0;

char c[]=y.toCharArray();

for(i=0;i

c[i]=(char)(c[i]+3);

//System.out.println(c[i]);

}

String a = new String(c);

System.out.println(a);

}

}

室友的耐心教导,让我写出来了。

运行结果

c2b9440226ba4b2d6b9ce2fafe65aeb8.png

3.已知字符串“ddejidsEFALDFfnef2357 3ed”。输出字符串里的大写字母数,小写英文字母数,非英文字母数。

实验代码

public class Classify {

public static void main(String args[]) {

String str = "ddejidsEFALDFfnef2357 3ed";

char c[] = str.toCharArray();

int count1 = 0, count2 = 0, count3 = 0;

for(int i = 0; i <= str.length() - 1;i++){

if(c[i] >= 'A' && c[i] <= 'Z'){

count1++;

}

else if(c[i] >= 'a' && c[i] <= 'z') {

count2++;

}

else {

count3++;

}

}

System.out.println("大写字母数:"+count1);

System.out.println("小写字母数:"+count2);

System.out.println("非英文字母数:"+count3);

}

}

运行结果

3acc96e9de4b61f197de4915cc9ea8e9.png

学习笔记

1、子类不能直接访问父类的私有属性,可通过get(),set()来间接访问。

2、super(),this()不可同时使用,因为其都必须放在首行,所以不可同时使用。

3、若删去super(),则无法使用父类的无参构造。

4、this(),super()不可出现在main方法中。

5、子类可扩大权限,不可缩小。

9fbeb9f26e1588184477979cac0ae82b.png

589fdc228e006fd6840580e0fc3ed85d.png

学习心得

好好学习,天天向上。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值