赞
踩
在一个源程序中,定义四个类:
第一个类是图形类(Shape),含有一个成员变量color(字符串类型),一个没有参数的构造方法,以及一个有一个字符串类型参数的构造方法来初始化颜色变量,还有一个打印颜色变量的成员方法show;
第二个类是圆形类(Circle)继承了图形类,自己又含有一个变量半径r,有一个有两个参数的构造方法,来初始化颜色和半径,还有一个打印两个成员变量的成员方法show;其中构造方法实现时,需先调用父类带参的那个构造方法
第三个类是矩形类(Rectangle)继承了图形类,自己又含有两个变量长a和宽b,有一个有三个参数的构造方法,来初始化颜色、长和宽,还有一个打印三个成员变量的成员方法show;其中构造方法实现时,需先调用父类带参的那个构造方法
第四个类是测试类(TestShape),分别定义圆形类和矩形类的实例对象,并用show方法来测试自己的定义。
Shape.java
public class Shape {
String color;
Shape() {}
Shape(String color) {
this.color = color;
}
String show(){
return color;
}
}
Circle.java
public class Circle extends Shape{
double r;
Circle(){}
Circle(String color,double r){
this.r = r;
this.color = color;
}
public void showCircle() {
System.out.println("圆形的颜色为"+color);
System.out.println("圆形的半径为"+r);
}
}
Rectangle.java
public class Rectangle extends Shape{
int a;
int b;
Rectangle(){}
Rectangle(int a,int b,String color){
this.a = a;
this.b = b;
this.color = color;
}
public void showRectangle() {
System.out.printf("矩形的长和宽为%d和%d",a,b);
System.out.println();
System.out.print("矩形的颜色为"+color);
}
}
TestShape.java
public class TestShape {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Circle circle = new Circle("red",10.5);
Rectangle rectangle = new Rectangle(4,6,"blue");
circle.showCircle();
rectangle.showRectangle();
}
}
【实验结果】
定义一个抽象类Figure,具有area()方法; 定义子类Circle,继承Figure,重写area()方法;
定义子类Square,继承Figure,重写area()方法;
定义测试类,计算并输出一个圆和一个正方形的面积。(要用上转型对象调用area()方法)
在前面的基础上, 定义子类Triangle,继承Figure,重写area()方法; 注意: 三角形面积求法:
海伦公式:设P=(a+b+c)/2 ,S△=根号下P(P-a)(P-b)(P-c)
。注:边长分别为a、b、c,三角形的面积S,而公式里的p为半周长(Math.sqrt())
定义测试类,计算并输出一个圆和一个正方形和一个三角形的面积。(要用上转型对象调用area()方法)
Figure.java
public abstract class Figure {
abstract void area();
}
Circle.java
public class Circle extends Figure{
double radius;
Circle(){}
Circle(double radius){
this.radius = radius;
}
void area() {
double result = 3.14*radius*radius;
System.out.println("圆形的面积是"+result);
}
}
Square.java
public class Square extends Figure{
int side;
Square(){}
Square(int side){
this.side = side;
}
void area() {
int result = side*side;
System.out.println("正方形的面积为"+result);
}
}
Triangle.java
public class Triangle extends Figure{
int a;
int b;
int c;
Triangle(){}
Triangle(int a,int b,int c){
this.a = a;
this.b = b;
this.c = c;
}
void area() {
double P = (a+b+c)/2;
double S = Math.sqrt(P*(P-a)*(P-b)*(P-c));
System.out.println("三角形的面积是"+S);
}
}
Test.java
public class Test {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Figure figure;
figure = new Circle(4);
figure.area();
figure = new Square(4);
figure.area();
figure = new Triangle(4,5,6);
figure.area();
}
}
【实验结果】
设计一个类,该类有一个方法public int f(int a,int b),返回a,b的最大公约数
设计一个子类,重写祖先的f方法,且返回a,b的最小公倍数。
要求在子类重写父类方法时,首先调用父类的方法f获得最大公约数m,然后再用公式(a*b)/m获得最小公倍数。
最后写一个测试程序,分别调用父类和子类的方法。(要用上转型对象调用f ()方法)
Father.java
public class Father {
/**
*求两个数a、b的最大公约数
*/
public int f(int a,int b) {
System.out.print(a+"和"+b+"的最大公约数为");
if(a<b) {
int c = a;
a = b;
b = c;
}
int r = a%b;
while(r != 0) {
a = b;
b = r;
r = a%b;
}
System.out.println(b);
return b;
}
}
Son.java
public class Son extends Father{
@Override
public int f(int a, int b) {
//super关键字调用父类的f函数
int m = super.f(a, b);
int result = a*b/m;
System.out.println(a+"和"+b+"的最小公倍数为"+result);
return result;
}
}
Test.java
public class Test {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Father father;
father = new Father();
father.f(10, 20);
father = new Son();
father.f(7, 5);
}
}
【实验结果】
将上题改为抽象编程 先定义一个抽象类F 再定义一个子类Max(最大公约数类) 再定义Max的一个子类Min(最小公倍数类)
最后写一个测试程序,用上转型对象调用f方法。
F.java
public abstract class F {
abstract int f(int a,int b);
}
Max.java
public class Max extends F{
public int f(int a,int b) { //求两个数a、b的最大公约数
if(a<b) {
int c = a;
a = b;
b = c;
}
int r = a%b;
while(r != 0) {
a = b;
b = r;
r = a%b;
}
System.out.println("最大公约数为"+b);
return b;
}
}
Min.java
public class Min extends Max{
public int f(int a,int b) {
int m = super.f(a, b);
int result = a*b/m;
System.out.println("最小公倍数为"+result);
return result;
}
}
Test.java
public class Test {
public static void main(String[] args) {
// TODO 自动生成的方法存根
F f;
f = new Max();
f.f(10, 20);
f = new Min();
f.f(7, 5);
}
}
【实验结果】
定义一个接口Shape,其中包括一个抽象方法area(),设计矩形/圆/三角形等类实现Shape接口。分别创建代表矩形/圆/三角形的3个对象存入一个Shape类型的数组中,通过调用area()方法将数组中各类图形的面积输出。
Shape.java
public interface Shape {
abstract public void area();
}
Circle.java
public class Circle implements Shape {
int radius;
Circle(){}
Circle(double radius){
this.radius = radius;
}
@Override
public void area() {
// TODO 自动生成的方法存根
double result = 3.14*radius*radius;
System.out.println("圆的面积为"+result);
}
}
Rectangle.java
public class Rectangle implements Shape {
int a;
int b;
Rectangle(){}
Rectangle(int a,int b){
this.a = a;
this.b = b;
}
@Override
public void area() {
// TODO 自动生成的方法存根
int result = a*b;
System.out.println("正方形的面积为"+result);
}
}
Triangle.java
public class Triangle implements Shape{
int a;
int b;
int c;
Triangle(){}
Triangle(int a,int b,int c){
this.a = a;
this.b = b;
this.c = c;
}
public void area(){
double P = (a+b+c)/2;
double S = Math.sqrt(P*(P-a)*(P-b)*(P-c));
System.out.println("三角形的面积是"+S);
}
}
Test.java
public class Test {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Circle circle = new Circle(4);
Triangle triangle = new Triangle(4,5,6);
Rectangle rectangle = new Rectangle(4,5);
Shape shape;
Shape array[] = {circle,triangle,rectangle};
for(int i=0;i<array.length;i++) {
shape = array[i];
shape.area();
}
}
}
【实验结果】
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。