JAVA 书中第四章的例题

来源:互联网 发布:linux 复制 所有文件 编辑:程序博客网 时间:2024/06/05 11:00
还有学校没有讲到的,也多少写了点:以备后用了:摘出了其中一部分,限于篇幅就不全贴出来了,这毕竟是技术论坛,不该发这些菜鸟级的代码的
package tom.jiafei;


class Tom{


    void speak(){


       System.out.println("Tom类在tom.jiafei包中");  


    }


}


public class Example4_10{


    public static void main(String args[]){


       Tom cat=new Tom();


       cat.speak();


       System.out.println("Example4_10类也在tom.jiafei包中");  


    }


}
import java.util.Date;
public class  Example4_11{
    public static void main(String args[]){
       Date date=new Date();
       System.out.printf("本地机器的时间:\n%s",date);  
    }
}


import tom.jiafei.*;
public class Example4_12{
    public static void main(String args[ ]){
       SquareEquation equation=new SquareEquation(4,5,1);
       equation.getRoots();
       equation.setCoefficient(-3,4,5);
       equation.getRoots();
    }
}


public class Example4_13{
    private int money;
    Example4_13(){
       money=2000;
    } 
    private int getMoney(){
       return money;
    }
    public static void main(String args[ ]){
       Example4_13 exa=new Example4_13(); //对象exa在Example4_13类中 
       exa.money=3000;
       int m=exa.getMoney();
       System.out.println("money="+m);
    }
}
贴图费劲,就不贴图了
class Lader{
    double above,bottom,height;


    Lader(){}


    Lader(double a,double b,double h){


        above=a;


        bottom=b;


        height=h;


    }


    public void setAbove(double a){


        above=a;


    }


    public void setBottom(double b){


        bottom=b;


    }


    public void setHeight(double h){


        height=h;


    }


    double computeArea(){


       return (above+bottom)*height/2.0;


    }


}


public class Example4_1{


  public static void main(String args[]){


       double area1=0,area2=0;


       Lader laderOne,laderTwo;


       laderOne=new Lader();


       laderTwo=new Lader(10,88,20);


       laderOne.setAbove(16);


       laderOne.setBottom(26);


       laderOne.setHeight(100);


       laderTwo.setAbove(300);


       laderTwo.setBottom(500);


       area1=laderOne.computeArea();


       area2=laderTwo.computeArea();


       System.out.println("laderOne的above,bottom和height:"+                                         laderOne.above+","+laderOne.bottom+","+laderOne.height);


       System.out.println("laderOne的面积:"+area1);


       System.out.println("laderTwo的above,bottom和height:"+


                       laderTwo.above+","+laderTwo.bottom+","+laderTwo.height);


       System.out.println("laderTwo的面积:"+area2);


    }


 }




class Lader{
  double above,height;    //实例变量
    static double bottom;   //类变量
    void setAbove(double a){
       above=a;
    }
    void setBottom(double b){
       bottom=b;
    }
    double getAbove(){
       return above;
    }
    double getBottom(){
       return bottom;
    }
}
class Example4_2{
    public static void main(String args[]){
       Lader.bottom=60;    //Lader的字节码被加载到内存,通过类名操作类变量
       Lader laderOne,laderTwo;
       System.out.println("现在所有Lader对象的bottom都是"+Lader.bottom);    
       laderOne=new Lader();  
       laderTwo=new Lader();
       System.out.println("laderOne的bottom:"+laderOne.getBottom());
       System.out.println("laderTwo的bottom:"+laderTwo.getBottom());
       laderOne.setAbove(11);
       laderTwo.setAbove(22);
       laderTwo.setBottom(100);
       System.out.println("现在所有Lader对象的bottom都是"+Lader.bottom);
       System.out.println("laderOne的above:"+laderOne.getAbove());
       System.out.println("laderTwo的above:"+laderTwo.getAbove());
    } 
}
class Tom{
    final int MAX=100;
    static final int MIN=20; 
}
public class Example4_3{
    public static void main(String args[ ]){
        System.out.println(Tom.MIN);
        Tom cat=new Tom();
        int x=0;
        x=Tom.MIN+cat.MAX;
        System.out.println(x);
    }
}


class Tom{
    void f(int x,double y){
        x=x+1;
        y=y+1;
        System.out.printf("参数x和y的值分别是:%d,%3.2f\n",x,y);
    } 
}
public class Example4_5{
    public static void main(String args[]){
        int x=10;
        double y=12.58; 
        Tom cat=new Tom();
        cat.f(x,y);
        System.out.printf("main方法中x和y的值仍然分别是:%d,%3.2f\n",x,y);
    }
}


public class Tom{
    int leg;
    Tom(int n){
       this.cry();   //可以省略this,即将this.cry();写成cry();
       leg=n;
       this.cry(); 
    }
    void cry(){
       System.out.println("我是Tom ,我现在有"+leg+"条腿");
    }
    public static void main(String args[]){
       Tom cat=new Tom(4);  //当调用构造方法Tom时,其中的this就是对象cat
    }
}
还有第四章的一部分代码,第四章例题有点多嘛,这么神奇,看来学过的也要看看这本奇书了:
public class Example4_16{


    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.printf("%6c",a[i]);


      }     


    } 


}




Rectangle.java


public class Rectangle{


    private double x,y,width,height;       


    public void setX(double x){


      this.x=x;


    }


    public double getX(){


      return x;


    }


    public void setY(double y){


      this.y=y;


    }


    public double getY(){


      return y;


    }


    public void setWidth(double width){


      if(width<=0)


        this.width=0;


      else 


        this.width=width;


    }


    public double getWidth(){


      return width;


    }


    public void setHeight(double height){


      if(height<=0)


        height=0;


      else


        this.height=height;


    }


    public double getHeight(){


      return height;


    }


}


Circle.java


public class Circle{


    private double x,y,radius;


    public void setX(double x){


      this.x=x;


    }


    public double getX(){


      return x;


    }


    public void setY(double y){


      this.y=y;


    }


    public double getY(){


      return y;


    }


    public void setRadius(double radius){


      if(radius<0)


        this.radius=0;


      else


       this.radius=radius;


    }


    public double getRadius(){


      return radius;


    }


}


Geometry.java


public class Geometry{


    private Rectangle rect;


    private Circle circle;


    Geometry(Rectangle rect,Circle circle){


      this.rect=rect;


      this.circle=circle;


    }


    public void setCirclePosition(double x,double y){


      circle.setX(x);


      circle.setY(y); 


    }


    public void setCircleRadius(double radius){


      circle.setRadius(radius);


    }


    public void setRectanglePosition(double x,double y){


      rect.setX(x);


      rect.setY(y); 


    }


    public void setRectangleWidthAndHeight(double w,double h){


      rect.setWidth(w);


      rect.setHeight(h); 


    }


    public void showState(){


      double circleX=circle.getX();


      double rectX=rect.getX();


      if(rectX-circleX==circle.getRadius()*2)  


          System.out.println("图形中的矩形在圆的右侧");


      if(circleX-rectX==rect.getWidth())  


          System.out.println("图形中的矩形在圆的左侧"); 


    }


}


MainClass.java


public class MainClass{


    public static void main(String args[]){


      Rectangle rect1=new Rectangle(),


              rect2=new Rectangle();


      Circle circle1=new Circle(),


           circle2=new Circle();


      Geometry geometryOne,geometryTwo;


      geometryOne=new Geometry(rect1,circle1);


      geometryOne.setRectanglePosition(30,40);


      geometryOne.setRectangleWidthAndHeight(120,80);


      geometryOne.setCirclePosition(150,30);


      geometryOne.setCircleRadius(60);


      geometryTwo=new Geometry(rect2,circle2);


      geometryTwo.setRectanglePosition(160,160);


      geometryTwo.setRectangleWidthAndHeight(120,80);


      geometryTwo.setCirclePosition(40,30);


      geometryTwo.setCircleRadius(60);


      geometryOne.showState();


      geometryTwo.showState();


    }


}






class Employee{


    private double salary=1800;


    public void setSalary(double salary){


      if(salary>1800&&salary<=6000){


          this.salary=salary;


      }


   }


    public double getSalary(){


      return salary;


    }


}


public class Example4_14{


    public static void main(String args[]){


      Employee zhang=new Employee(); 


      Employee wang=new Employee();  


      zhang.setSalary(100); 


      System.out.println("zhang的薪水:"+zhang.getSalary()); 


      wang.setSalary(3888);


      //wang.salary=88888;是非法的,因为对象wang已经不在Employee类中 


      System.out.println("wang的薪水:"+wang.getSalary()); 


    }


}
原创粉丝点击