⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 101.txt

📁 Java 2实用教程(第三版) 清华大学出版社 (编著 耿祥义 张跃平) 例子源代码 第四章
💻 TXT
📖 第 1 页 / 共 2 页
字号:
}
class 堆 
{  图形 底;
   double 高;
   堆(图形 底,double 高)
 {  this.底=底;
      this.高=高;
   }
   void 换底(图形 底)
 {  this.底=底;
   }
   public double 求体积()
 {  return (底.求面积()*高)/3.0;
   }
}
public class Example4_22
{  public static void main(String args[]) 
{  堆 zui;
      图形 tuxing;
      tuxing=new 梯形(2.0,7.0,10.7);
      System.out.println("梯形的面积"+tuxing.求面积());
      zui=new  堆(tuxing,30);
      System.out.println("梯形底的堆的体积"+zui.求体积());
      tuxing=new 圆形(10);
      System.out.println("半径是10的圆的面积"+tuxing.求面积());
      zui.换底(tuxing);
      System.out.println("圆形底的堆的体积"+zui.求体积());
   }
}
例子23
class Student 
{  int number;String name;
Student()
   {  
   }
   Student(int number,String name)
 {  this.number=number;
this.name=name;
      System.out.println("I am "+name+ "my number is "+number);
   }
 }
class Univer_Student extends Student 
{  boolean 婚否;
   Univer_Student(int number,String name,boolean b) 
 {  super(number,name);
      婚否=b;
      System.out.println("婚否="+婚否);
   }
 }
public class Example4_23 
 {  public static void main(String args[])
 {  Univer_Student zhang=new Univer_Student(9901,"和晓林",false);
   }
}
例子24
class Sum
{  int n;
    float f() 
{  float sum=0;
       for(int i=1;i<=n;i++)
          sum=sum+i;
      return sum;  
    }
}
class Average extends Sum
{  int n;  
   float f() 
{  float c;
      super.n=n;
      c=super.f();
      return c/n; 
   }
   float g()
 {  float c;
     c=super.f();
      return c/2; 
   }
 }
public class Example4_24
 {  public static void main(String args[])
 {  Average aver=new Average();
       aver.n=100;
       float result_1=aver.f();
       float result_2=aver.g();
       System.out.println("result_1="+result_1);
       System.out.println("result_2="+result_2);
   }
}
例子25
interface Computable
{  int MAX=100;
   int f(int x);
}
class China implements Computable
{  int number;
   public  int f(int x) //不要忘记public关键字
 {  int sum=0;
      for(int i=1;i<=x;i++)
 { sum=sum+i;
       }
       return sum;
   }
}
class Japan implements Computable
{  int number;
   public int f(int x)
 {  return 44+x; 
   }
}
public class Example4_25 
{  public static void main(String args[]) 
{  China zhang; 
      Japan henlu;
      zhang=new China();   
      henlu=new Japan();  
      zhang.number=991898+Computable.MAX; 
      henlu.number=941448+Computable.MAX;
      System.out.println("number:"+zhang.number+"求和"+zhang.f(100));
      System.out.println("number:"+henlu.number+"求和"+henlu.f(100));
   }
}      
例子26
interface  收费 
{  public void  收取费用();
}
interface  调节温度 
{  public void  controlTemperature();
}
class 公共汽车 implements 收费
{  public  void 收取费用()
{  System.out.println("公共汽车:一元/张,不计算公里数");
    }
} 
class 出租车 implements 收费, 调节温度
{  public void 收取费用() 
{  System.out.println("出租车:1.60元/公里,起价3公里");
    }
public void  controlTemperature() 
{  System.out.println("安装了Hair空调");
    }
}
class 电影院 implements 收费,调节温度
{   public void 收取费用()
 {  System.out.println("电影院:门票,十元/张");
    }
public void  controlTemperature()
{  System.out.println("安装了中央空调");
    }
}
class Example4_26
{   public static void main(String args[])
 {  公共汽车 七路=new 公共汽车();
       出租车   天宇=new 出租车();
       电影院   红星=new 电影院();
       七路.收取费用();
       天宇.收取费用();
       红星.收取费用();
       天宇.controlTemperature();
       红星.controlTemperature();
    }
}
例子27
interface  ShowMessage
{   void 显示商标(String s);
}
class TV implements ShowMessage
{  public void 显示商标(String s)
 {  System.out.println(s);
   }
}
class PC implements ShowMessage
{  public void 显示商标(String s)
 {  System.out.println(s);
   }
}
public class Example4_27 
{  public static void main(String args[])
 {  ShowMessage sm;                  
      sm=new TV();                     
      sm.显示商标("长城牌电视机");     
      sm=new PC();                     
      sm.显示商标("联想奔月5008PC机"); 
   } 
}
例子28
interface  Computerable
{  public  double 求面积();
}
class 梯形 implements Computerable
{  double a,b,h;
梯形(double a,double b,double h) 
{  this.a=a;
this.b=b;
this.h=h;
    }
public double 求面积()
 {  return((1/2.0)*(a+b)*h);
     }
}
class 圆形 implements Computerable
{  double r;
   圆形(double r)
 {  this.r=r;
   }
   public double 求面积()
 {  return(3.14*r*r);
   }
}
class 堆 
{ Computerable 底;          
   double 高;
   堆(Computerable 底,double 高) 
{  this.底=底;
      this.高=高;
   }
   void 换底(Computerable 底)
 {  this.底=底;
   }
public double 求体积()
{  return (底.求面积()*高)/3.0;
   }
}
public class Example4_28 
{  public static void main(String args[])
 {  堆 zui;
      Computerable bottom;
      bottom=new 梯形(2.0,7.0,10.7); 
System.out.println("梯形的面积"+bottom.求面积());
      zui=new  堆(bottom,30);
      System.out.println("梯形底的堆的体积"+zui.求体积());
      bottom=new 圆形(10);  
  System.out.println("半径是10的圆的面积"+bottom.求面积());
  zui.换底(bottom);
      System.out.println("圆形底的堆的体积"+zui.求体积());
   }
}
例子29
interface SpeakHello 
{   void speakHello();
}
class Chinese implements SpeakHello
{  public  void speakHello() 
{  System.out.println("中国人习惯问候语:你好,吃饭了吗? ");
   }
}
class English implements SpeakHello
{   public  void speakHello()
 {  System.out.println("英国人习惯问候语:你好,天气不错 ");
    }
}
class KindHello
{  public void lookHello(SpeakHello hello)  
   {  hello.speakHello();                   
   }
}
public class Example4_29
{  public static void main(String args[]) 
 {  KindHello kindHello=new KindHello();
      kindHello.lookHello(new Chinese());
      kindHello.lookHello(new English());
   }
}
例子30
class China
{   final String nationalAnthem="义勇军进行曲";
    Beijing beijing;                  
    China()
    {  beijing=new Beijing();
    }
    String getSong()
    {  return nationalAnthem;
    }
   class Beijing           
    {   String name="北京";
        void speak()
        {  System.out.println("我们是"+name+" 我们的国歌是:"+getSong());
        }
    }
}
public class Example4_30 
{ public static void main(String args[])
 {  China china=new China();
      china.beijing.speak();
   }   
}   
例子31
class Cubic 
{  double getCubic(int n)
    { return 0;
    }
}
abstract class Sqrt
{  public abstract double getSqrt(int x);
}
class A
{   void f(Cubic cubic)
    {  double result=cubic.getCubic(3);
       System.out.println(result);
    } 
}
public class Example4_31
{  public static void main(String args[])
   {  A a=new A();
      a.f(new Cubic()         
           {  double getCubic(int n)
              {   return n*n*n;
              }
           }
         );
     Sqrt ss=new Sqrt()                
            { public double getSqrt(int x) 
               {  return Math.sqrt(x);     
               }
            };
     double m=ss.getSqrt(5);   
     System.out.println(m);
   } 
}
例子32
interface Cubic 
{   double getCubic(int n);
}
interface  Sqrt
{  public double getSqrt(int x);
}
class A
{   void f(Cubic cubic)
    {  double result=cubic.getCubic(3); 
       System.out.println(result);
    } 
}
public class Example4_32
{  public static void main(String args[])
   {  A a=new A();
      a.f(new Cubic()  
           {  public double getCubic(int n)
              {  return n*n*n;
              }
           }
         );
     Sqrt ss=new Sqrt()
            {  public double getSqrt(int x) 
               {  return Math.sqrt(x);      
               }
            };
     double m=ss.getSqrt(5); 
     System.out.println(m);
   } 
}
例子33
public class Example4_33
{   public static void main(String args[ ]) 
    { int n=0,m=0,t=555;
      try{  m=Integer.parseInt("8888");
            n=Integer.parseInt("abc789");
            t=9999; 
         }
      catch(NumberFormatException e)
         {  System.out.println("发生异常:"+e.getMessage());
            e.printStackTrace();
            n=123;
         }
      System.out.println("n="+n+",m="+m+",t="+t);
    }
}
例子34
class NopositiveException extends Exception
{   String message;
    NopositiveException(int m,int n)
    {   message="数字"+m+"或"+n+"不是正整数";
    }
    public String toString()
    {   return message;
    }
}
class Computer
{   public int getMaxCommonDivisor(int m,int n) throws NopositiveException
    { if(n<=0||m<=0)
        {  NopositiveException exception=new NopositiveException(m,n);
           throw exception; 
        }
      if(m<n)
        {  int temp=0;
           temp=m;
           m=n;
           n=temp;
        }
      int r=m%n;
      while(r!=0) 
        {  m=n;
           n=r;
           r=m%n;
        }
      return n;
    }
}
public class Example4_34
{   public static void main(String args[])
    {  int m=24,n=36,result=0;
       Computer a=new Computer();
       try  {  result=a.getMaxCommonDivisor(m,n);
               System.out.println(m+"和"+n+"的最大公约数 "+result);
m=-12;
n=22;
               result=a.getMaxCommonDivisor(m,n);
               System.out.println(m+"和"+n+"的最大公约数 "+result);
            }
       catch(NopositiveException e)
            {  System.out.println(e.toString());
            }
    }
}
例子35
import java.lang.reflect.*;
class Rect 
{  double width,height,area;
   public double getArea()
 {  area=height*width;
      return area;
   }
}
public class Example4_35
{ public static void main(String args[])
 {  Rect rect=new Rect();
     Class cs=rect.getClass();
     String className=cs.getName();
     Constructor[] con=cs.getDeclaredConstructors();
     Field[] field=cs.getDeclaredFields() ;
     Method[] method=cs.getDeclaredMethods();
     System.out.println("类的名字:"+className);
     System.out.println("类中有如下的成员变量:");
     for(int i=0;i<field.length;i++)
{  System.out.println(field[i].toString());
     }
     System.out.println("类中有如下的方法:");
     for(int i=0;i<method.length;i++)
 {  System.out.println(method[i].toString());
     }
     System.out.println("类中有如下的构造方法:");
     for(int i=0;i<con.length;i++)
 {  System.out.println(con[i].toString());
     }
  }  
}
例子36
class Rect 
{ double width,height,area;
  public double getArea()
  {  area=height*width;
     return area;
  }
}
public class Example4_36
{  public static void main(String args[])
 {  try{  Class cs=Class.forName("Rect");
            Rect rect=(Rect)cs.newInstance();
            rect.width=100;
            rect.height=200;
            System.out.println("rect的面积"+rect.getArea());
          }
       catch(Exception e){} 
   }  
}
例子37
public class Example4_37
{  public static void main(String args[ ])
 {  char a[]={'a','b','c','D','E','F'};
      for(int i=0;i<a.length;i++)
 {  if(Character.isLowerCase(a[i]))
 {  a[i]=Character.toUpperCase(a[i]);
          }
       else if(Character.isUpperCase(a[i]))
 {  a[i]=Character.toLowerCase(a[i]);
          }
       }
      for(int i=0;i<a.length;i++) 
{  System.out.print(" "+a[i]);
      }     
   } 
}
Mymoon.mf
Manifest-Version: 1.0
Main-Class: A
Created-By: 1.4(Sun Microsystems Inc.)
Test1.java
public class  Test1
{   public void fTest1() 
{  System.out.println("I am a method In Test1 class");
    }
}
Test2.java
public class Test2
{  public void fTest2()
{  System.out.println("I am a method In Test2 class");
   }
}
moon.mf
Manifest-Version: 1.0
Class: Test1  Test2
Created-By: 1.4(Sun Microsystems Inc.) 
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -