Java练习:实验四

1.请先阅读下面的程序,确定它的输出结果。再将程序上机运行,验证自己分析的输出结果是否正确。

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';       }
}

3.编写一个程序,其中设计一个矩阵类Matrix,最后计算:

在这里插入图片描述

要求Matrix类满足:
1)Matrix的属性有:
m,n:int型,矩阵的行、列数;
ma:int型两维数组,放置矩阵的数据。
2)Matrix的方法有:
Matrix (int m , int n ):构造方法,设置矩阵的行数和列数;
cheng(Matrix a):将当前矩阵与形参矩阵相乘,最后返回乘的结果(Matrix对象); void print():输出矩阵。

import java.util.*;
public class Exp43 {

	public static void main(String[] args)
	{
		Matrix a=new Matrix();
		Matrix b=new Matrix();
		a.cheng(b).print();
	}
}

class Matrix{
	int m;
	int n;
	int ma[][];
	
	Matrix(int m,int n)
	{
		this.m=m;
		this.n=n;
		ma=new int[m][n];
	}
	
	Matrix()
	{
		System.out.println("请输入矩阵的行数和列数");
		Scanner sc=new Scanner(System.in);
		m=sc.nextInt();
		n=sc.nextInt();
		ma=new int[m][n];
		System.out.println("请输入矩阵");
		for(int i=0;i<m;i++)
			for(int j=0;j<n;j++)
			{
				ma[i][j]=sc.nextInt();
			}
		sc.close();
	}
	
	Matrix cheng(Matrix a)
	{
		Matrix temp=new Matrix(m,a.n);
		
		for(int i=0;i<m;i++)
			for(int j=0;j<a.n;j++)
				for(int k=0;k<n;k++)
				{
					temp.ma[i][j]=temp.ma[i][j]+this.ma[i][k]*a.ma[k][j];
				}
		return temp;
	}
	void print()
	{
		for(int i=0;i<m;i++)
		{	for(int j=0;j<n;j++)
			{
				System.out.print(ma[i][j]+" ");
			}
		System.out.println();
		}
	}
}

4.请仔细阅读下面的程序,分析程序的结构和输出结果。再将程序上机运行,验证输出结果。(注意重点理解:内部类、对象内部类、静态内部类)

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!");
     Inner2 ib2=new Inner2("InnerClass2 Object!");
    }
  } 

请再尝试将:
Outer.Inner1 ib1 = ob.new Inner1(“InnerClass1 Object!”);
改为:
ob.Inner1 ib1 = ob.new Inner1(“InnerClass1 Object!”);
看有什么变化。

说明
改动前输出:
OuterClass Object!
InnerClass1 Object!
InnerClass2 Object!
考察内部类的概念,需要注意内部类

  • 内部类对象创建的方法 ①使用外部类的方法②在其他类里面用new 创建
  • 但是这两种方法的前提都是必须要先创建一个该内部类的外部类的实例对象

改动后:报错,因为ob.Inner不是类型
5.请仔细阅读下面的程序,分析程序的结构和输出结果。由此理解接口、抽象类、继承、实现接口,进一步理解多态。

interface Food
{ public 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();  }   

观察系统有何反应?
再在Fruit类中加入方法“abstract void doEat();” ,看结果怎样,你能得出什么结论?

输出:
在这里插入图片描述

解释:

  • Food类是一个接口,Fruit和Meat属于抽象类,Apple继承了抽象类Fruit并实现了Food接口,Beef继承了抽象类Meat并实现了Food接口。
  • 在主方法中,首先创建了一个Apple类对象并向上转型成Food类(与子类向上转型成父类相似),因此Food类f可以使用子类Apple中重写的父类Food的方法,但子类中有的而父类中没有的则不能调用。
  • f.doEat()在Apple中重写,因此输出 我是苹果,属于水果类,你不必烹饪我就可吃!
  • 然后创建了一个Beef类对象赋给,与上段同理,输出 我是牛肉,属于肉类,必须烹饪后才可吃!

改动主方法后:

在这里插入图片描述

在Fruit类加入方法“abstract void doEat();” 后
输出:我是苹果,属于水果类,你不必烹饪我就可吃!
接口优先级高于抽象类,因此Apple中实现的是接口中的方法doEat()而不是抽象类中的doEat(),所以当Food类对象f调用f.doEat()时,调用的是Apple中重写过的doEat(),输出“我是苹果,属于水果类,你不必烹饪我就可吃!”

6.设计一个程序,其中含有一个接口Shape(形状),其中有求形状的面积的方法area()。再定义三个实现接口的类:三角型类、矩形类和圆类。在主方法中创建Shape类型的一维数组,它有三个元素,放置三个对象,分别表示三角形、矩形和圆,然后利用循环输出三个图形的面积。
( 注:三角形面积s=Math.sqrt(p*(p-a)(p-b)(p-c)),a,b,c为三条边,p=(a+b+c)/2 )

import java.util.Scanner;

public class ShapeTest {

	public static void main(String []args) {
		Shape Shapet[]=new Shape[3];
		Shapet[0]=new triangle(3,4,5);
		Shapet[1]=new rectangle(6,8);
		Shapet[2]=new circlel(5);
		for(int i=0;i<3;i++)
			System.out.println(Shapet[i].area());
	}
}

interface Shape{
	double area() ;
}

class triangle implements Shape{
	int a;
	int b;
	int c;
	
	triangle(int a,int b,int c)
	{
		this.a=a;
		this.b=b;
		this.c=c;
	}
	
	public double area() {
		double p=(a+b+c)/2;
		return Math.sqrt(p*(p-a)*(p-b)*(p-c));
	}
	
}

class rectangle implements Shape{
	int a;
	int b;
	rectangle(int a,int b)
	{
		this.a=a;
		this.b=b;
	}
	public double area() {
		return a*b;
	}
}

class circlel implements Shape{
	int r;
	circlel(int r){
		this.r=r;
	}
	public double area() {
		return 3.14*Math.pow(r, 2);
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值