Java interface exercises (with return values) __java learning

Source: Internet
Author: User
Requirements:
design a shape interface and its two implementation classes Square and circle, as required:
1 The Shape interface has an abstract method area (), which receives an argument of type double, returning a result of a double type
2 in square and circle, the area () abstraction of the shape interface is realized, and the square and circular areas are calculated, and the square and
circle objects are created in the test class, and the square area with the edge length 2 and the circular area with a radius of 3 are computed.


Package experiment.cpm;

Interface Shape {
	double area (double n);
}
public class Square implements Shape {public
	double area (double N)
	{return
		n*n;
	}
	
}
public class Circle implements Shape {public
	double area (double N) {return
		3.14*n*n;
	}
}
public class Test {public

	static void Main (string[] args) {
		double s1,s2;
		Shape circle=new Circle ();
		S1=circle.area (3);
		Shape Square=new Square ();
		S2=square.area (2);
		System.out.println ("Square area:" +s2);
		System.out.println ("Round area is:" +S1);
	}

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.