图形类作业

作业

图形类作业_第1张图片

题一

Graph.java

package com.maps.one;
/**
 * 
  * 

Title: Graph.java

  *

Description:抽象类的使用

  * @author PoleYupro   * @date 2019年9月27日 */
public abstract class Graph implements Comparable { /* * 定义两个抽象方法 */ public abstract double area(); public abstract double girth(); public int compareTo(Graph graph){ int i = 0; if(this.area()>graph.area()){ return i=1; }else if(this.area()<graph.area()){ return i=-1; }else{ return i=0; } } }

Circle.java

package com.maps.one;
import com.maps.one.*;
/**
 * 
  * 

Title: Circle.java

  *

Description:圆形类继承抽象类

  * @author PoleYupro   * @date 2019年9月27日 */
public class Circle extends Graph{ //圆形 public double radius; public Circle(double radius){ super(); this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double area(){ return 3.14*radius*radius;//Math.PI*radius*radius } public double girth(){ return 3.14*2*radius;//Math.PI*2*radius } }

Square.java

package com.maps.one;

public class Square extends Graph{
     //正方形
	public double side;

	
	public Square(double side) {
     
		super();
		this.side = side;
	}

	public double getSide() {
     
		return side;
	}

	public void setSide(double side) {
     
		this.side = side;
	}
	
	public double area(){
     
		return side*side;
	}
	public double girth(){
     
		return 4*side;
	}
}

Oblong.java

package com.maps.one;

public class Oblong extends Graph{
     //长方形
	public double length;
	public double width;
	
	public Oblong(double length, double width) {
     
		super();
		this.length = length;
		this.width = width;
	}
	
	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 double area(){
     
		return length*width;
	}
	public double girth(){
     
		return 2*(length+width);
	}
}

MapsTest.java

package com.maps.one;
import java.util.Scanner;
import com.maps.one.*;

public class MapsTest {
     
	public static void main(String[] args){
     
		//圆
//		Circle cle = new Circle();
//		System.out.println("请输入圆的半径:");
//		Scanner sc = new Scanner(System.in);
//		cle.radius = sc.nextDouble();
//		System.out.println("圆形的面积为"+cle.area());
//		System.out.println("圆形的周长为"+cle.girth());
//		//正方形
//		Square sqe=new Square();
//		System.out.println("请输入正方形的边长:");
//		Scanner s=new Scanner(System.in);
//		sqe.side=s.nextDouble();
//		System.out.println("正方形的面积为"+sqe.area());
//		System.out.println("正方形的周长为"+sqe.girth());
//		//长方形
//		Oblong obl=new Oblong();
//		System.out.println("请输入长方形的长和宽:");
//		Scanner c=new Scanner(System.in);
//		obl.length=c.nextDouble();
//		obl.width=c.nextDouble();
//		System.out.println("长方形的面积为"+obl.area());
//		System.out.println("长方形的周长为"+obl.girth());
		
		Maps circle = new Circle(5.0);
		Maps square = new Square(4.0);
		Maps oblong = new Oblong(5.0,4.0);
		System.out.println("圆的面积为:"+circle.area());
		System.out.println("圆的周长为:"+circle.girth());
		System.out.println("正方形的面积为:"+square.area());
		System.out.println("正方形的周长为:"+square.girth());
		System.out.println("长方形的面积为:"+oblong.area());
		System.out.println("长方形的周长为:"+oblong.girth());
	}
}

题二

Comparable.java

package com.maps.two;
import com.maps.one.*;

public interface Comparable {
     
	
	//比较面积
	public int compareTo(Graph graph); 

}

MapsTest2.java

package com.maps.two;
import com.maps.one.*;
import com.maps.two.*;

public class MapsTest2 {
     
	
	public static void main(String[] args) {
     
		//实例化图形对象数组
		Graph[] gra = new Graph[10];
		for(int i=0;i<3;i++){
     
			Circle c = new Circle(2.0+i);
			gra[i] = c;
		}
		for(int i=3;i<7;i++){
     
			Square s = new Square(3.0+i);
			gra[i] = s;
		}
		for(int i=7;i<10;i++){
     
			Oblong o = new Oblong(3.0+i,4.0+i);
			gra[i] = o;
		}
		Graph max=gra[0];
		for(int i=0;i<gra.length;i++){
     
			if(gra[i].area()>max.area()){
     
				max = gra[i];
			}
		}
		System.out.println(max.area());
	}
}

简单总结

  • 接口,抽象类的基本概念并理解
  • 熟悉派生子类
  • 对Java SE 阶段进行基础的复习

你可能感兴趣的:(一些作业)