@Override
public double getArea(double r) {
return Math.PI*r*r;
}
@Override
public double getCircumference(double r) {
return 2*Math.PI*r;
}
}
package com.dream.zhangsan;
import com.dream.Shapeable;
public class SemiCircle implements Shapeable{
@Override
public double getArea(double r) {
return Math.PI*r*r/2;
}
@Override
public double getCircumference(double r) {
return 2*r + Math.PI*r;
}
}
package com.dream.zhangsan;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Train1 {
public static void main(String[] args) throws IOException {
System.out.println("==== 实现图形接口的圆和半圆 ====");
System.out.println("请输入半径(直接按回车结束程序):");
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = "";
do {
str = bf.readLine();
if (str.length() != 0) {
showMsg(Double.parseDouble(str));
}else {
System.out.println("程序结束!");
}
} while (str.length() != 0);
bf.close();
}
private static void showMsg(double r) {
Circle circle = new Circle();
SemiCircle semiCircle = new SemiCircle();
System.out.printf("圆面积 %.2f、周长 %.2f \n半圆面积 %.2f、周长 %.2f\n", circle.getArea(r), circle.getCircumference(r), semiCircle.getArea(r), semiCircle.getCircumference(r));
System.out.println("请输入半径(直接按回车结束程序):");
}
}
//========================================
package com.dream.lisi;
import com.dream.Shapeable;
public class Square implements Shapeable {
@Override
public double getArea(double r) {
return r*r;
}
@Override
public double getCircumference(double r) {
return r*4;
}
}
package com.dream.lisi;
import com.dream.Shapeable;
public class Cube implements Shapeable {
@Override
public double getArea(double r) {
return r*r*6;
}
@Override
public double getCircumference(double r) {
return r*12;
}
}
package com.dream.lisi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Train2 {
public static void main(String[] args) throws IOException {
System.out.println("==== 实现图形接口的正方形和正方体 ====");
System.out.println("请输入半径(直接按回车结束程序):");
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = "";
do {
str = bf.readLine();
if (str.length() != 0) {
showMsg(Double.parseDouble(str));
}else {
System.out.println("程序结束!");
}
} while (str.length() != 0);
bf.close();
}
private static void showMsg(double r) {
Square square = new Square();
Cube cube = new Cube();
System.out.printf("正方形面积 %.2f、周长 %.2f \n正方体表面积 %.2f、周长 %.2f\n", square.getArea(r), square.getCircumference(r), cube.getArea(r), cube.getCircumference(r));
System.out.println("请输入半径(直接按回车结束程序):");
}
}
怎么用JAVA画半个圆或者是半圆?。?/h2>画圆一般通过继承JPanel 或者JFrame,通过调用panel或者frame中的Graphics实例完成画图 。
java绘图的基本原理:画一个圆
import javax.swing.*;
import java.awt.*;
public class DrawCicle extends JFrame{MyPanel mp=null;public static void main(String[] args) {DrawCicle dc=new DrawCicle();}
public DrawCicle(){mp =new MyPanel();this.add(mp);this.setSize(300, 250);
this.setLocation(600, 300);this.setDefaultCloseOperation(this.EXIT_ON_CLOSE)this.setVisible(true);}}
推荐阅读
- 电脑角色扮演中文游戏,电脑角色扮演中文游戏叫什么
- nohbase,nohbase master found
- idc虚拟主机维修,虚拟主机
- flutter安装android,flutter androidview
- linux命令背景变白 linux换背景
- oracle数据库超过4000长度,oracle字段长度超过4000
- 波音飞机飞行模拟游戏,飞机飞行模拟下载
- 路由器搭建网站,路由器部署网站
- java教材订购代码 java购买商品代码