📄 3.txt
字号:
例程3-1
//Circle.java
public class Circle
{
//定义程序运行常量π的值
public static final double PAI = 3.14;
//定义成员变量:圆的半径
public static double radius;
//类的构造函数
public Circle( double radius)
{
Circle.radius = radius;
}
//类的方法成员
public static double getArea()
{
return radius*radius*PAI;
}
public static void main(String[] args)
{
Circle aCircle = new Circle( 2.0 );
double area = Circle.getArea();
System.out.println("圆的面积是:"+ area);
System.exit(0);
}
}
例程3-2
//VariableDemo.java
public class VariableDemo
{
public VariableDemo()
{
}
public void memberForIllustration()
{
int nIntegerValue = 10;
double dDoubleValue = 3.14;
char cCharValue = 'a';
boolean bBoolen = false;
System.out.println("整数变量:"+nIntegerValue+ "双精度变量:"+dDoubleValue+ "字符变量:"+ cCharValue);
}
public static void main(String[] args)
{
VariableDemo aVariableDemo = new VariableDemo();
aVariableDemo.memberForIllustration();
}
}
例程3-3
//IntDemo.java
class IntDemo
{
public static int MAXINTEGER = 100;
public IntDemo()
{
}
public void demoUsing()
{
byte btByteValue = 5;
int nIntegerValue = 10;
short sShortCalue = 20;
long lLongValue=0;
for(;;)
{
lLongValue = lLongValue + btByteValue;
lLongValue = lLongValue + nIntegerValue;
lLongValue = lLongValue + sShortCalue;
if( lLongValue > MAXINTEGER )
break;
}
System.out.println("在本程序中整数变量的最大值:" +lLongValue);
}
public static void main(String[] args)
{
IntDemo aIntDemo = new IntDemo ();
aIntDemo.demoUsing ();
}
}
例程3-4
//RealDemo.java
class RealDemo
{
public static float fPAI = 3.14f;
public float fRadius;
public double dTotalLength;
public RealDemo( float fRadius )
{
this.fRadius = fRadius;
}
public double computeLength()
{
dTotalLength = 2*fPAI*fRadius;
return dTotalLength;
}
public static void main(String[] args)
{
RealDemo aRealDemo = new RealDemo( 5 );
double dLength = aRealDemo.computeLength();
System.out.println("计算获得的圆的周长为:"+dLength);
}
}
例程3-5
// VariableTypeDemo.Java
class VariableTypeDemo
{
//计数器成员变量
public int nCounter;
//部分量成员变量
public float fStep;
//总成员变量
public double dTotal;
//标志字
public boolean bFlag;
//字符成员变量
public char cYesOrNo;
//构造函数
public VariableTypeDemo(int nCounter, float fStep, double dTotal, char cYesOrNo)
{
//根据构造函数的参数,将成员变量初始化赋值
this.nCounter = nCounter;
this.fStep = fStep;
this.dTotal = dTotal;
this.cYesOrNo = cYesOrNo;
}
//取得累加结果的方法
public double getTotal()
{
return dTotal;
}
public double processAccumulate()
{
//循环累加,同时计数器nCounter等于100作为循环终止条件
for( int i=0; i<nCounter; i++)
{
//判断当前的累加条件
if( cYesOrNo == 'N' || !bFlag )
break;
//累加
dTotal += fStep;
//累加终止条件
if( dTotal >= 300 ) bFlag = false;
}
return dTotal;
}
//Java应用程序的main方法
public static void main(String args[])
{
//生成类Variable的实例
VariableTypeDemo usrVariable = new VariableTypeDemo(100,15,0,'y');
//调用实例方法
usrVariable.processAccumulate();
//调用实例方法
double result = usrVariable.getTotal();
System.out.println("经过累加后,小于界限的结果为:"+result);
}
}
例程3-6
//convention.java
class Convention
{
public static void main( String args[ ] )
{
byte bByte = 2;
char cChar = 'c';
int nInt = 10;
float fFloat = 3.5f;
double dDouble = 1.234;
//float OP byte ->float
float fFloatNew = fFloat + bByte;
//char OP int ->int
int nIntNew = cChar + nInt;
//float OP double ->double
double dDoubleNew = fFloatNew/nInt;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -