Java作业三 语言基础与面向对象
掌握Java语言基础与面向对象的综合编程技术与方法,更全面深入地理解面向对象程序设计技术内涵。
一、阅读程序题1
请先阅读下面的程序,确定它的输出结果。再将程序上机运行,验证自己分析的输出结果是否正确。
package Practice3;
class Yuan
{
int r;
String name;
static int z= 5;
Yuan(int r){ this.r=r; }
int a(){ return z*r*r; }
int p(){ return 5*z*r; }
void value (){ name="china"; }
}
class testOverride extends Yuan
{
int h;
String name;
testOverride(int r,int h1) { super(r); h=h1; }
int a(){ value(); return 2*super.a()+p()*h; }
void value()
{
super.value();
name="America";
System.out.println("\""+this.name+"\"");
System.out.println(super.name);
}
public static void main(String args[])
{
Yuan y= new Yuan(4);
Yuan yz= new testOverride(5,2);
System.out.println(y.a());
System.out.println(yz.p());
System.out.println(yz.a());
}
}
自己运行一下就好了,结果如下:
80
125
"America"
china
500
二、阅读程序题2
请先阅读下面的程序,确定它的输出结果。再将程序上机运行,验证自己分析的输出结果是否正确。
public class TestTransOfValue
{
public static void main(String args[])
{
double val;
StringBuffer sb1, sb2;
String sb3;
char s[]={'a','p','p','l','e'};
val = 5.8;
sb1 = new StringBuffer("apples");
sb2=new StringBuffer("pears");
sb3 = new String("pear");
modify(val, sb1, sb2,sb3,s);
System.out.println(val);
System.out.println(sb1);
System.out.println(sb2);
System.out.println(sb3);
System.out.println(s);
}
public static void modify(double a, StringBuffer r1, StringBuffer r2,String r3,char s[] )
{
a = 6.8;
r1.append(" taste good");
r2=null;
r3="banana";
s[2]='R';
}
}
结果如下:
5.8
apples taste good
pears
pear
apRle
三、阅读程序题3
请仔细阅读下面的程序,分析程序的结构和输出结果。再将程序上机运行,验证输出结果。(注意重点理解:内部类、对象内部类、静态内部类)
package Practice3;
public class Outer
{
public Outer()
{
System.out.println("OuterClass Object!");
}
private class Inner1
{
private Inner1(String s){ System.out.println(s);}
}
static class Inner2
{
Inner2(String s){ System.out.println(s);}
}
public static void main(String[] args)
{
Outer ob= new Outer();
Outer.Inner1 ib1 = ob.new Inner1("InnerClass1 Object!");
//Inner1 ib1 = ob.new Inner1("InnerClass1 Object!");
Inner2 ib2 = new Inner2("InnerClass2 Object!");
}
}
结果:
OuterClass Object!
InnerClass1 Object!
InnerClass2 Object!
请再尝试将:
Outer.Inner1 ib1 = ob.new Inner1("InnerClass1 Object!");
改为:
ob.Inner1 ib1 = ob.new Inner1("InnerClass1 Object!");
看有什么变化。
结果还是:
OuterClass Object!
InnerClass1 Object!
InnerClass2 Object!
四、阅读程序题4
请仔细阅读下面的程序,分析程序的结构和输出结果。由此理解接口、抽象类、继承、实现接口,进一步理解多态。
package Practice3;
interface Food { void doEat();} // doEat()是吃食物的方法
abstract class Fruit{ } //水果抽象类
abstract class Meat{ } //肉抽象类
class Apple extends Fruit implements Food //苹果类
{
public void doEat() { System.out.println("我是苹果,属于水果类,你不必烹饪我就可吃!"); }
}
class Beef extends Meat implements Food //牛肉类
{
public void doEat() {System.out.println("我是牛肉,属于肉类,必须烹饪后才可吃!"); }
}
public class Use
{
public static void main(String[] args)
{
Food f=new Apple();
f.doEat();
f=new Beef();
f.doEat(); // 两个“f.doEat()”体现了多态
}
}
输出结果:
我是苹果,属于水果类,你不必烹饪我就可吃!
我是牛肉,属于肉类,必须烹饪后才可吃!
试一试将主方法改为:
public static void main(String args[])
{ Food f=new Apple(); f.doEat(); }
观察系统有何反应?
输出结果:
我是苹果,属于水果类,你不必烹饪我就可吃!
分析:
接口可以作为一种引用类型来使用。任何实现该接口的类的实例都可以存储在该接口类型的变量中,通过这些变量可以访问类所实现的接口中的方法。Java运行时系统动态地确定该使用哪个类中的方法。主方法中Food f=new Apple();
用到了这一概念。
再在Fruit类中加入方法
abstract void doEat();
看结果怎样,你能得出什么结论?
输出结果:
我是苹果,属于水果类,你不必烹饪我就可吃!
结论:
Fruit类中加入了doEat()方法,Food接口中也有doEat()方法。Apple类继承了Fruit类,同时实现了Food接口。
那Apple类中 public void doEat() { System.out.println("我是苹果,属于水果类,你不必烹饪我就可吃!"); }
这句话是对Food接口来说的还是对Fruit抽象类来说的?答案应该是前者。
因为接口抽象程度比抽象类更高,接口的实现优先于抽象类。
五、实现两个矩阵相乘
编写一个程序,其中设计一个矩阵类Matrix,最后计算:
要求Matrix类满足:
- Matrix的属性有:
m,n:int型,矩阵的行、列数。
ma:int型两维数组,放置矩阵的数据。
- Matrix的方法有:
Matrix (int m , int n ):构造方法,设置矩阵的行数和列数。
cheng(Matrix a):将当前矩阵与形参矩阵相乘,最后返回乘的结果(Matrix对象)。
void print():输出矩阵。
代码
import java.util.Scanner;
public class CalculateMatrix
{
static class Matrix
{
int m;
int n;
int [][]ma;
public Matrix(int m, int n)
{
this.m = m;
this.n = n;
ma = new int[m][n];
}
Matrix cheng(Matrix a)
{
int x = this.m;
int y = this.n;
int z = a.n;
Matrix tmp = new Matrix(x,z);
for (int i = 0; i < x; i++)
{
for (int j = 0; j < z; j++)
{
for (int k = 0; k < y; k++)
{
tmp.ma[i][j]+= this.ma[i][k] * a.ma[k][j];
}
}
}
return tmp;
}
void print()
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(ma[i][j]);
System.out.print('\t');
}
System.out.print('\n');
}
}
}
public static void main(String[] args)
{
int x;
int y;
int z;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个矩阵的行数与列数");
x = scanner.nextInt();
y = scanner.nextInt();
Matrix mtx1 = new Matrix(x,y);
System.out.println("请逐行输入矩阵的各个元素:");
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
mtx1.ma[i][j] = scanner.nextInt();
}
}
System.out.println("请输入第二个矩阵的行数与列数:");
while (scanner.nextInt()!=y)
{
z = scanner.nextInt();
System.out.println("无法相乘,请重新输入第二个矩阵的行数与列数:");
}
z = scanner.nextInt();
Matrix mtx2 = new Matrix(y,z);
System.out.println("请逐行输入矩阵的各个元素:");
for (int i = 0; i < y; i++)
{
for (int j = 0; j < z; j++)
{
mtx2.ma[i][j] = scanner.nextInt();
}
}
scanner.close();
System.out.println("相乘后得到的矩阵如下:");
mtx1.cheng(mtx2).print();
}
}
运行截图
六、使用Shape接口输出图形面积
设计一个程序,其中含有一个接口Shape(形状),其中有求形状的面积的方法area()。再定义三个实现接口的类:三角型类、矩形类和圆类。在主方法中创建Shape类型的一维数组,它有三个元素,放置三个对象,分别表示三角形、矩形和圆,然后利用循环输出三个图形的面积。
( 注:三角形面积s=Math.sqrt(p*(p-a)*(p-b)*(p-c))
,a,b,c为三条边,p=(a+b+c)/2 )
代码实现
public class Graph
{
interface Shape{void area();}
static class Triangle implements Shape
{
double a;
double b;
double c;
public Triangle(double a,double b,double c)
{
this.a = a;
this.b = b;
this.c = c;
}
@Override
public void area()
{
double p = (a+b+c)/2.0;
double s = Math.sqrt(p*(p-a)*(p-b)*(p-c));
System.out.println("三角形的面积是:");
System.out.println(s);
}
}
static class Rectangle implements Shape
{
double x;
double y;
public Rectangle(double x,double y)
{
this.x = x;
this.y = y;
}
@Override
public void area()
{
double s = x * y;
System.out.println("矩形的面积是:");
System.out.println(s);
}
}
static class Circle implements Shape
{
double r;
public Circle(double r)
{
this.r = r;
}
@Override
public void area()
{
double s = Math.PI*r*r;
System.out.println("圆的面积是:");
System.out.println(s);
}
}
public static void main(String[] args)
{
Shape[] graph = new Shape[3];
graph[0] = new Triangle(3,4,5);
graph[1] = new Rectangle(6,6);
graph[2] = new Circle(2);
for (int i = 0; i < 3; i++)
{
graph[i].area();
}
}
}
运行截图
七、编写一个含有三个包的程序
(参考作业2第一题中的类)
第一个包:人员包,其中有人员、学生和教师三个类。
第二个包:管理包,其中有班级和师资两个类,它们分别有学生名单(学生对象构成的数组)和教师名单(教师对象构成的数组)属性,分别有建立和输出班级和师资的方法。
第三个包:使用包,只有一个类(主类),主方法中创建一个师资类对象,调用它的方法建立一个含有3个教师的师资,然后调用方法输出师资。
项目结构
人员包package People
//Person类
package People;
public class Person
{
private String number;
private String name;
private String gender;
public Person(String number,String name)
{
this.number = number;
this.name = name;
}
public void setNumber(String number)
{
this.number = number;
}
public void setName(String name)
{
this.name = name;
}
public String getNumber()
{
return number;
}
public String getName()
{
return name;
}
}
//Student类
package People;
public class Student extends Person
{
private String grade;
public Student(String number, String name)
{
super(number, name);
}
public void setGrade(String grade)
{
this.grade = grade;
}
public String getGrade()
{
return grade;
}
}
//Teacher类
package People;
public class Teacher extends Person
{
private String department;
public Teacher(String number, String name)
{
super(number, name);
}
public void setDepartment(String department)
{
this.department = department;
}
public String getDepartment()
{
return department;
}
}
管理包package Manage
//Klass类
package Manage;
import People.Student;
public class Klass
{
private String klassName;
private Student[] stuList = new Student[100];
public Klass(String klassName)
{
this.klassName = klassName;
}
public void setStuList(int i, Student student)
{
stuList[i] = student;
}
public void getStuList(int n)
{
for (int i = 0; i < n; i++)
{
System.out.println(stuList[i].getName());
}
}
}
//Faculty类
package Manage;
import People.Teacher;
public class Faculty
{
private String facultyName;
private Teacher[] teaList = new Teacher[100];
public Faculty(String facultyName)
{
this.facultyName = facultyName;
}
public void setTeaList(int i, Teacher teacher)
{
teaList[i] = teacher;
}
public void getTeaList(int n)
{
for (int i = 0; i < n; i++)
{
System.out.println(teaList[i].getName());
}
}
}
使用包package Use
//只有一个主类
package Use;
import Manage.Faculty;
import People.Teacher;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
System.out.println("请输入师资名称:");
Scanner scanner = new Scanner(System.in);
if (scanner.hasNext()) {
String facultyname = scanner.next();
Faculty faculty1 = new Faculty(facultyname);
System.out.println("请输入师资人数:");
int sum = Integer.parseInt(scanner.next());
System.out.println("请分别输入每位教师的编号和姓名:");
for (int i = 0; i < sum; i++) {
String number = scanner.next();
String name = scanner.next();
Teacher teacher = new Teacher(number, name);
faculty1.setTeaList(i, teacher);
}
System.out.println("该师资的教师名单如下:");
faculty1.getTeaList(sum);
}
}
}
运行截图