资源描述
《Java语言和WWW技术》课程
实验报告三
姓名:穆岩
学号:1111001200159
实验题1
[实验要求]
• 修改程序,使得在子类中可以对两个f分别进行赋值和显示操作
• 记录调试过程、步骤、程序和运行结果
[实验程序]
class Parent{
public int f;
public void setF(int f){ this.f=f; }
public int getF( ) { return f; }
}
public class Child extends Parent{
public int f;
public double d;
public static void main(String [] args){
Child c = new Child();
c.f=100;
c.setF(30);
System.out.println(" Parent: " + c.getF());
System.out.println(" Child: " + c.f);
}
}
[运行结果]
[实验结论与收获]
实验题2
[实验要求]
• 完成程序,使用super创建重写的方法
• 调试并记录结果
[实验程序]
public class Employee {
public String name="zhang";
public double salary=2000;
public String getDetails() {
return "Name:" + name + "\n" + "Salary:" + salary;
}
}
public class Manager extends Employee {
public String department="Office";
public String getDetails() {
return super.getDetails()+ "\n" + " Department: " + department;
}
}
public class TestOverride1{
public static void main(String args[ ]) {
Employee e = new Employee();
Manager m = new Manager();
System.out.println("Employee:"+"\n"+e.getDetails());
System.out.println("Manager:"+"\n"+m.getDetails());
}
}
[运行结果]
[实验结论与收获]
掌握使用super创建重写的方法
实验题3
[实验要求]
• 修改并完成程序
• 说明程序错在哪里,如何修改
• 调试并记录结果
[实验程序]
class Parent {
public void doSomething() {}
}
class Child extends Parent {
public void doSomething() {}
}
public class TestOverride2 {
public static void main(String[] args) {
Parent p1 = new Parent();
Parent p2 = new Child();
p1.doSomething();
p2.doSomething();
}
}
[运行结果]
[实验结论与收获]
Child类的修饰符不能是private,只能是public。原因是只能扩大,不能缩小。
主方法定义有错误,应改为public static void main(String[] args)
实验题4
[实验要求]
• 执行程序记录程序结果
[实验程序]
class Count {
public static int counter;
static {
counter = 100;
System.out.println("in static block!");
}
}
public class Test {
public static void main(String[] args) {
System.out.println("counter = "+ Count.counter);
}
}
[运行结果]
[实验结论与收获]
生成类对象时所设置的打印只打印一次。
实验题5
[实验要求]
• 执行程序,修改程序中错误
• 说明错误原因及解决方法
• 记录修改后程序和运行结果
[实验程序]
import java.util.*;
public class Employee {
private static final double SALARY = 2000.00;
private String name;
private double salary;
private Date birthDate;
public Employee() {
}
public Employee(String name, double salary, Date date) {
this.name = name;
this.salary = salary;
this.birthDate = date;
}
public Employee(String name, double salary) {
this(name, salary, null);
}
public Employee(String name, Date date) {
this(name, SALARY, date);
}
public Employee(String name) {
this(name, SALARY);
}
}
public class Manager extends Employee {
private String department;
public Manager(String name, double salary , String dept) {
super(name, salary);
department = dept;
}
public Manager(String name, String dept) {
super(name);
department = dept;
}
public Manager(String dept) {
department = dept;
}
public Manager() {
}
}
public class TestSuper {
public static void main(String args[])
{
Manager m1 = new Manager ();
Manager m2 = new Manager ("Office");
Manager m3 = new Manager ("Liu","Office");
Manager m4 = new Manager ("Liu", 4500.00 ,"Office");
}
}
[运行结果]
[实验结论与收获]
实验题6
[实验要求]
• 为本实验中程序添加异常处理机制
• 记录修改后程序和运行结果
[6-1实验程序和运行结果]
class A{}
class B extends A{}
public class ClassCastExceptionTest{
public static void main(String args[]){
try{ A a=new A();
B b=(B)a;
}
catch(Exception e){
System.out.println("无法创建对象…");
}
}
}
结果:
[6-2实验程序和运行结果]
public class ArrayIndexOutOfBoundsExceptionTest{
public static void main(String arg[]){
int a[]=new int [10];
try{ for (int i=0 ;i<=10;i++){a[i]=0;}
}
catch(Exception e){
System.out.println("被零除错误…");
}
}
}
结果:
[6-3实验程序和运行结果]
public class ArithmeticExceptionTest{
public static void main(String arg[]){
int s=100;
try{ for (int i=-3;i<5;i++){s=s/i;}
}
catch(Exception e){
System.out.println("被零除错误…");
}
}
}
结果:
[6-4实验程序和运行结果]
public class StringException{
public static void main(String args[]){
String s1=new String("aaaa bbbb cccc");
System.out.println(s1);
String s2;
try{ s2=s1.substring(30);
System.out.println(s2);
}
catch(Exception e){
System.out.println("超出字符串长度…");
}
}
}
结果
[实验结论与收获]
掌握异常处理的方法。
展开阅读全文