编写Java程序

编写Java程序,第1张

public class Circle {

private double r;

public Circle() {

   thisr = 0;

}

public Circle(double r) {

thisr = r;

}

public double getR() {

return r;

}

public void setR(double r) {

thisr = r;

}

/

  获取圆的面积

 /

private double getArea() {

return MathPIrr;

}

/

  获取圆的周长

 /

private double getPerimeter() {

return MathPIr2;

}

/

  输出

 /

public void show() {

Systemoutprintln("圆的半径为:"+r+",周长为:"+getPerimeter()+",面积为:"+getArea());

}

}

public class Cylinder extends Circle {

private double height;

public Cylinder(double r ,double height) {

    thissetR(r);

thisheight = height;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

thisheight = height;

}

/

  计算圆柱体的体积

 /

private double  getVolumn() {

return MathPIthisgetR()thisgetR()height;

}

/

  显示圆柱体的体积

 /

public void showVolumn() {

Systemoutprintln("半径为:"+thisgetR()+",圆柱体的体积为:"+getVolumn() );

}

}

测试类:

public class MainTest {

public static void main(String[] args) {

Circle c = new Circle(20);

cshow();

Cylinder cy = new Cylinder(20,40);

cyshowVolumn();

}

}

结果:

圆的半径为:20,周长为:12566370614359172,面积为:12566370614359172

半径为:20,圆柱体的体积为:5026548245743669

三种方法:

1在IDE中,如eclipse中写的Java程序,在点击保存后eclipse就会调用javac编译程序编译,编译文件在当前项目的bin目录下。

2作为一个独立的文件且没有定义Java环境变量,需要在cmd窗口中切换到java bin目录下执行Javac程序,执行格式为     javac 空格 Java源文件;如 javac  C://hellojava;

3作为一个独立的文件且定义了Java环境变量,打开cmd窗口,可以在任意目录输入javac java源文件,如javac D://hijava。

扩展资料

Java也是一种高级语言,要让计算机执行你撰写的Java程序,也得通过编译程序的编译。但是Java编译程序并不直接将Java源代码编译为相依于计算机平台的0、1序列,而是将其编译为字节码。

Java源代码的扩展名为java,经过编译程序编译之后生成扩展名为class的字节码。

这个问题,太难!

在当地,找个老师教一下吧。

1、安装JDK

2、

//Hellojava

public class Hello{

public static void main(String [] argv){

Systemoutprintln("Hello workd!");

}

}

3、编译 javac Hellojava

4、运行 java Hello

3:

public class Test{

public static void main(String[] args)

{

A a=new A();

af();

B b=new B();

bf();

bg();

}

}

class A

{

public static void f(){

char cStart='A',cEnd='Z';

for(char c=cStart;c<=cEnd;c++){

Systemoutprint(" "+c);

}

Systemoutprintln();

}

}

class B extends A

{

public static void g(){

char cStart='α',cEnd='ω';

for(char c=cStart;c<=cEnd;c++){

Systemoutprint(" "+c);

}

Systemoutprintln();

}

}

///输出结果为

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ τ υ φ χ ψ ω

////---------------------------------------------------------------------/////

4

class Num

{

public static void main(String[] args)

{

A a=new A();

int max=af(15,45);

Systemoutprintln("两个数的最大公约数为"+max);

B b=new B();

int min=bf(15,45);

Systemoutprintln("两个数的最小公倍数为"+min);

}

}

class A

{

public int f( int a, int b){

int m,n;

if(a<b){

m=a;

a=b;

b=m;

}

n=a%b;

while(n!=0){

a=b;

b=n;

n=a%b;

}

return b;

}

}

class B extends A

{

public int f(int a,int b){

int m=superf(a,b);

return ab/m;

}

}

///输出结果为

两个数的最大公约数为15

两个数的最小公倍数为4

以上就是关于编写Java程序全部的内容,包括:编写Java程序、如何编译java程序、怎么编写java程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9275509.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-26
下一篇2023-04-26

发表评论

登录后才能评论

评论列表(0条)

    保存