实验5Java常用类(一)
java实验5图形用户界面设计试验

java实验5图形⽤户界⾯设计试验常⽤布局1)、流布局: FlowLayout 从左到右,⾃上⽽下⽅式在容器中排列,控件的⼤⼩不会随容器⼤⼩变化.容器.setLayout(new FlowLayout(FlowLayout.LEFT));2)、⽹格布局: GridLayout 按照指定⾏数与列数,将容器分成⼤⼩相等的单元格每个单元格放置⼀个控件. 不能将控件放在指定单元格 容器.setLayout(new GridLayout(3,4,10,15));3)、边界布局: BorderLayout 将容器分成东、西、南、北、中五个部分 容器.setLayout(new BorderLayout()); 窗⼝的内容⾯板默认布局就是边界布局。
容器.add(控件,BorderLayout.NORTH);4)、混合布局: 使⽤JPanel,将多个布局组合在⼀起使⽤5)、绝对布局 null: 以坐标定位 容器.setLayout(null); 每个控件在放置在容器之前,必须设置其边界 setBounds(x,y,width,height); btn.setBounds(10,100,30,60);常⽤事件1)、事件源 EventSource:能够触发事件控件如:JButton,JTextField,JFrame,JComboBox,....2)、事件 Event:ActionEvent,KeyEvent,WindowEvent,TextEvent,...3)、事件侦听者Listener(接⼝) ActionListener,WindowListener,...class A implements ActionListener{public void actionPerformed(ActionEvent e){....}}A lis=new A();4)、事件处理函数public void actionPerformed(ActionEvent e){....}事件流程:事件源触发事件-->事件源侦听者接收事件-->⾃动调⽤相应事件处理函数.实践编程1.在应⽤程序窗体中安排1个⽂本框,⼀个标签。
JAVA语言实验指导

JAVA语言实验指导《Java程序设计》课程实验指导书实验一熟悉Java编程环境和Java程序结构一、实验目的:通过简单的输入输出程序熟悉Java编程环境,认识Java程序结构。
二、实验内容:在JDK或者V isual J++环境下编写简单的输入输出程序。
三、实验要求:1. 接收一个键盘输入的字符;2. 输出一条简单的问候信息;3. 写出实验报告,要求对程序结构做出详细的解释。
四、实验学时:2学时五、实验步骤:1.进入Java编程环境;2. 新建一个Java文件,命名为inOut.java;3. 定义主方法static public void main(String[ ] args);在方法体中调用System.in.read方法接收一个键盘输入的字符;调用System.out.println()方法,使程序输出一条问候信息;4. 编译运行程序,观察运行情况和输出结果。
(如果使用JDK环境,调用javac.exe和java.exe编译和执行程序)六、选作实验编写Java Applet程序实现上述功能。
实验二 Java语言基础训练一、实验目的:熟悉Java基本语法,练习使用Java的数据类型和控制结构,实现简单的数据和字符串操作。
二、实验内容:编写Java程序,输出1900年到2000年之间的所有润年。
三、实验要求:1. 正确使用Java语言的控制结构;2. 从屏幕输出1900年到2000年之间的所有润年;3. 写出实验报告。
四、实验学时:2学时五、实验步骤:1.进入Java编程环境;2. 新建一个Java文件,命名为runY ear.java;3. 定义主方法,查找1900到2000年之间的润年,并输出它们;4. 编译运行程序,观察输出结果是否正确。
六、选作实验编写程序,查找一个字符串中是否包含指定的字符。
实验三面向对象基础熟悉Java类的结构,掌握类的定义、方法和属性的定义以及对象的实现,掌握类的继承。
java实验报告 类的继承

java实验报告类的继承一、实验目的1、深入理解 Java 中类的继承概念和机制。
2、掌握通过继承实现代码复用和功能扩展。
3、学会使用 super 关键字和方法重写。
二、实验环境1、操作系统:Windows 102、开发工具:Eclipse 或 IntelliJ IDEA三、实验内容1、创建一个父类`Animal`包含属性:`name`(字符串类型),`age`(整数类型)包含方法:`eat()`(打印出动物正在吃东西),`sleep()`(打印出动物正在睡觉)2、创建子类`Dog` 继承自`Animal`新增属性:`breed`(字符串类型,表示狗的品种)重写父类的`eat()`方法,打印出狗吃东西的特有方式新增方法:`bark()`(打印出狗在叫)3、创建子类`Cat` 继承自`Animal`新增属性:`color`(字符串类型,表示猫的颜色)重写父类的`sleep()`方法,打印出猫睡觉的特有姿势新增方法:`meow()`(打印出猫在叫)四、实验步骤1、在 Java 项目中创建一个名为`Animal` 的类:```javapublic class Animal {private String name;private int age;public Animal(String name, int age) {thisname = name;thisage = age;}public void eat(){Systemoutprintln(name +" is eating");}public void sleep(){Systemoutprintln(name +" is sleeping");}}```2、创建一个名为`Dog` 的类继承自`Animal`:```javapublic class Dog extends Animal {private String breed;public Dog(String name, int age, String breed) {super(name, age);thisbreed = breed;}@Overridepublic void eat(){Systemoutprintln(supergetName()+" the "+ breed +" dog is eating greedily");}public void bark(){}}```3、创建一个名为`Cat` 的类继承自`Animal`:```javapublic class Cat extends Animal {private String color;public Cat(String name, int age, String color) {super(name, age);thiscolor = color;}@Overridepublic void sleep(){Systemoutprintln(supergetName()+" the "+ color +" cat is sleeping curled up");}public void meow(){}}```4、在`Main` 类中进行测试:```javapublic class Main {public static void main(String args) {Dog dog = new Dog("Buddy", 3, "Labrador");dogeat();dogbark();dogsleep();Cat cat = new Cat("Mimi", 2, "White");cateat();catsleep();catmeow();}}```五、实验结果1、运行`Main` 类后,输出结果如下:`Buddy the Labrador dog is eating greedily``Buddy is barking``Buddy is sleeping``Mimi is eating``Mimi the White cat is sleeping curled up``Mimi is meowing`2、可以看到,子类`Dog` 重写了父类`Animal` 的`eat()`方法,展示了狗特有的吃东西方式;子类`Cat` 重写了父类的`sleep()`方法,展示了猫特有的睡觉姿势。
Java实验

实验一熟悉Java开发环境(验证性2学时)一、实验目的与要求:1 掌握Java Application程序的开发过程并编写第一个Java Application程序*2 掌握Java Applet程序的开发过程并编写第一个Java Applet程序。
*3 练习简单的HTML文件配合Java Applet使用。
4 熟悉jdk的使用二、实验内容:1 编写一个Java Application程序,在屏幕上显示”This is my first java program!”.*2 编写第一个Java Applet 程序,在屏幕上显示”This is my first Java Applet!”三、实验步骤:1、jdk的使用jdk开发工具包可以从网站下载,jdk不是集成编译环境,须手动运用命令行程序进行编译和解释执行1)编辑.java文件可以在记事本或其他纯文本编辑器中编辑,保存时把文件扩展名定为.java即可,当然要注意文件名命名的要求2)编译生成.class文件进入dos系统进行编译(如图1.1所示),格式如javac MyFirstApplication.java,要注意图1.1进入命令行看javac.exe的路径并且MyFirstApplication.java文件路径和javac.exe路径一样。
编译成功后就能在浏览器中看见多了一个MyFirsApplication.class或更多的.class文件。
如图1.2所示图1.2使用jdk编译MyFirstApplication.java文件3)解释执行Application程序:同样是在dos系统下解释执行,格式如java MyFirstApplication,注意.class后缀别加,如图1.3所示。
图1.3解释执行MyFirstApplication.class程序* applet程序进入dos系统进行编译,格式如javac MyFirstApplet.java,要注意看javac.exe的路径并且MyFirstApplet.java文件路径和javac.exe路径一样。
实验5 类和对象1答案

实验5:Java类与对象一、实验目的(1)使用类来封装对象的属性和功能;(2)掌握Java类的定义。
(3)java对象的使用二、实验任务(1)按要求编写一个Java程序。
(2)按要求完善一个Java程序,编译、运行这个程序,并写出运行结果。
三、实验内容1.编写一个Java程序片断,以定义一个表示学生的类Student。
这个类的属性有“学号”、“班号”、“姓名”、“性别”、“年龄”,方法有“获得学号”、“获得班号”、“获得性别”、“获得姓名”、“获得年龄”、“获得年龄”。
2.为类Student增加一个方法public String toString( ),该方法把Student类的对象的所有属性信息组合成一个字符串以便输出显示。
编写一个Java Application程序,创建Student类的对象,并验证新增加的功能。
class Student{long studentID;int classID;String name;String sex;int age;public Student(long studentID,int classID,String name,String sex,int age){ this.studentID=studentID;this.classID=classID;=name;this.sex=sex;this.age=age;}public long getStudentID(){return studentID;}public int getClassID(){return classID;}public String getName(){return name;}public String getSex(){return sex;}public int getAge(){return age;}public String toString(){return "学号:"+getStudentID()+"\n班号:"+getClassID()+"\n姓名:"+getName()+"\n性别:"+getSex()+"\n年龄:"+getAge();}}public class StudentDemo{public static void main(String[] args){Student s1=new Student(90221,2,"Tom","male",20);System.out.println(s1.toString());}}运行结果:学号:90221班号:2姓名:Tom性别:male年龄:202.程序填空,程序中包含以下内容:一个学生类(Student),包含:属性:学号s_No,姓名s_Name,性别s_Sex,年龄s_Age。
Java中常用的集合类有哪些?它们的使用场景是什么?

Java中常用的集合类有哪些?它们的使用场景是什么?Java作为目前最为流行的编程语言之一,其优越的面向对象编程思想和强大的类库使其成为了广大编程爱好者和专业开发者的首选语言之一。
在Java开发中,常用的集合类具有广泛的应用场景,可以大大简化我们代码的编写和维护。
在本篇文章中,我们将介绍Java中常用的集合类有哪些,它们的使用场景是什么,以及如何选择合适的集合类来应对各种场景。
一、Java中常用的集合类Java中常用的集合类包括List、Set、Map等,具体如下:1.ListList是Java中最基础和最常用的集合类之一,它是一个有序的集合,可以存储重复的元素。
List提供了一系列的方法用来操作列表中的元素,如添加、删除、获取、修改等。
常见的List有ArrayList 和LinkedList。
2.SetSet是Java中的另一个基础集合类,它是一个无序的集合,不允许存储重复的元素。
Set提供了一系列的方法用来操作集合中的元素,如添加、删除、获取等。
常见的Set有HashSet、TreeSet。
3.MapMap是Java中常用的映射关系集合,它存储键值对,支持通过键来访问值。
Map提供了一系列的方法用来操作映射关系,如添加、删除、获取、修改等。
常见的Map有HashMap、TreeMap、ConcurrentHashMap等。
二、Java中常用集合类的使用场景不同的集合类有不同的使用场景,我们需要根据具体的业务需求来选择合适的集合类。
下面我们来介绍几种常见的使用场景及其对应的集合类。
1.需要随机访问元素的情况:ArrayListArrayList是Java中常用的集合类之一,它支持随机访问,通过索引访问元素的时间复杂度为O(1),是处理元素数量较大的情况下的较好选择。
2.需要频繁插入或删除元素的情况:LinkedListLinkedList是另一个常用的集合类,它支持快速的插入和删除操作,通过节点互相关联实现。
java类与对象实验报告

java类与对象实验报告Java类与对象实验报告一、引言Java是一种面向对象的编程语言,类和对象是Java编程的基本概念。
在本次实验中,我们将学习和掌握Java类和对象的概念、定义和使用方法,并通过实际的编程实验来加深对这些概念的理解。
二、实验目的1. 理解Java类和对象的概念;2. 学会定义和使用Java类;3. 掌握创建和操作Java对象的方法;4. 熟悉Java类和对象的相关语法和规范。
三、实验过程1. 类的定义在Java中,类是对象的模板,用于描述对象的属性和行为。
我们首先需要定义一个类,以便创建对象。
类的定义包括类名、属性和方法。
属性即对象的特征,方法即对象的行为。
2. 对象的创建通过使用关键字"new",我们可以创建一个类的对象。
对象是类的实例化,每个对象都有自己的属性和方法。
3. 对象的属性和方法对象的属性和方法可以通过对象名加点操作符来访问。
属性可以是基本类型或其他类的对象,方法可以是对象的行为或功能。
4. 构造方法构造方法是一种特殊的方法,用于创建对象时进行初始化操作。
构造方法的名称必须与类名相同,没有返回类型,可以有参数。
5. 封装性封装性是面向对象编程的重要特性之一,它将数据和方法封装在类中,对外部隐藏内部实现细节。
通过使用访问修饰符(private, public, protected)来控制属性和方法的访问权限。
6. 继承继承是面向对象编程的另一个重要特性,它允许我们创建一个新类,并从现有类中继承属性和方法。
通过使用关键字"extends"来实现继承。
7. 多态性多态性是面向对象编程的核心概念之一,它允许我们使用一个父类类型的引用来引用子类的对象。
通过方法的重写和重载来实现多态性。
四、实验结果通过本次实验,我们成功定义了一个Java类,并创建了多个对象。
我们可以通过对象名来访问对象的属性和方法,并对其进行操作。
我们还学习了构造方法的使用,以及封装性、继承和多态性的相关概念。
java实验报告

java实验报告Java 实验报告一、实验目的本次 Java 实验的主要目的是通过实际编程操作,深入理解和掌握Java 语言的基本语法、面向对象编程的概念和方法,以及常用类库的使用。
同时,培养我们的问题解决能力、逻辑思维能力和代码规范意识,为今后的软件开发工作打下坚实的基础。
二、实验环境1、操作系统:Windows 102、开发工具:Eclipse IDE for Java Developers3、 JDK 版本:JDK 18三、实验内容本次实验共包括以下三个部分:1、 Java 基本语法练习变量与数据类型运算符与表达式控制流语句(ifelse、for、while、dowhile)数组2、面向对象编程实践类与对象的定义和使用构造函数与方法重载封装、继承与多态抽象类与接口3、 Java 常用类库应用String 类与字符串操作集合框架(ArrayList、HashMap)文件输入输出(File、FileReader、FileWriter)四、实验步骤及代码实现1、 Java 基本语法练习变量与数据类型:```javapublic class VariableDataType {public static void main(String args) {int age = 20;double salary = 50005;String name ="张三";boolean isStudent = true;Systemoutprintln("年龄:"+ age);Systemoutprintln("工资:"+ salary);Systemoutprintln("姓名:"+ name);Systemoutprintln("是否是学生:"+ isStudent);}}```运算符与表达式:```javapublic class OperatorExpression {public static void main(String args) {int num1 = 10;int num2 = 5;int sum = num1 + num2;int difference = num1 num2;int product = num1 num2;int quotient = num1 / num2;int remainder = num1 % num2; Systemoutprintln("和:"+ sum);Systemoutprintln("差:"+ difference);Systemoutprintln("积:"+ product);Systemoutprintln("商:"+ quotient);Systemoutprintln("余数:"+ remainder);}}```控制流语句:```javapublic class ControlFlowStatement {public static void main(String args) {// ifelse 语句int score = 80;if (score >= 90) {Systemoutprintln("优秀");} else if (score >= 80) {Systemoutprintln("良好");} else if (score >= 70) {Systemoutprintln("中等");} else if (score >= 60) {Systemoutprintln("及格");} else {Systemoutprintln("不及格");}// for 循环for (int i = 1; i <= 5; i++){Systemoutprintln("第" + i +"次循环");}// while 循环int j = 1;while (j <= 5) {Systemoutprintln("第" + j +"次 while 循环");j++;}// dowhile 循环int k = 1;do {Systemoutprintln("第" + k +"次 dowhile 循环");k++;} while (k <= 5);}}```数组:```javapublic class ArrayExample {public static void main(String args) {//一维数组int numbers ={1, 2, 3, 4, 5};for (int num : numbers) {Systemoutprintln(num);}//二维数组int matrix ={{1, 2, 3},{4, 5, 6},{7, 8, 9}};for (int row : matrix) {for (int num : row) {Systemoutprint(num +"");}Systemoutprintln();}}}```2、面向对象编程实践类与对象的定义和使用:```javapublic class Person {private String name;private int age;public Person(String name, int age) {thisname = name;thisage = age;}public void showInfo(){Systemoutprintln("姓名:"+ name +",年龄:"+ age);}public static void main(String args) {Person person1 = new Person("张三", 20);person1showInfo();}}```构造函数与方法重载:```javapublic class ConstructorOverloading {private String name;private int age;public ConstructorOverloading(String name) {thisname = name;}public ConstructorOverloading(String name, int age) {thisname = name;thisage = age;}public void showInfo(){if (age == 0) {Systemoutprintln("姓名:"+ name);} else {Systemoutprintln("姓名:"+ name +",年龄:"+ age);}}public static void main(String args) {ConstructorOverloading person1 = new ConstructorOverloading("张三");person1showInfo();ConstructorOverloading person2 = new ConstructorOverloading("李四", 25);person2showInfo();}}```封装、继承与多态:```java//父类class Animal {private String name;public Animal(String name) {thisname = name;}public void eat(){Systemoutprintln(name +"正在吃东西");}}//子类继承父类class Dog extends Animal {public Dog(String name) {super(name);}public void bark(){Systemoutprintln(name +"在叫");}}public class InheritancePolymorphism {public static void main(String args) {Animal animal = new Dog("小黑");animaleat();//向下转型为 Dog 类型调用 bark 方法if (animal instanceof Dog) {Dog dog =(Dog) animal;dogbark();}}}```抽象类与接口:```java//抽象类abstract class Shape {abstract void draw();}//实现抽象类的子类class Circle extends Shape {@Overridevoid draw(){Systemoutprintln("画一个圆");}}//接口interface Moveable {void move();}//实现接口的类class Car implements Moveable {@Overridepublic void move(){Systemoutprintln("汽车在移动");}}public class AbstractInterfaceExample {public static void main(String args) {Shape shape = new Circle();shapedraw();Moveable car = new Car();carmove();}}```3、 Java 常用类库应用String 类与字符串操作:```javapublic class StringOperation {public static void main(String args) {String str1 ="Hello, ";String str2 ="World!";String str3 = str1 + str2;Systemoutprintln(str3);int length = str3length();Systemoutprintln("字符串长度:"+ length);char charAt = str3charAt(5);Systemoutprintln("第 5 个字符:"+ charAt);boolean contains = str3contains("World");Systemoutprintln("是否包含 World:"+ contains);}}```集合框架(ArrayList、HashMap):```javaimport javautilArrayList;import javautilHashMap;import javautilMap;public class CollectionFramework {public static void main(String args) {// ArrayListArrayList<String> names = new ArrayList<>();namesadd("张三");namesadd("李四");namesadd("王五");for (String name : names) {Systemoutprintln(name);}// HashMapHashMap<String, Integer> ages = new HashMap<>();agesput("张三", 20);agesput("李四", 25);agesput("王五", 30);for (MapEntry<String, Integer> entry : agesentrySet()){Systemoutprintln(entrygetKey()+"的年龄是" +entrygetValue());}}}```文件输入输出(File、FileReader、FileWriter):```javaimport javaioFile;import javaioFileReader;import javaioFileWriter;import javaioIOException;public class FileIOExample {public static void main(String args) {//写入文件try (FileWriter writer = new FileWriter("outputtxt")){writerwrite("这是写入文件的内容");} catch (IOException e) {eprintStackTrace();}//读取文件try (FileReader reader = new FileReader("outputtxt")){int character;while ((character = readerread())!=-1) {Systemoutprint((char) character);}} catch (IOException e) {eprintStackTrace();}}}```五、实验结果与分析1、 Java 基本语法练习变量与数据类型:能够正确定义和使用各种数据类型的变量,并进行基本的运算和输出。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验5 Java类库和常用类(一)一. 实验目的及实验环境1理解类库的概念,掌握分析、应用类库中的类的方法。
2熟练掌握Math类的常用方法。
熟悉Random类的常用方法。
3理解String类的特性,熟练掌握String类的常用方法。
4能用Date类创建对象,熟练掌握Date类的常用方法。
5熟练掌握SimpleDateFormat解析日期和设置日期输出格式。
6学会查阅Java API在线参考文档和离线文档的方法。
二. 实验内容1 基本内容(实验前请及时熟悉如下相关内容)1)练习使用Math类的常用方法。
2)应用String类编程练习。
3)编写程序应用Random类生成随机数。
4)练习使用Date类的常用方法。
5)查阅Java API在线参考文档和下载Java API离线文档。
示例1. 应用SimpleDateFormat类的程序示例如下,共同学们模仿参考。
import java.text.*;import java.util.Date;public class FormatDateTime {public static void main(String[] args) {SimpleDateFormat myFmt = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");SimpleDateFormat myFmt1 = new SimpleDateFormat("yy/MM/dd HH:mm");SimpleDateFormat myFmt2 = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");// 等价于now.toLocaleString()SimpleDateFormat myFmt3 = new SimpleDateFormat("yyyy年MM月dd日 HH 时mm分ss秒 E ");SimpleDateFormat myFmt4 = new SimpleDateFormat("一年中的第 D 天一年中第w个星期一月中第W个星期在一天中k时 z时区");Date now = new Date();//当前时间System.out.println(myFmt.format(now));System.out.println(myFmt1.format(now));System.out.println(myFmt2.format(now));System.out.println(myFmt3.format(now));System.out.println(myFmt4.format(now));System.out.println(now.toGMTString());//The method toGMTString() from the type Date is deprecated.System.out.println(now.toLocaleString());System.out.println(now.toString());}}示例2. 应用GregorianCalendar类的程序示例如下,共同学们模仿参考。
import java.util.GregorianCalendar;import java.util.Date;import java.text.DateFormat;//public class DateExample5 {public class TestGregorian {public static void main(String[] args) {DateFormat dateFormat =DateFormat.getDateInstance(DateFormat.FULL);// Create our Gregorian Calendar.GregorianCalendar cal = new GregorianCalendar();// Set the date and time of our calendar// to the system´s date and timecal.setTime(new Date());System.out.println("System Date: " +dateFormat.format(cal.getTime()));// Set the day of week to FRIDAYcal.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.FRIDAY);System.out.println("After Setting Day of Week to Friday: " + dateFormat.format(cal.getTime()));int friday13Counter = 0;while (friday13Counter <= 10) {// Go to the next Friday by adding 7 days.cal.add(GregorianCalendar.DAY_OF_MONTH, 7);// If the day of month is 13 we have// another Friday the 13th.if (cal.get(GregorianCalendar.DAY_OF_MONTH) == 13) {friday13Counter++;System.out.println(dateFormat.format(cal.getTime()));}}}}2 综合实验:2.1 (Y. Daniel Liang 英文版第10版P360:*9.3 或英文版八版P296:8.3*) (Using the Date class) Write a program that creates a Date object, sets its elapsed time to 10000, 100000, 10000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, and displays the date and time using the toString() method, respectively.2.2(Y. Daniel Liang 英文版第10版P360:*9.4 或英文版八版P296:8.4*) (Using the Random class) Write a program that creates a Random object with seed 1000 and displays the first 50 random integers between 0 and 100 using the nextInt(100) method.2.3 (Y. Daniel Liang 英文版第10版P361:*9.5 或英文版八版P296:8.5*) (Using the GregorianCalendar class) Java API has the GregorianCalendar class in the java.util package that can be used to obtain the year, month, and day of a date. The no-arg constructor constructs an instance for the current date, and the methodsget(GregorianCalendar.YEAR), get(GregorianCalendar.MONTH), andget(GregorianCalendar.DAY_OF_MONTH) return the year, month, and day.Write a program to perform two tasks:■Display the current year, month, and day.■The GregorianCalendar class has the setTimeInMillis(long), which can be used to set a specified elapsed time since January 1, 1970. Set the value to 1234567898765L and display the year, month, and day.2.4(Y. Daniel Liang英文版第10版P284:**7.34 或英文版八版P337:9.11**) (Sorting characters in a string) Write a method that returns a sorted string using the following header:public static String sort(String s)For example, sort("acb") returns abc.Write a test program that prompts the user to enter a string and displays the sorted string.2.5 (Y. Daniel Liang英文版八版P201:*5.50)(Count uppercase letters) Write a program that prompts the user to enter a string and displays the number of the uppercase letters in the string.或(Y. Daniel Liang英文版八版P338:9.15*) (Finding the number of uppercase letters in a string) Write a program that passes a string to the main method and displays the number of uppercase letters in a string.2.6附加题(供学有余力同学选做,平时成绩有加分!)(Y. Daniel Liang英文版第10版P238:**6.18 或英文版八版P336:9.3**) (Checking password) Some Websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rule is as follows: ■A password must have at least eight characters.■A password consists of only letters and digits.■A password must contain at least two digits.Write a program that prompts the user to enter a password and displays "Valid Password" if the rule is followed or "Invalid Password" otherwise.。