Compare C and Java

作者: Tedisaname | 来源:发表于2018-08-19 21:17 被阅读15次

    There is a lot of differences between C and java, so write those codes and compare with them.There is a question about a triangle, need u to write codes to obtain its length and area.Use C and java to write it and compare what the differences there are.

    C codes:
    #include <stdio.h>
    #include <math.h> 
    
    int perimeter(int m,int n,int q)
    {
        return (m+n+q);
    }
    
    double trianglearea(int m,int n,int q)
    {
        double p = 1.0*(m+n+q)/2;
        return sqrt(p*(p-m)*(p-n)*(p-q));//This is a formula can quickly get the area of a triangle.
    }
    
    int main()
    {
        int m,n,q;
        scanf("%d %d %d",&m,&n,&q);
        
        int len = perimeter(m,n,q);
        
        double area = trianglearea(m,n,q);
        
        printf("%d\n",len);
        printf("%lf\n",area);
        
        return 0;
    } 
    
    Java codes:
    package javabase;
    class Trian{
        int a;
        int b;
        int c;
        int len()
        {
            return a+b+c;
        }
        double area()
        {
            double p = 1.0*(a+b+c)/2;
            return Math.sqrt(p*(p-a)*(p-b)*(p-c));
        }
    }
    public class Triangle {
    
        public static void main(String[] args) {
    
            Trian t = new Trian();
            t.a = 3;
            t.b = 4;
            t.c = 5;
            System.out.printf("%d    %f\n",t.len(),t.area());
            //use java,no matter the type is double or float ,print out with the format %f
        }
    
    }
    
    

    相关文章

      网友评论

        本文标题:Compare C and Java

        本文链接:https://www.haomeiwen.com/subject/akkabftx.html