java-1

需求:写一个求矩形面积的java程序,要求有主类(Example1)和一个矩形类(Rect),方法(getArea)写在Rect类中,主类用于输入、调用、输出。

code:

import java.util.Scanner;
public class Rect {
    double width;    
    double height;    
    double getArea(){
        return width*height;    
    }
}
class Example1{
    public static void main(String args[]){
        Rect rectangle;        
        rectangle = new Rect();        
        //Rect rectangle = new Rect();        
        Scanner reader = new Scanner(System.in);
        System.out.println("input the width : ");        
        rectangle.width = reader.nextDouble();        
        System.out.println("input the height : ");        
        rectangle.height = reader.nextDouble();
        double area = rectangle.getArea();        
        System.out.print("The area of the rectangle is "+area);    
        }
}

评论

此博客中的热门博文

My Determination

简单内网转发环境搭建