The calculation of the area volume of cylindrical body by inheriting

Source: Internet
Author: User

A Write a circle class circle, which has:

① a member variable

radius//the radius of the circle; private, floating point type

② Two construction methods

Circle ()//set RADIUS to 0

Circle (Doubler)////Create Circle object with radius initialized to R

③ Three Member methods

Double Getarea ()//Get the area of a circle

Double Getperimeter ()//Get the perimeter of a circle

void Show ()//Output the radius, perimeter, area of the circle to the screen

Write a cylindrical class cylinder, which inherits from the Circle class above. also has:

① a member variable

Double hight//height of cylinder, private, floating point type

② Construction Method

Cylinder (Double R, Double h)//When the Circle object is created, the radius is initialized to R

③ Member method

Double Getvolume ()//Get the volume of a cylinder

void Showvolume ()//output The volume of a cylinder to the screen

Write the application, create two classes of objects, set the radius of the circle, the height of the cylinder, calculate and show the circle radius, circle area, circumference length, cylinder volume.

public class Circle {
	private double Radius;
	Public Circle () {
		radius=0;
	}
	Public Circle (double radius) {
		super ();
		radius = radius;
	}
	Public double Getarea () {return
		math.pi*radius*radius/2;
	}
	Public double Getperimeter () {return
		2*math.pi*radius;
	}
	public void Show () {
		System.out.println ("radius of Circle:" +radius);
		System.out.println ("Area of the Circle:" +getarea ());
		System.out.println ("Circumference of Circle:" +getperimeter ());
	}
public class Cylinder extends Circle {
	private double hight;
	Public Cylinder (Double r,double h) {
		super (R);
		this.hight=h;
	}
	Public double Getvolume () {return
		getarea () *hight;
	}
	public void Show () {
		System.out.println ("Cylinder Volume:" +getvolume ());
	}
	public static void Main (string[] args) {
		double r=1;
		Double h=2;
		Cylinder c=new Cylinder (r,h);
		Circle c1=new Circle (r);
		C1.show ();
		C.show ();
	}







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.