您好,欢迎来到九壹网。
搜索
您的当前位置:首页实验报告四

实验报告四

来源:九壹网


面向对象程序设计

(Java)

实 验 报 告

系: 数学与计算机科学学院

专 业:

班级学号:

学生姓名:

实 验 报 告

院(系): 班级 专业 实验 名称 所用 软件 实 验 目 的 或 要 求 实 验 步 骤 、 心 得 体 会 课程名称:面向对象程序设计(Java) 日期: 学号 姓名 抽象类、接口和包 JDK、记事本或EditPlus、Java IDE 实验室 计算机号 成绩评定 教师签名 1. 掌握static、this、super等关键字的使用。 2. 了解Java中包(package)、接口(interface)的作用 3. 掌握Java中的继承机制及包、接口等的设计方法 1. 解释下面的程序运行的结果输出为什么是null public class My{ String s; public void My(){ s=”Constructor”; } public void Go(){ System.out.println(s); } public static void main(String[] args){ My m=new My(); m.Go(); } } My和Go都是一种方法,调用的是第二种方法,它没有给s赋值

2.运行下面的程序,写出运行结果,写出类成员属性和实例成员属性的区别。 class MyParts { static{//初始化 先于构造方法的执行 x=10; } public static int x = 7; public int y = 3; } public class JL{ public static void main(String[] args) { MyParts a = new MyParts(); MyParts b = new MyParts(); System.out.println(\"输出一:a.x = \" + a.x); System.out.println(\"输出一:b.x = \" + b.x); a.y = 5; b.y = 6; a.x = 1; b.x = 2; System.out.println(\"输出二:a.y = \" + a.y); System.out.println(\"输出二:b.y = \" + b.y); System.out.println(\"输出三:a.x = \" + a.x); System.out.println(\"输出三:b.x = \" + b.x); } } 1) 输出一的结果是什么,它的值的来源是什么,反映了什么特性? 输出一:a.x = 7 输出一:b.x = 7 2) 输出二的结果是什么,它的值的来源是什么,反映了什么特性? 输出二:a.y = 5 输出二:b.y = 6 3) 输出三的结果是什么,它的值的来源是什么,反映了什么特性 输出三:a.x = 2 输出三:b.x = 2 3.分析下面的程序,完成后面的要求。 ClassOne.java

package com.abc.pkg1; public class ClassOne{ private char var=’a’; char getVar() {return var;} } ClassTest.java package com.aba.pkg2; import com.abc.pkg1.ClassOne; public class ClassTest extends ClassOne{ public static void main(String[] args){ char a=new ClassOne().getVar(); char b=new ClassTest().getVar(); } } 要求: 1) ClassTest.java程序编译和运行正确吗?如果错误,解释原因。 不正确 Exception in thread \"main\" java.lang.Error: Unresolved compilation problem: at ClassTest.main(ClassOne.java:12) 2) 删除ClassOne.java第一行的public,分析ClassTest.java程序有何变化? Exception in thread \"main\" java.lang.Error: Unresolved compilation problem: at ClassTest.main(ClassOne.java:12) 3) 保持ClassOne.java第一行的public,修改第四行getVar的访问范围分别为public、private、protected,分析ClassTest.java程序有何变化? 一、Exception in thread \"main\" java.lang.Error: Unresolved compilation problems: The method getVar() is undefined for the type ClassOne The method getVar() is undefined for the type ClassTest

at ClassTest.main(ClassOne.java:13) 二、Exception in thread \"main\" java.lang.Error: Unresolved compilation problems: at ClassTest.main(ClassOne.java:13) 三、Exception in thread \"main\" java.lang.Error: Unresolved compilation problems: at ClassTest.main(ClassOne.java:13) The method getVar() is undefined for the type ClassOne The method getVar() is undefined for the type ClassTest The method getVar() is undefined for the type ClassOne The method getVar() is undefined for the type ClassTest 4.定义一个类MyProgram包含两个属性,一个是private的整型属性data、一个是private的String类型的属性str,封装这两个属性的四个方法setData和getData、setStr和getStr,将这两个属性转变为字符串的方法是toStr。 编写程序:使用MyProgram类,实现数据的访问和修改,并调用toStr方法显示该类的属性。 程序如下: package package4_4; public class MyProgram { private ? data; private ? str; MyProgram(int d, String s) { data = d; str = s; } public void setData(int d){ this.data=?; } public void ?(String ?){ this.str=s; } public int ?(){

return data; } public String ?(){ return ?; } public String ?(){ return String.valueOf(data)+?; } public static void main(String args[]){ MyProgram program = new MyProgram(123,\"abc\"); System.out.println(\"data=\"+ ? +\" str=\"+ ?); System.out.println(program.toStr()); } } 5.设计一个类层次,定义一个抽象类—形状,其中包括有求形状的面积的抽象方法。继承该抽象类定义三角形、矩形、圆。分别创建一个三角形、矩形、对象,将各类图形的面积输出。 注:三角形面积 s=sqrt(p*(p-a)*(p-b)*(p-c)) 其中,a,b,c为三条边, p=(a+b+c)/2 程序如下: abstract class Shape{ abstract public double area(); } class Triangle extends Shape{ private double a,b,c; public Triangle(double a,double b,double c){ this.a=a;?;?; } public double area(){ double p=(a+b+c)/2; return ?; } } class Rectangle extends Shape{ private double width,height; public Rectangle(double j,double k){

width=j;height=k; } public double area() { return ?; } } class Circle extends Shape{ private double r; public Circle(double r){ ; } public double area(){ ; } } public class Count{ public static void main(String args[]){ Shape ?; s1=new Triangle(22,41,57); s2=? Rectangle(17,17); =new Circle(47); System.out.println(s1.area()); System.out.println(s2.area()); System.out.println(s3.area()); } } 6.定义接口Shape,其中包括一个方法size(),设计“直线”、“圆”类实现Shape接口。分别创建一个“直线”、“圆”对象,将各类图形的大小输出。 程序如下: interface ?{ double ?(); } class Line implements Shape{

private double x1,y1,x2,y2; public Line(double ?,double ?,double ?, double ?){ this.x1=x1;this.y1=y1; } public double size(){ Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); } } class Circle implements Shape{ private double r; public Circle (double r){ } public double size(){ ? } } public class Count{ public static void main(String args[]){ Shape ?; s1=new Line(22,56,41,57); =new Circle(47); System.out.println(s1.size()); System.out.println(s2.size()); } } 7.创建自定义包Mypackage,一个学生类(Student),包含:属性:学号s_No,姓名s_Name,性别s_Sex,年龄s_Age。方法:构造方法,显示学号方法showNo(),显示姓名方法showName(),显示性别方法showSex(),显示年龄方法showAge(),修改年龄方法modifyAge()。主类(X4_3_2),包含:主方法main(),在其中创建两个学生对象s1和s2并初始化,两个对象的属性自行确定,然后分别显示这两个学生的学号、姓名、性别、年龄,然后修改s1的年龄并显示修改后的结果。 程序如下: package ? public class ?{

public static void main(String[] args) { Student s1 = new Student(101,\"张三\男\ Student s2 = ? Student(102,\"李四\女\ System.out.println(\"第1个学生的信息为:\"); s1.showNo(); ? s1.showSex(); s1.showAge(); System.out.println(?); ? s2.showName(); s2.showSex(); s2.showAge(); System.out.println(\"修改第1个学生的年龄:\"); s1.modifyAge(24); System.out.println(\"第1个学生的年龄修改为:\"); s1.showAge(); } } class ?{ private int s_No; ? String s_Name; private ?; private int s_Age; Student(int no, String name, String sex, int ?){ s_No = ? s_Name = ? ? = sex; s_Age = age; } void showNo(){ System.out.println(\"学号:\"+s_No); } void ?{ System.out.println(\"姓名:\" + ?); } void showSex(){

} System.out.println(? + s_Sex); } void showAge(){ System.out.println(\"年龄:\" + s_Age); } void modifyAge(int newAge){ s_Age = newAge; }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 91gzw.com 版权所有 湘ICP备2023023988号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务