Java Novice Notes 16 area

Source: Internet
Author: User

1. Graphic class

Package Com.yfs.javase;public class Shape {//Calculate Area method public double Getarea () {System.out.println ("calculated area"); return 0;}}

2. Round

Package Com.yfs.javase;public Class Circle extends Shape {private double r;public Circle (double r) {THIS.R = R; SYSTEM.OUT.PRINTLN ("Create circular area"); Public double Getarea () {//override method System.out.println for parent class ("Calculate circular area ..."); return 3.14 * R * r;}}

3. Rectangle

Package Com.yfs.javase;public class Rangton  extends Shape {private double width;private double length;public Rangton (double width, double length) {this.width = Width;this.length = length; SYSTEM.OUT.PRINTLN ("Create rectangular area"); Public double Getarea () {System.out.println ("Calculate Rectangle area ..."); return width * length;}}

4. Triangles

Package Com.yfs.javase;public class Trantangle  extends Shape {private double height;private double width;public Trantangle (double height, double width) {this.height = Height;this.width = width; System.out.println ("Create Triangle area"); Public double Getarea () {System.out.println ("Calculate Triangle area ..."), return 1.0/2 * width * height;}

5. Testing

The package Com.yfs.javase;import Java.util.random;public class Test1 {/** *) writes a graphics class that provides methods for calculating the area. * By inheriting graphics classes, encapsulate triangles, circles, squares, * methods that override the parent class. Randomly generate 10 graphs in a test class, * area summation. */public static void Main (string[] args) {shape[]  shapes = new shape[10];//holds sub-class object Random ran = new random ();d ouble sum = 0;//creates the graphic for (int i = 0; i < shapes.length; i++) {int r = Ran.nextint (101), if (R > N) {shapes[i] = new Circle (ran . Nextint (10));} else if (R >) {shapes[i] = new Rangton (Ran.nextint, Ran.nextint);//shapes[i].setwidth ();} else {shapes[i] = n EW Trantangle (Ran.nextint (Ten), Ran.nextint (10));}} System.out.println ("================");//calculates the random plot area for (int i = 0; i < shapes.length; i++) {//circle c = (Circle) shapes[i ];//sum + = C.getarea (); sum + = Shapes[i].getarea ();//Sub-class object calculates area}system.out.println ("sum =" + sum);}}

Java Novice Notes 16 area

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.