Java³ÌÐòÉè¼Æ×ܸ´Ï°Ìâ 1¡¢±àдһ¸öJava³ÌÐòÔÚÆÁÄ»ÉÏÊä³ö¡°ÄãºÃ£¡¡±¡££¨p13,Àý1-1£© //programme name Helloworld.java public class Helloworld {
public static void main(String args[]) { } }
2. ±àдһ¸öJava³ÌÐò£¬ÓÃif-elseÓï¾äÅжÏijÄê·ÝÊÇ·ñΪÈòÄê¡£ // Programme Name LeapYear.java public class LeapYear{
public static void main(String args[]){ int year=2010; if(args.length!=0)
year=Integer.parseInt(args[0]);
if((year%4==0 && year0!=0)||(year@0==0)) ÄêÊÇÈòÄê¡£\else
Äê²»ÊÇÈòÄê¡£\ }
}//if-elseÓï¾ä
3¡¢±àдһ¸öJava³ÌÐòÔÚÆÁÄ»ÉÏÊä³ö1£¡+2£¡+3£¡+¡¡+10£¡µÄºÍ¡££¨p64,Àý2-2£© // programme name ForTest.java public class ForTest {
public static void main( String args[] ) { int i,j,mul,sum=0; for(i=1;i<=10;i++) { mul=1;
for(j=1,j<=i;j++) { mul=mul*j; }
sum=sum+mul;
}
¡°1£¡+2£¡+3£¡+¡¡+10£¡= ¡±+sum); } }
4. £¨1£©±àдһ¸öÔ²ÀàCircle£¬¸ÃÀàÓµÓУº
¢ÙÒ»¸ö³ÉÔ±±äÁ¿
Radius£¨Ë½ÓУ¬¸¡µãÐÍ£©£» // ´æ·ÅÔ²µÄ°ë¾¶£»
¢ÚÁ½¸ö¹¹Ôì·½·¨
Circle( ) // ½«°ë¾¶ÉèΪ0
Circle(double r ) //´´½¨Circle¶ÔÏóʱ½«°ë¾¶³õʼ»¯Îªr
¢Û Èý¸ö³ÉÔ±·½·¨
double getArea( ) //»ñÈ¡Ô²µÄÃæ»ý double getPerimeter( ) //»ñÈ¡Ô²µÄÖܳ¤
void show( ) //½«Ô²µÄ°ë¾¶¡¢Öܳ¤¡¢Ãæ»ýÊä³öµ½ÆÁÄ» £¨2£©±àдһ¸öÔ²ÖùÌåÀàCylinder£¬Ëü¼Ì³ÐÓÚÉÏÃæµÄCircleÀà¡£»¹ÓµÓУº
¢ÙÒ»¸ö³ÉÔ±±äÁ¿
double hight£¨Ë½ÓУ¬¸¡µãÐÍ£©£» // Ô²ÖùÌåµÄ¸ß£»
¢Ú¹¹Ôì·½·¨
Cylinder (double r, double h ) //´´½¨Circle¶ÔÏóʱ½«°ë¾¶³õʼ»¯Îªr
¢Û ³ÉÔ±·½·¨
double getVolume( ) //»ñÈ¡Ô²ÖùÌåµÄÌå»ý
void showVolume( ) //½«Ô²ÖùÌåµÄÌå»ýÊä³öµ½ÆÁÄ» ±àдӦÓóÌÐò£¬´´½¨ÀàµÄ¶ÔÏ󣬷ֱðÉèÖÃÔ²µÄ°ë¾¶¡¢Ô²ÖùÌåµÄ¸ß£¬¼ÆËã²¢·Ö±ðÏÔʾԲ°ë¾¶¡¢Ô²Ãæ»ý¡¢Ô²Öܳ¤£¬Ô²ÖùÌåµÄÌå»ý¡£ //Programme Name TestCylinder.java
class Circle { //¶¨Ò常Àà--Ô°Àà
private double radius; //³ÉÔ±±äÁ¿--Ô°°ë¾¶ Circle() { //¹¹Ôì·½·¨ }
Circle(double r) { //¹¹Ôì·½·¨ }
double getPerimeter() { //³ÉÔ±·½·¨--ÇóÔ°Öܳ¤ }
double getArea() { //³ÉÔ±·½·¨--ÇóÔ°Ãæ»ý }
void disp() { //³ÉÔ±·½·¨--ÏÔʾ԰°ë¾¶¡¢Öܳ¤¡¢Ãæ»ý }
return Math.PI*radius*radius; return 2*Math.PI*radius; radius=r; radius=0.0;
}
class Cylinder extends Circle { //¶¨Òå×ÓÀà--Ô²ÖùÀà }
public class TestCylinder { //¶¨ÒåÖ÷Àà
public static void main(String[] args) { //Ö÷³ÌÈë¿Ú Circle Ci=new Circle(10.0); // Éú³ÉÔ°ÀàʵÀý Ci.disp(); // µ÷ÓÃÔ°ÀàµÄ·½·¨
Cylinder Cyl=new Cylinder(5.0,10.0); //Éú³ÉÔ²ÖùÀàʵÀý Cyl.disp(); //µ÷Óø¸Àà·½·¨ Cyl.dispVol(); //µ÷ÓÃ×ÓÀà·½·¨ } }
private double hight; //³ÉÔ±±äÁ¿--Ô°Öù¸ß Cylinder(double r,double h) { //¹¹Ôì·½·¨ }
public double getVol() { //³ÉÔ±·½·¨--ÇóÔ°ÖùÌå»ý }
public void dispVol() { //³ÉÔ±·½·¨--ÏÔʾ԰ÖùÌå»ý }
return getArea()*hight; super(r); hight=h;
5¡¢±àдһ¸öJavaÓ¦ÓóÌÐò£¬´Ó¼üÅ̶ÁÈ¡Óû§ÊäÈëÁ½¸ö×Ö·û´®£¬²¢ÖØÔØ3¸öº¯Êý·Ö±ðʵÏÖÕâÁ½¸ö×Ö·û´®µÄÆ´½Ó¡¢ÕûÊýÏà¼ÓºÍ¸¡µãÊýÏà¼Ó¡£Òª½øÐÐÒì³£´¦Àí£¬¶ÔÊäÈëµÄ²»·ûºÏÒªÇóµÄ×Ö·û´®Ìáʾ¸øÓû§£¬²»ÄÜʹ³ÌÐò±ÀÀ£¡££¨p39£¬Àý2-10,2-11£© //programme name Strinput.java import java.io.*; public class Strinput {
public static void main(String args[]) { String s1,s2,ss,si,sf; int i1,i2; float f1,f2; BufferedReader strin=new
InputStreamReader(System.in));
try{
s1= strin.readLine(); s2= strin.readLine();} c
i1 = Integer.parseInt(s1); i2 = Integer.parseInt(s2); f1 = Float.parseFloat(s1); f2 = Float.parseFloat(s2); ss = strAdd(s1,s2); si = strAdd(i1,i2); sf = strAdd(f1,f2); }
String strAdd(String str1,String str2) {
return str1+str2; }
String strAdd(int int1,int int2) { return String.valueOf(int1+int2); }
String strAdd(float flt1,float flt2) { return String.valueOf (flt1+flt2); } }
BufferedReader(new