189 8069 5689

java电脑源代码 java程序源代码用什么命令运行程序

Java100行以上源代码,至少五个class以及一个interface,可以简单点?

下面是一个可能的Java源代码,它包含了一个接口租冲薯(Shape)和五个类(Circle, Rectangle, Triangle, Square 和 Main)。它的功能是计算不同形状的面积和周长。

创新互联建站是一家专业提供汤旺企业网站建设,专注与做网站、网站建设H5高端网站建设、小程序制作等业务。10年已为汤旺众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

//定义一个接口Shape,有两判指个抽象方法:getArea()和getPerimeter()interface Shape { double getArea(); double getPerimeter();

}//定义一个类Circle,实现Shape接口class Circle implements Shape { //定义一个私有属性radius,表示圆的半径

private double radius; //定义一个公有构造方法,用于初始化radius

public Circle(double radius) { this.radius = radius;

} //实现getArea()方法,返回圆的面积

public double getArea() { return Math.PI * radius * radius;

} //实现getPerimeter()方法,返回圆的周长

public double getPerimeter() { return Math.PI * radius * 2;

}

}//定义一个类Rectangle,实现Shape接口class Rectangle implements Shape { //定义两个私有属性width和height,表示矩形的宽度和高度

private double width; private double height; //定义一个公有构造方法,用于初始化width和height

public Rectangle(double width, double height) { this.width = width; this.height = height;

} //实现getArea()方法,返回矩形的面积

public double getArea() { return width * height;

} //实现getPerimeter()方法,返回矩形的周长

public double getPerimeter() { return (width + height) *2;

}

}//定义一个类Triangle,实现Shape接口class Triangle implements Shape { //定义三个私有属性a,b,c表示三角形的三条边长

private double a; private double b; private double c; //定义一个公有构造方法,用于初始化a,b,c,并检查是否满足三角形条件(任意两边之和大于第三边)

public Triangle(double a, double b, double c) throws Exception{ if (a + b c a + c b b + c a) {

this.a = a; this.b = b;

this.c = c;

} else {

throw new Exception("Invalid triangle");

}

} //实现getArea()方法,返回三角形的面积(使用海伦公式)

public double getArea() { //计算半周长p

double p = (a + b + c) /2; //计算并返回面积s(使用Math.sqrt()函数求平方根)

return Math.sqrt(p * (p - a) * (p - b) * (p - c));

} //实现getPerimeter()方法,返回三角形的周长

public double getPerimeter(){ return a + b + c;

}

}//定义一个类Square,继承Rectangle类,并重写构造方法和toString()方法class Square extends Rectangle { //重写构造方法,在调用父类构造方法时传入相弊者同的参数side作为width和height

public Square(double side){ super(side, side);

} //重写toString()方法,在原来基础上加上"Square:"前缀,并只显示side属性而不显示width和height属性(使用String.format()函数格式化字符串)

@Override

public String toString(){ return String.format("Square: side=%.2f", super.width); /* 或者直接使用super.getPerimeter()/4作为side */

/* return String.format("Square: side=%.2f", super.getPerimeter()/4); */

/* 注意:不能直接访问super.side属性,

怎么在电脑上运行Java源程序代码

首先你返灶要在你的电脑上安装jdk。你可以在后面链接地址下载适合你自己的版本(),如果这个链接过期了,请在这个首先找一找。

在你的电脑上配置java环境变量,主要是配置path和classpath。你可以百度java环境变量配置,可以找到很多java环境变量配置方法。配置完毕,可以在cmd窗口下用java -version来查看是否配置成功。如果显示出java版本相关的信息表示配置成功,可以进行下一步了。

编译你的源代码,cmd窗口下把路径改变(cd)到你源代码文件所在的路径,然后用javac 源文件名编译,例如javac Hello.java(需要注意的是源文件名需要是你文件public类的类名,如果你的文件有public类的话)。当然你也可以不改变(cd)到源文件所在的路径,你的文件就需要加上绝对路径就可以了。例如:javac e:\src\Hello.java.

运行你编译并世瞎好的文件,java Hello(需要注意运行的时候没有后缀.java或者.class),同样你可以不改变路径用绝对路径运行,例如:java e:\src\Hello.如果你的绝空代码中有窗口这样的类似的图形化界面,你就需要用javaw来运行。

另外,你可以使用eclipse,NetBeans这样的集成开发环境(IDE)来写代码,这样方便很多。

求JAVA简易计算机源代码

试试这个

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.applet.*;

public class SZJSQ extends JApplet implements ActionListener

{

boolean i=true;

private JButton num0=new JButton("0");

private JButton num1=new JButton("明兆1");

private JButton num2=new JButton("2");

private JButton num3=new JButton("3");

private JButton num4=new JButton("4");

private JButton num5=new JButton("5");

private JButton num6=new JButton("6"激胡租);

private JButton num7=new JButton("7");

private JButton num8=new JButton("8");

private JButton num9=new JButton("9");

private JButton zuok=new JButton("(");

private JButton youk=new JButton(")");

private JButton dian=new JButton(".");

private JButton NULL=new JButton("N");

private JButton plu=new JButton("+");

private JButton min=new JButton("-");

private JButton mul=new JButton("x");

private JButton div=new JButton("/");

private JButton equ=new JButton("=");

private JButton cle=new JButton("C");//做或清除

private JTextField space=new JTextField(30);

public void init()

{

JPanel text=new JPanel();

text.setLayout(new FlowLayout());

text.add(space);

JPanel buttons=new JPanel();

buttons.setLayout(new GridLayout(5,4));

buttons.add(num9);

buttons.add(num8);

buttons.add(num7);

buttons.add(plu);

buttons.add(num6);

buttons.add(num5);

buttons.add(num4);

buttons.add(min);

buttons.add(num3);

buttons.add(num2);

buttons.add(num1);

buttons.add(mul);

buttons.add(num0);

buttons.add(cle);

buttons.add(equ);

buttons.add(div);

buttons.add(zuok);

buttons.add(youk);

buttons.add(dian);

buttons.add(NULL);

(num9).addActionListener(this);

(num8).addActionListener(this);

(num7).addActionListener(this);

(num6).addActionListener(this);

(num5).addActionListener(this);

(num4).addActionListener(this);

(num3).addActionListener(this);

(num2).addActionListener(this);

(num1).addActionListener(this);

(num0).addActionListener(this);

(plu).addActionListener(this);

(min).addActionListener(this);

(mul).addActionListener(this);

(div).addActionListener(this);

(equ).addActionListener(this);

(cle).addActionListener(this);

(zuok).addActionListener(this);

(youk).addActionListener(this);

(dian).addActionListener(this);

setLayout(new BorderLayout());

add("North",text);

add("South",buttons);

space.setText("0");

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==num9)

{

if(i==true)

{

space.setText("9");

i=false;

}

else space.setText(space.getText()+'9');

}

if(e.getSource()==num8)

{

if(i==true)

{

space.setText("8");

i=false;

}

else space.setText(space.getText()+'8');

}

if(e.getSource()==num7)

{

if(i==true)

{

space.setText("7");

i=false;

}

else space.setText(space.getText()+'7');

}

if(e.getSource()==num6)

{

if(i==true)

{

space.setText("6");

i=false;

}

else space.setText(space.getText()+'6');

}

if(e.getSource()==num5)

{

if(i==true)

{

space.setText("5");

i=false;

}

else space.setText(space.getText()+'5');

}

if(e.getSource()==num4)

{

if(i==true)

{

space.setText("4");

i=false;

}

else space.setText(space.getText()+'4');

}

if(e.getSource()==num3)

{

if(i==true)

{

space.setText("3");

i=false;

}

else space.setText(space.getText()+'3');

}

if(e.getSource()==num2)

{

if(i==true)

{

space.setText("2");

i=false;

}

else space.setText(space.getText()+'2');

}

if(e.getSource()==num1)

{

if(i==true)

{

space.setText("1");

i=false;

}

else space.setText(space.getText()+'1');

}

if(e.getSource()==num0)

{

if(i==true)

{

space.setText("0");

i=false;

}

else space.setText(space.getText()+'0');

}

if(e.getSource()==zuok)

{

if(i==true)

{

space.setText("(");

i=false;

}

else space.setText(space.getText()+'(');

} if(e.getSource()==youk)

{

if(i==false)

space.setText(space.getText()+')');

}

if(e.getSource()==dian)

{

if(i==false)

space.setText(space.getText()+'.');

}

if(e.getSource()==plu)

{

space.setText(space.getText()+'+');

i=false;

}

if(e.getSource()==min)

{

space.setText(space.getText()+'-');

i=false;

}

if(e.getSource()==mul)

{

space.setText(space.getText()+'*');

i=false;

}

if(e.getSource()==div)

{

space.setText(space.getText()+'/');

i=false;

}

if(e.getSource()==equ)

{

space.setText(String.valueOf(Calculator(space.getText())));

i=true;

}

if(e.getSource()==cle)

{

space.setText("0");

i=true;

}

}

public double Calculator(String f)//科学计算

{

int i=0,j=0,k;

char c;

StringBuffer s=new StringBuffer();

s.append(f);

s.append('=');

String formula=s.toString();

char[] anArray;

anArray=new char[50];

StackCharacter mystack=new StackCharacter();

while(formula.charAt(i)!='=')

{

c=formula.charAt(i);

switch(c)

{

case '(': mystack.push(new Character(c));

i++;

break;

case ')':

while(mystack.peek().charValue()!='(')

{

anArray[j++]=mystack.pop().charValue();

}

mystack.pop();

i++;

break;

case '+':

case '-':

while(!mystack.empty()mystack.peek().charValue()!='(')

{

anArray[j++]=mystack.pop().charValue();

}

mystack.push(new Character(c));

i++;

break;

case '*':

case '/':

while(!mystack.empty()(mystack.peek().charValue()=='*'||mystack.peek().charValue()=='/'))

{

anArray[j++]=mystack.pop().charValue();

}

mystack.push(new Character(c));

i++;

break;

case' ': i++;

break;

default: while((c='0'c='9')||c=='.')

{

anArray[j++]=c;

i++;

c=formula.charAt(i);

}

anArray[j++]='#';

break;

}

}

while(!(mystack.empty()))

anArray[j++]=mystack.pop().charValue();

i=0;

int count;

double a,b,d;

StackDouble mystack1 =new StackDouble();

while(ij)

{

c=anArray[i];

switch(c)

{

case '+':

a=mystack1.pop().doubleValue();

b=mystack1.pop().doubleValue();

d=b+a;

mystack1.push(new Double(d));

i++;

break;

case '-':

a=mystack1.pop().doubleValue();

b=mystack1.pop().doubleValue();

d=b-a;

mystack1.push(new Double(d));

i++;

break;

case '*':

a=mystack1.pop().doubleValue();

b=mystack1.pop().doubleValue();

d=b*a;

mystack1.push(new Double(d));

i++;

break;

case '/':

a=mystack1.pop().doubleValue();

b=mystack1.pop().doubleValue();

if(a!=0)

{

d=b/a;

mystack1.push(new Double(d));

i++;

}

else

{

System.out.println("Error!");

}

break;

default:

d=0;count=0;

while((c='0'c='9'))

{

d=10*d+c-'0';

i++;

c=anArray[i];

}

if(c=='.')

{

i++;

c=anArray[i];

while((c='0'c='9'))

{

count++;

d=d+(c-'0')/Math.pow(10,count);

i++;

c=anArray[i];

}

}

if(c=='#')

mystack1.push(new Double(d));

i++;

break;

}

}

return(mystack1.peek().doubleValue());

}

}

eclipse怎么查看java源代码

在Eclipse中查看JDK类库的源代码

设置:

1.点 “window”- "Preferences" - "Java" - "Installed JRES"

2.此时"Installed JRES"睁败渗右边是列表窗格,列出了系统中的 JRE 环境,选择你的JRE,然后点边上的 "Edit...", 会出现一个窗口(Edit JRE)

3.选中rt.jar文件的这一项:“c:\program files\java\jre_1.5.0_06\lib\rt.jar” 

点 左边的“+” 号展开它

4.展开后,可以看到“Source Attachment:(none)”,点这一项,点右边的按钮“Source Attachment...”, 选择你的JDK目录下的 “src.zip”文件

5.一路点"ok",结束。

dt.jar是关于运行环境的类库,主要是swing的包 

tools.jar是关于一些工具的类库 

rt.jar包含了jdk的基础类库,也就枯孙是你在java doc里面看到的所有的类的class文件

使用:

可以在 Java 源代码编辑器或代码片段编辑测试窗中选择类型、方法或字段的名称,然后对元素的定义打开编辑器。

在 Java 编辑器中,选择类型、方法或字段的名称。您也可以仅仅在名称中单击一次。 

执行下列其中悉脊一项操作: 

1.从菜单栏中,选择浏览 打开声明 

2.从编辑器的弹出菜单中,选择打开声明 

3.按 F3 键,如下图


本文标题:java电脑源代码 java程序源代码用什么命令运行程序
标题网址:http://cdxtjz.cn/article/ddpscdj.html

其他资讯