JAVA课堂整理二
例子一
1、编写一个Java应用程序,该程序包括3个类:Monkey类、People类和测试类。要求:
(1)Monkey类中有个public void speak()方法,在speak方法中输出“咿咿呀呀。。。。。。”的信息。
(2)People类是Monkey类的子类,在People类中重写方法speak,在speak方法中输出“小样的,不错嘛,会说话了!”的信息。
(3)在People类中新增方法void think(),在think方法中输出“别说话,认真思考!”的信息。
/** * Monkey 父类 */ package cn.yjlblog.www; public class Monkey { public void speak() { System.out.println("咿咿呀呀。。。。。。"); } } /** * People 子类 */ package cn.yjlblog.www; public class People extends Monkey { public void speak() { System.out.println("小样的,不错嘛,会说话了!");// TODO Auto-generated method stub } void think() { System.out.println("别说话,认真思考!"); } } /** * TestClass 测试类 */ package cn.yjlblog.www; public class TestClass { public static void main(String[] args) { Monkey m = new Monkey(); m.speak(); Monkey p = new People(); p.speak(); //Monkey p1 = new People();//The method think() is undefined for the type Monkey People p1 = new People(); p1.think(); } }
例子2
2、按要求编写一个Java应用程序:
(1)定义一个类(Rectangle),描述一个矩形,包含有长、宽两种属性和计算面积的方法。
(2)定义一个类(Cuboid),继承自矩形类,同时该类描述长方体,具有长、宽、高属性和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。
/** * Rctangle 父类 */ package cn.yjlblog.www; public class Rectangle { private double length; private double width; //生成set 和get 方法 public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } //构造含有参数的方法 public Rectangle(double length, double width) { this.length = length; this.width = width; } //求面积 public double Aera() { return length * width; } } /** * Cuboid 子类 */ package cn.yjlblog.www; public class Cuboid extends Rectangle { private double height; private double volume; public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getVolume() { return volume; } public void setVolume(double volume) { this.volume = volume; } public Cuboid(double length, double width, double height) { super(length, width); this.height = height; } } /** * TestClass 测试类 */ package cn.yjlblog.www; public class TestClass { public static void main(String[] args) { Cuboid rct = new Cuboid(10,20,30); double v = rct.Aera()*rct.getHeight(); double s = rct.Aera(); System.out.println("The Rctangle's volume is:"+v); System.out.println("The Rctangle's floor space is:"+s); } }
运行结果
The Rctangle's volume is:6000.0 The Rctangle's floor space is:200.0
例子3
3、按要求编写一个Java应用程序:
(1)编写一个Shape类,具有属性周长(perimeter)和面积(area);
(2)定义其子类三角形(Triangle)和矩形(Rectangle),分别具有求周长和面积的方法。
(3)定义测试类,在其main方法中创建三角形和矩形类的对象,并赋给Shape类的对象a和b,使用对象a、b来测试其特性。
/** * Shape 父类 */ package cn.yjlblog.www; public class Shape { private double perimeter; private double area; //get set 方法 public double getPerimeter() { return perimeter; } public void setPerimeter(double perimeter) { this.perimeter = perimeter; } public double getArea() { return area; } public void setArea(double area) { this.area = area; } //构造方法 public Shape(double perimeter, double area) { this.perimeter = perimeter; this.area = area; } } /** * Triangle 子类 */ package cn.yjlblog.www; public class Triangle extends Shape { public Triangle(double perimeter, double area) { super(perimeter, area); // TODO Auto-generated constructor s } private double a1; private double a2; private double a3; //set get 方法 public double getA1() { return a1; } public void setA1(double a1) { this.a1 = a1; } public double getA2() { return a2; } public void setA2(double a2) { this.a2 = a2; } public double getA3() { return a3; } public void setA3(double a3) { this.a3 = a3; } public double perimeter() { return a1+a2+a3; } public double area() { double s1=(a1+a2+a3)/2; double s2 = s1*(s1-a1)*(s1-a2)*(s1-a3); double result = Math.sqrt(s2); return result; } } package cn.yjlblog.www; /** * Rectangle 子类 */ public class Rectangle extends Shape{ public Rectangle(double perimeter, double area) { super(perimeter, area); // TODO Auto-generated constructor stub } private double b1; private double b2; public double getB1() { return b1; } public void setB1(double b1) { this.b1 = b1; } public double getB2() { return b2; } public void setB2(double b2) { this.b2 = b2; } public double perimeter() { return (b1+b2)*2; } public double area() { return b1*b2; } } /** * TestClass 测试类 */ package cn.yjlblog.www; public class TestClass { public static void main(String[] args) { Triangle a = new Triangle(0, 0); a.setA1(3); a.setA2(4); a.setA3(5); System.out.println(a.perimeter()); System.out.println(a.area()); Rectangle b = new Rectangle(0, 0); b.setB1(3); b.setB2(4); System.out.println(b.perimeter()); System.out.println(b.area()); } }
运行结果
12.0 //三角形周长 6.0 //三角形面积 14.0 //长方形周长 12.0 //长方形面积
例子4
4、按要求编写一个Java应用程序:
(1)编写一个矩形类Rect,包含:两个protected属性:矩形的长length和矩形的宽width;
两个构造方法:
一个带有两个参数的构造方法,用于将width和length属性初始化;
一个不带参数的构造方法,用于将width和length属性初始化为0;
两个方法:
求矩形面积的方法area();
求矩形周长的方法perimeter();
(2)通过继承Rect类编写一个具有确定位置的矩形类PlainRect,其确定位置用矩形的左上角坐标来标识,包含:两个属性:矩形左上角坐标startX和startY
两个构造方法:
一个带有四个参数的构造方法,用于对startX、startY、width和length属性初始化;
一个不带参数的构造方法,用于将startX、startY、width和length属性初始化为0;
一个方法:
判断某个点是否在矩形内部的方法isInside(double x,double y)。如在矩形内,返回true,否则返回false。
提示:点在矩形内的判断条件:
x>=startX&&x<=(startX+length)&&y>=startY&&y<=(startY+width)
(3)编写测试类
创建一个左上角坐标为(10,10),长为20,宽为10的矩形对象;计算并打印输出矩形的面积和周长;并判断点(25.5,13)是否在矩形内,并打印输出相关信息。
public class 测试 { public static void main(String[] args) { PlainRect p=new PlainRect(10,10,10,20); p.area(); p.perimeter(); System.out.println("矩形的面积:"+p.area()); System.out.println("矩形的周长:"+p.perimeter()); p.isInside(25.5,13); } } public class Rect { protected double length; protected double width; public Rect(double length,double width) { //super(); this.length=length; this.width=width; } public Rect() { //super(); length=0; width=0; } public double area() { return length*width; } public double perimeter() { return length+width; } } public class PlainRect extends Rect { protected double startX; protected double startY; public PlainRect(double startX,double startY,double width,double length) { this.startX=startX; this.startY=startY; this.width=width; this.length=length; } public PlainRect() { startX=0; startY=0; width=0; length=0; } public boolean isInside(double x,double y) { if(x>=startX&&x<=(startX+length)&&y>=startY&&y<=(startY+width)){ System.out.println("点在矩形内部"); return true; }else { System.out.println("点不在矩形内部"); return false; } } }