一、对象的赋值与比较
1.实验目的
(1)学习对象的赋值
(2)学习对象的比较
2.实验要求
编写一个Java程序,在程序中定义一个StudentA类,生成StudentA类的两个对象,分别对这些对象进行赋值,再进行比较,使程序运行结果如图 6.8所示。
3.程序模板
按模板要求,将[代码1]~[代码4]替换为相应的Java序代码使之输出如图68所示的结果。
//文件名:StudentA.java
public class StudentA
String strName;public static void main(String[] args)
StudentA a = new StudentA();a.setName("张三");
StudentA b=a;
b.setName("李四");
StudentA c = new StudentA();
c.setName("王五");
System.out.println("a的名字是”+agetName());
System.out.println("b的名字是”+b.getName());
System.out.printn("c的名字是”+cgetName());
[代码1]//比较对象a与b,根据比较结果,显示a与b相等或不相等//比较对象a与c,根据比较结果,显示a与c相等或不相等[代码2]
[代码3[代码4]
//定义一个公共方法setName(String name),用于设置属性strName的值//定义一个公共方法getName(),用于返回属性strName的值
}
4.代码
public class java7 {
String strName;
public static void main(String args[]) {
StudentA a = new StudentA();
a.setName("张三");
StudentA b = a;
b.setName("李四");
StudentA c = new StudentA();
c.setName("王五");
System.out.println("a 的名字是" + a.getName());
System.out.println("b 的名字是" + b.getName());
System.out.println("c 的名字是" + c.getName());
if (a == b) {
System.out.println("a 与b 相等");
} else System.out.println("a 与 b 不相等");
if (a == c) {
System.out.println("a 与 c 相等");
} else System.out.println("a 与c 不相等");
}
public void setName(String name) {
strName = name;
}
public String getName(){
return strName;
}
}
5.代码运行
二、类的继承
1.实验目的
(1)学习类的继承的语法。
(2)学习在子类的对象中调用父类定义的成员方法。
(3)理解所有类都是java.lang.bject类的子类或子孙类。
2.实验要求
编写一个Java程序,在程序中定义一个 PersonA类,定义一个 PersonA类的子类StudentA类,再定义一个C1类,在main()方法中,生成StudentA的对象,使程序运行结果如图7.1所示。
3.代码
class PersonA{
private String name;
public void setName(String newName){
name=newName;
}
public String getName(){
return name;
}
}
class StudentA extends PersonA{
private String department;
public void setDepartment(String newDepartment){
department=newDepartment;
}
public String getDepartment(){
return department;
}
}
public class C1{
public static void main(String[] args){
StudentA s1=new StudentA();
s1.setName("张三");
s1.setDepartment("计算机");
System.out.println("你好,我是"+s1.getName());
System.out.println("我是"+s1.getDepartment()+"的学生");
}
}
三、子类调用父类的方法
1.实验目的
(1)学习在类的继承中,子类与父类构造方法的继承关系
(2)掌握在子类的方法中调用父类定义的方法。
(3)掌握在子类中调用父类构造方法的规则
2.实验要求
编写一个Java程序在程序中定义一个PersonB类定义一个PersonB类的子类StudentB类,再定义一个C2类,在main()方法中生成StudentB类的两个对象,使程序运行结果如图所示。
3.代码
import javax.swing.plaf.PanelUI;
class PersonB
{
String name;
int age;
public PersonB()
{
System.out.println("PersonB()被调用");
}
public PersonB(String newName)
{
name=newName;
System.out.println("PersonB(String newName)被调用");
}
public void introuduce()
{
System.out.println("我是"+name+",今年"+age+"岁");
}
}
class StudenB extends PersonB
{
public StudenB()
{
System.out.println("StudenB()被调用");
}
public StudenB(String newName,int newAge)
{
super(newName);
age=newAge;
}
}
public class C2
{
public static void main(String[] args)
{
StudenB s1=new StudenB();
StudenB s2=new StudenB("张三",19);
s2.introuduce();
}
}
四、抽象类
1.实验目的
(1)学习Java语言抽象类的语法。
(2)学习在子类中实现父类中的抽象方法
2.实验要求
编写一个Java程序,在程序中定义一个抽象类Shape,再定义两个Shape类的子类Rectangle Circle类,在子类中实现父类的抽象方法,使程序运行结果如图7.4所示。
3.代码
abstract class Shape{
public abstract float area();
public abstract void printArea();
}
class Rectangle extends Shape{
int width;
int length;
public Rectangle(int newWidth,int newLength){
width=newWidth;
length=newLength;
}
public float area(){
return width*length;
}
public void printArea(){
System.out.println("我是个矩形,我的面积是:"+area());
}
}
class Circle extends Shape{
final float pi=3.14F;
int radius;
public Circle(int newRadius){
radius=newRadius;
}
public float area(){
return pi*radius*radius;
}
public void printArea(){
System.out.println("我是个圆形,我的面积是:"+area());
}
}
class ChouXiang{
public static void main(String[] args){
Rectangle s1= new Rectangle(3,4);
Circle s2=new Circle(2);
s1.printArea();
s2.printArea();
}
}
五、Java 常见的异常类
1.实验目的
(1)认识程序的逻辑错误。
(2)加深对异常机制的理解(3)认识Java语言中的常见异常类。(4)进一步理解Java 语言的异常处理机制。
2.实验要求
编写一个Java程序,在 main()方法中,对可能产生错误的语捕获相应的异常,使程序运行结果如图所示。
3.代码
public class otherexception
{
public static void main(String[] args)
{
try
{
int a[]=null;
a[0]=1;
}
catch(NullPointerException e)
{
System.out.println("空指针异常");
}
try
{
String str=null;
str.length();
}
catch(NullPointerException e)
{
System.out.println("空指针异常");
}
try
{
Object obj=new Object();
String str=(String)obj;
}
catch(ClassCastException e)
{
System.out.println("类型强制转化异常");
}
try
{
int a[]=new int[-1];
}
catch(NegativeArraySizeException e)
{
System.out.println("数组负下标异常");
}
try
{
int a[]=new int[1];
a[0]=0;
a[1]=1;
}
catch(IndexOutOfBoundsException e)
{
System.out.println("数组下标越界异常");
}
}
}
六、主动抛出异常
1.实验目的
(1)学习定义自己的异常类。
(2)学习在方法体中使用 throw 语句抛出异常。
2.实验要求
编写一个Java程序程序中定义两个异常类在main()方法中使用throw语句抛出异常,使程序运行结果如图所示。
3.代码
class AaaException extends Exception{}
class BbbException extends Exception{}
class ThrowException
{
public static void main(String args[])
{
int x=1;
{
try{
if(x>0){
throw new AaaException();
} else
{
throw new BbbException();
}
}
catch(AaaException e)
{
System.out.println("执行aaa异常处理程序");
}
catch(BbbException e)
{
System.out.println("执行bbb异常处理程序");
}
}
}
}
七、FileOutputStream 类的应用
1.实验目的
(1)学习 FileOutputStream 类的语法格式。
(2)学习 FileOutputStream类的使用。
2.实验要求
编写一个Java程序把文件myfiletxt内容复制到文件yourfiletxt文件中使序运行结果如图所示。
3.代码
import java.io.*;
public class CopyFile
{
public static void main(String[] args) throws IOException
{
int i;
FileInputStream fin;
FileOutputStream fout;
fin=new FileInputStream("d:\\myfile.txt");
fout=new FileOutputStream("d:\\yourfile.txt");
do
{
i = fin.read();
if (i != -1)
fout.write(i);
} while (i != -1);
fin.close();
fout.close();
System.out.println("myfile.txt 内容已经被复制到 yourfile.txt 文件中");
}
}
八、文件操作
1.实验目的
(1)学习 File类的使用。
(2)学习在程序中新建文件。
(3)学习在程序中对文件读取和写人操作。
(4)学习在程序中获取文件信息。
(5)学习在程序中查看目录内容
(6)学习在程序中删除文件。
2.实验要求
编写六个 Java 程序,实现文件的常见操作3序模板(1)新建文件程序 Filel.java,序功能是创建新的文件。
File1.java
public class File1
{
public static void main(String[] args)
{
if (args.length==0)
{
System.out.println("未输入任何附带参数");
}else{
for (int i=0;i<args.length ; i++)
{
System.out.println(args[i]);
}
}
}
}
File2.java
import java.io.*;
public class File2
{
public static void main(String[] args) throws IOException
{
BufferedWriter out = new BufferedWriter(new FileWriter("d:\\a.txt"));
out.write("岭南师范学院");
out.newLine();
out.write("Java 程序设计");
out.flush();
out.close();
}
}
File3.java
import java.io.*;
public class File3{
public static void main(String[] args)throws IOException{
String thisLine;
BufferedReader in = new BufferedReader(new FileReader("d:\\a.txt")); // 创建缓存区字符输入流,需要传如Reader对象
while((thisLine = in.readLine()) != null) // 每次读取一行,直到文件结束
System.out.println(thisLine);
in.close();
}
}
File4.java
import java.io.*;
import java.util.*;
public class File4
{
public static void main(String []args) throws IOException
{
if(args.length==0)
{
System.out.print("缺少文件名");
System.exit(1);
}
for(int i=0; i<args. length; i++)
status(args[i]);
}
public static void status(String fileName) throws IOException
{
System.out.println("------"+fileName+"------");
File f = new File(fileName); // 创建 File 类对象
if(!f.exists())
{ // 测试文件是否存在
System.out.println("文件没收找到"+"\n");
return;
}
System.out.println("文件全名为:"+f.getCanonicalPath());
String p = f.getParent();
if(p!=null)
{
System.out.println("Parent directory: "+p); // 显示文件的父目录
}
if(f.canRead()){
System.out.println("File is readable."); // 测试文件是否可读
}
if(f.canWrite())
{ // 测试文件是否可写
System.out.println("File is writable.");
}
Date d = new Date();
d.setTime(f.lastModified());
System.out.println("Last modifiled : " + d);
if(f.isFile())
{
System.out.println("文件大小是: "+f.length()+"bytes");
}else if(f.isDirectory()){
System.out.println("它是目录");
}else{
System.out.println("既不是文件也不是目录");
}
System.out.println();
}
}
File5.java
import java.io.File;
public class File5
{
public static void main(String[] args) {
//查看当前目录内容
String[] dir = new java.io.File(".").list();
//将当前目录下的文件存入数组dir中
java.util.Arrays.sort(dir);
for(int i = 0;i<dir.length;i++) {
System.out.println(dir[i]);
}
//查看系统驱动器列表
File[] drives = File.listRoots();
for(int i =0;i<drives.length;i++) {
System.out.println(drives[i]);
}
}
}
File6.java
import java.io.File;
public class File6 {
public static void main(String[] args) {
File target = new File("d://a.txt");
if(!target.exists())
System.out.println("文件不存在");
else if(target.delete())
System.out.println("文件被删除了");
else
System.out.println("文件不能被删除");
}
}
九、实验实现Runnable接口创建线程
1.实验目的
(1)了解Runnable接口。
(2)学习通过Runnable接口来创建线程
2.实验要求
编写一个Java程序定义一个类ThreadB实现Runnable接口,在main()方法中创建Thread类的三个实例,执行这些线程,使程序运行结果如图所示
3.代码
public class ThreadB implements Runnable
{
int count = 1;
int num;
public ThreadB(int newNum)
{
num = newNum;
System.out.println("创建线程"+num);
}
public void run()
{
while(true)
{
System.out.println("线程"+num+":计数"+count);
count++;
if(count==3)
break;
}
}
public static void main(String[] args)
{
Thread a1 = new Thread(new ThreadB(1));
Thread a2 = new Thread(new ThreadB(2));
Thread a3 = new Thread(new ThreadB(3));
a1.start();
a2.start();
a3.start();
System.out.println("主方法main()运行结束");
}
}
十、类作为类型实参的泛型应用
1.实验目的
(1)学习泛型类对象如何调用Objecr 类的方法。
(2)以类为类型实参创建泛型对象。
2.实验要求
分别声明一个泛型类Cylinder一个圆形类Circle和一个矩形类Rectangle。在主类中分别以类Circle 和类 Rectangle 为类型实参创建泛型对象,然后计算各自图形的面积,并调用泛型类的方法输出各自的计算结果,使程序运行结果如图所示。
3.代码
public class Cylinder<E>{
E bottom;
double height;
public Cylinder(E bottom,double height){
this.bottom=bottom;
this.height=height;
}
public double volume(){
String s = bottom.toString();
double area=Double.parseDouble(s);
return area*height;
}
public static void main(String[] args)
{
Rectangle rect = new Rectangle(8,5);
Cylinder <Rectangle> c1=new Cylinder<Rectangle>(rect,12);
System.out.println("显示长方体的体积"+c1.volume());
Circle circle =new Circle(5);
Cylinder <Circle> c2=new Cylinder<Circle>(circle,7);
System.out.println("显示圆柱体的体积"+c2.volume());
}
}
class Circle
{
double radius,area;
public Circle(double radius)
{
this.radius=radius;
}
public String toString()
{
area = 3.14 * radius *radius;
return ""+area;
}
}
class Rectangle
{
double length, width,area;
public Rectangle(double length,double width)
{
this.length=length;
this.width=width;
}
public String toString()
{
area=length * width;
return ""+area;
}
}