java_实验四
- 格式:pdf
- 大小:587.28 KB
- 文档页数:23


计算机工程学院实验报告课程名称Java 高级语言程序设计实验名称实验四、字符串与容器类实验类型设计型姓名学号日期地点成绩教师评语:一、实验目的1.掌握字符串类的应用;掌握正则表达式的基本应用;2.掌握几种常用容器类的使用方法。
二、实验内容设计实验内容:(1)设计一个字符串处理程序:实现字符串的整体、前缀、后缀比较;子字串定位;数字型字符转化为数字等各种功能。
(2).设计一个正则表达式处理程序:能完成输入字串的处理—(a) 能找出各种表达形式的日期;(b) 能对指定格式的子串进行替换。
(3)设计一个程序:利用List进行学生信息列表的存储和操作,能按学号搜索、增加、删除一个学生。
定义一个异常类,当删除不存在的学生时,将抛出错误(4)设计一个关键字统计程序:利用HashMap存储关键字统计信息,对用户输入的关键字序列进行统计三、实验要求1、掌握字符串定义的方法以及对字符串的各种操作功能,能够熟练运用其中的方法进行字符串操作;2、掌握集合的相关知识,能够使用其中的各种实现类解决实际问题;3、会定义异常操作,在程序运行的时候捕获异常。
四、实验内容:第一题:package string;import java.util.Scanner;public class Textstring {public static void main(String[] args) {String s1="abcdel .hcj9, int",s2="bcdsjhk,kji.hsk-/sjhg";Scanner input=new Scanner(System.in);System.out.println("----------字符串处理程序---------");System.out.println("字符串的整体比较如下:");if(pareTo(s2)<0)S ystem.out.println("'"+s1+"' < '"+s2+"'");elseS ystem.out.println("'"+s1+"' >= '"+s2+"'");//==================================================== System.out.println("字符串的前缀比较如下:");System.out.print("请输入要比较的前缀:");String s=input.next();if(s1.startsWith(s))System.out.println(s+"是"+s1+"的前缀。
实验四java知识本实验目的是练习如何创建Java 的基本语法,以及面向对象的知识,为后续的实验作好准备工作。
【课堂练习】一(1)对数组排序【实验效果】根据实验要求,您必须在浏览器的地址栏中输入:Tomcat服务器的IP地址和端口号,如:http://localhost:8080/sort.jsp【课堂练习】Java面向对象思想课堂练习——包创建和引用案例:新建Project项目,在该项目中创建aa.bb包,然后再引入java.util包中的Date类,并创建ShiJian类,练习包的创建和引用。
课堂练习——相同包之间的访问案例:在已经建立Project项目中创建mr.bbb包,然后在该包中创建Test和ImpTest两个类,测试同包之间类的访问。
课堂练习——不同包中类之间的访问案例:在已经建立Project项目中创建mr.aaa包和mr.zzk包,然后分别在包中创建Exam 和ImpExam两个类,测试不同包之间类的访问。
课堂练习——final变量案例:在已经建立Project项目中创建mingri包,并在该包中创建T1类,在该类中定义一个final变量,然后再主方法中创建T1类的实例,并测试能否改变final变量的值。
课堂练习——final方法案例:在已创建的项目Project的包mingri中创建T1类的之类ExT1,然后再ExT1类中测试是否能重写父类T1中的final方法。
课堂练习——继承性案例:在项目的包aaa中创建A和B两个类,使类B是类A的子类,然后在子类B中创建B的实例,并通过该实例分别调用类A和类B的方法,掌握继承的原理。
课堂练习——抽象类案例:在项目的包aaa中创建抽象类Example及其子类SubExample,在抽象类Example 中分别定义一个成员方法和一个抽象方法,并在子类中进行测试。
课堂练习——权限访问案例:在项目的包aaa中创建类Exm,在该类中分别定义4种权限修饰符的成员变量并附初值,然后在主方法中创建类的实例,并分别输出这4个成员变量的值,测试上述4种权限修饰符的成员变量在当前类中的可见性。
Java面向对象程序设计实验报告序号:实验三实验报告代码清单2.2)import java.util.Scanner;public class RightTriangle {public static void main (String[] args){double side1, side2,num; // lengths of the sides of a right triangle double hypotenuse; // length of the hypotenuseScanner scan = new Scanner(System.in);// Get the lengths of the sides as inputSystem.out.println ("Please enter the lengths of the two sides of " + "a right triangle (separate by a blank space): ");System.out.print ("side1 = ");side1 = scan.nextDouble();System.out.print ("side2 = ");side2 = scan.nextDouble();num = side1*side1 + side2*side2;hypotenuse = Math.sqrt(num);System.out.println ("Length of the hypotenuse: " + hypotenuse);}}4.import java.util.Random;public class Dice{public static void main (String[] args){Random generator = new Random();int num1,num2;System.out.print("num1 = ");num1 = generator.nextInt(6) + 1;System.out.println(num1);System.out.print("num2 = ");num2 = generator.nextInt(6) + 1;System.out.println(num2);System.out.println("The total of two numbers = " + (num1 + num2));}}6.import java.util.Scanner;import java.text.NumberFormat;import java.text.DecimalFormat;public class Deli{// ---------------------------------------------------// main reads in the price per pound of a deli item// and number of ounces of a deli item then computes// the total price and prints a "label" for the item// --------------------------------------------------public static void main (String[] args){final double OUNCES_PER_POUND = 16.0;double pricePerPound; // price per pounddouble weightOunces; // weight in ouncesdouble weight; // weight in poundsdouble totalPrice; // total price for the itemScanner scan = new Scanner(System.in);NumberFormat money = NumberFormat.getCurrencyInstance();DecimalFormat fmt = new DecimalFormat("0.###");// Declare money as a NumberFormat object and use the// getCurrencyInstance method to assign it a value// Declare fmt as a DecimalFormat object and instantiate// it to format numbers with at least one digit to the left of the// decimal and the fractional part rounded to two digits.// prompt the user and read in each inputSystem.out.println ("Welcome to the CS Deli!!\n ");System.out.print ("Enter the price per pound of your item: ");pricePerPound = scan.nextDouble();System.out.print ("Enter the weight (ounces): ");weightOunces = scan.nextDouble();// Convert ounces to pounds and compute the total priceweight = weightOunces / OUNCES_PER_POUND;totalPrice = pricePerPound * weight;// Print the label using the formatting objects// fmt for the weight in pounds and money for the pricesSystem.out.println("***** CS Deli *****");System.out.println();System.out.println("Unit Price: ¥"+pricePerPound +" per pound");System.out.println("Weight: "+weight +" pounds");System.out.println("Total: ¥" + totalPrice);}}。