zoukankan      html  css  js  c++  java
  • 第七周课程总结&实验报告(五)

    (一)设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法。 继承该抽象类定义三角型、矩形、圆。 分别创建一个三角形、矩形、圆存对象,将各类图形的面积输出。
    注:三角形面积s=sqrt(p(p-a)(p-b)*(p-c)) 其中,a,b,c为三条边,p=(a+b+c)/2
    一:实验代码

    package shiyan4;
    
    abstract class shape {
        public abstract double print();   
    }
    class s extends shape { 
    	private double a;
        private double b;
        private double c;         
        public s(double a,double b,double c){
        	this.a=a;this.b=b;this.c=c;              
        }
        public double print() { 
            double p=(a+b+c)/2;
            return  Math.sqrt(p*(p-a)*(p-b)*(p-c));
        }
    }
    class j extends shape {
        private double width;
        private double height; 
        public j(double width, double height){
            this.height=height;this.width=width;    
        }
        public double print() {      
            return width*height;  
        }
    }
    class y extends shape {
    	double banjing;     
        public y(double banjing){  
            this.banjing=banjing;  
        }
        public double print() {     
            return Math.PI*banjing*banjing;
        }
    }
    public class shiyan4 {
        public static void main(String[] args){
            shape s1=new s(3,6,8);
            shape s2=new j(5,10);
            shape s3=new y(5);     
            System.out.println("三角形的面积为: "+s1.print()+"矩形的面积为: "+s2.print()+"圆的面积为: "+s3.print());     
        }
    }
    

    (二)定义接口Shape,其中包括一个方法size(),设计“直线”、“圆”、类实现Shape接口。分别创建一个“直线”、“圆”对象,将各类图形的大小输出。
    一:实验代码

    package shiyan4;
    
    public interface Shape {
    	public abstract void size(); 
    }
    class z implements Shape{   
    	private double x;
    	public z(double x){  
    		this.x=x;     
    	}
    	public void size() {  
    	    System.out.println("直线: "+x);
    	}
    }
    class y implements Shape{
    	private double banjing;
    	public y(double banjing){    
    	    this.banjing=banjing;        
    	}
    	public void size(){    
    	    System.out.println("圆的面积: "+Math.PI*banjing*banjing);
    	}
    }
    package shiyan4;
    
    public class shiyan {
    	public static void main(String[] args){
            Shape s1=new z(10);
            Shape s2=new y(10);   
            s1.size();
            s2.size();
    	}
    }
    
    

    本周学习总结
    学习了新的关键词object,并对其有了一些了解。
    学了抽象类和接口,就是usb接口的工作原理⑧。

  • 相关阅读:
    今天面试一些程序员(新,老)手的体会
    UVA 10635 Prince and Princess
    poj 2240 Arbitrage
    poj 2253 Frogger
    poj 2485 Highways
    UVA 11258 String Partition
    UVA 11151 Longest Palindrome
    poj 1125 Stockbroker Grapevine
    poj 1789 Truck History
    poj 3259 Wormholes
  • 原文地址:https://www.cnblogs.com/swaggy89/p/11663284.html
Copyright © 2011-2022 走看看