Java fifth job - creation and use of objects


First, the needs analysis 

      Programming, defines a rectangular type, having a length, width, area, perimeter, a total of four member variables, calculation of the area and calculating the perimeter of the two methods, the main class creates objects in the rectangle, the length and width of the input, output rectangular area and perimeter.

Second, the functional design

  Basic Function: Define a rectangle class, having length, width, area, perimeter, a total of four members of the variable, two calculation methods calculate the area and perimeter, the length and width of the input, it outputs a rectangular area and perimeter.

Third, the design and implementation

  I have devised a main class Rectangle, Rect rectangle created in the main category, used to calculate the area and perimeter of the rectangle.

Fourth, the test run

  LWH keyboard input, obtains a rectangular area and perimeter.

      

V. Code

1, the main class code

Package COM; 

Import java.util.Scanner; 

public  class the Rectangle the extends Rect { 

    / ** 
     * @param args
      * / 
    public  static  void main (String [] args) {
         // the TODO Auto-Generated Method Stub 
        Scanner SC = new new Scanner ( the System.in); 
        Rect T = new new Rect (); 
        System.out.println ( "Please enter the rectangle length:" ); 
        TA = sc.nextDouble (); 
        System.out.println ( "Please enter the width of the rectangle:" ) ; 
        TB= Sc.nextDouble (); 
        System.out.println ( "Rectangular area:" + t.rearea (TA, TB)); 
        System.out.println ( "rectangular perimeter:" + t.relength (TA, TB)); 
        
    } 

}

2, subclass code

package com;

public class Rect {
    double a;//矩形长
    double b;//矩形宽
    double area;//矩形面积
    double length;//矩形周长
    double rearea(double a,double b){
        area=a*b;
        return area;
    }
    double relength(double a,double b){
        length=2*a+2*b;
        return length;
    }
}

VI Summary

  See when the subject think this simple question, but when I start to do when still a little difficult, and only then thought for a program written in the way you want, feel his own shortcomings, the future course to be harder to learn, improve yourself!

Guess you like

Origin www.cnblogs.com/jingxueyan/p/11537098.html