Java实验 实验四

1.例子10中的Lader.java中的Lader类创建的梯形对象共享一个下底。

public class Lader {

            static double above;

    double height;   //实例变量

            static double bottom;   //定义类变量

            void setabove(double a)

            {   above = a;     }

            void setbottom(double b)

            { bottom = b;      }

            double getabove()

            {  return above;    }

            static double getbottom()  //定义类方法getbottom

            {  System.out.println("上底:"+above); //能否访问实例成员变量above

                return bottom;  

        public static void main(String args[]) {

            System.out.println("下底:"+Lader.getbottom());   //用lader类调用getbottom方法

            Lader.bottom=100;   //给类变量bottom赋值

            Lader lad1=new Lader();

            Lader lad2=new Lader();

            lad1.setabove(28);

            lad2.setbottom(66);

            System.out.println("lad1上底:"+lad1.getabove());

            System.out.println("lad1下底:"+lad1.getbottom());

            System.out.println("lad2上底:"+lad2.getabove());

            System.out.println("lad2下底:"+lad2.getbottom());

        }

    }

运行结果:

【代码3】不能在静态方法中访问实例成员变量above,

  1. 修改上题:定义Lader类变量num,用于计数创建的对象个数

定义静态变量num,并定义构造方法实现num++

public class Lader {

    static double above;

    double height;   //实例变量

    static double bottom;//定义类变量

    static int num;

    Lader() {

        num++;

    }

    void setabove(double a) {

        above = a;

    }

    void setbottom(double b) {

        bottom = b;

    }

    double getabove() {

        return above;

    }

    static double getbottom()  //定义类方法getbottom

    {

        System.out.println("上底:" + above); //【代码3】能否访问类成员变量above

        return bottom;

    }

    public static void main(String args[]) {

        System.out.println("下底:" + Lader.getbottom());   //用lader类调用getbottom方法

        Lader.bottom = 100;   //给类变量bottom赋值

        Lader lad1 = new Lader();

        Lader lad2 = new Lader();

        lad1.setabove(28);

        lad2.setbottom(66);

        System.out.println("lad1上底:" + lad1.getabove());

        System.out.println("lad1下底:" + lad1.getbottom());

        System.out.println("lad2上底:" + lad2.getabove());

        System.out.println("num:" +lad2.num);

    }

}

3.基本类型的类封装

 Double(double num)   Float(float num) Byte(byte num) Short(short num)

 Integer(int num)     Long(long num)

对象.类型Value() 取对象内的值

public class Main {

    public static void main(String[] args) {

        String s1="20.4";

        String s2="80.7";

        System.out.println(s1+s2);

        Double d1=new Double(s1);//Double d1=20.5;

        Double d2=new Double(s2);

        double m1=d1.doubleValue();//Double.parseDouble(s1);

        double m2=d2.doubleValue();

        System.out.println(m1);

        System.out.println(m2);

    }

}

4.CHARACTER 类:判断是数字还是字母,是字母的话小写的转换成大写,大写的转换成小写。

 public class Main {

    public static void main(String args[]) {

            String a="+4cDbF6*";

            String s1="",s2="",s3="",s4="";

            for(int i=0;i<a.length();i++) {

                char ch=a.charAt(i);

                if(Character.isDigit  (ch)) //该字符如果是数字

                    s1=s1+ch;

                else if(Character.isLetter(ch)) //该字符如果是字母

                    if(Character.isUpperCase(ch));  //该字符如果是大写字母                   s2=s2+Character.toLowerCase(ch);

                else  s3=s3+Character.toUpperCase(ch);

               else  s4=s4+ch;

            }

            System.out.println("数字:"+s1);

            System.out.println("to Lower Case:"+s2);

            System.out.println("to Upper Case:"+s3 );

            System.out.println("Others:"+s4);

        }

    }

  1. 例22使用对象数组

class Student{   int number;  }

        public class Example4_22 {

            public static void main(String args[ ]) {

                Student stu[] = new Student[10];  //创建10个元素的对象数组

                for(int i=0;i<stu.length;i++) {

                    stu[i]=new Student();     //创建Student对象

                    stu[i].number = 101+i;

                }

                for(int i=0;i<stu.length;i++)

                {  System.out.println(stu[i].number);  } //输出每个学生的学号

            }

}

新实践四

实验一:

public class Vehicle {

  double speed;//声明double型变量speed,刻画速度

    int power;//声明int型变量power,刻画功率

    void speedUp(int s) {      

speed=speed+s;

 //将参数s的值与成员变量speed的和赋值给成员变量speed

    }

    void speedDown(int d) {   

     speed=speed-d;  //将成员变量speed与参数d的差赋值给成员变量speed

    }

    void setPower(int p) {

       power=p;      //将参数p的值赋值给成员变量power

    }

    int getPower() {

       return power;  //返回成员变量power的值

    }

    double getSpeed() {

       return speed;

    }

}

public class User {

   public static void main(String args[]) {

      Vehicle car1,car2;    

      car1=new Vehicle(); //使用new 运算符和默认的构造方法创建对象car1

      car2=new Vehicle(); //使用new 运算符和默认的构造方法创建对象car2

      car1.setPower(128);   

      car2.setPower(76);

      System.out.println("car1的功率是:"+car1.getPower());

      System.out.println("car2的功率是:"+car2.getPower());

      car1.speedUp(80); //car1调用speedUp方法将自己的speed的值增加80

      car2.speedUp(80);  //car2调用speedUp方法将自己的speed的值增加80

      System.out.println("car1目前的速度:"+car1.getSpeed());

      System.out.println("car2目前的速度:"+car2.getSpeed());

      car1.speedDown(10);

      car2.speedDown(20);

      System.out.println("car1目前的速度:"+car1.getSpeed());

      System.out.println("car2目前的速度:"+car2.getSpeed());

   }

}

运行结果:

实验二:

public class TV {

    int channel;   //电视频道

    void setChannel(int m) {

       if(m>=1){

          channel=m;

       }

    }

    int getChannel(){

       return channel;

    }

    void showProgram(){

       switch(channel) {

          case 1 : System.out.println("综合频道");

                   break;

          case 2 : System.out.println("经济频道");

                   break;

          case 3 : System.out.println("文艺频道");

                   break;

          case 4 : System.out.println("国际频道");

                   break;

          case 5 : System.out.println("体育频道");

                   break;

          default : System.out.println("不能收看"+channel+"频道");

       }

    }

}

public class Family {

    TV homeTV;

    void buyTV(TV tv) {

       homeTV=tv;            //将参数tv赋值给homeTV

    }

    void remoteControl(int m) {

       homeTV.setChannel(m);  

    }

    void seeTV() {

       homeTV.showProgram();  //homeTV调用showProgram()方法

    }

}

public class MainClass {  

   public static void main(String args[]) {

       TV haierTV = new TV();

       haierTV.setChannel(5); //haierTV调用setChannel(int m),并向参数m传递5

       System.out.println("haierTV的频道是"+haierTV.getChannel());

       Family zhangSanFamily = new Family();

       zhangSanFamily.buyTV(haierTV);

//zhangSanFamily调用void buyTV(TV tv)方法,并将haierTV传递给参数TV

       System.out.println("zhangSanFamily开始看电视节目");

       zhangSanFamily.seeTV();

       int m=2;

       System.out.println("hangSanFamily将电视更换到"+m+"频道");

       zhangSanFamily.remoteControl(m);

       System.out.println("haierTV的频道是"+haierTV.getChannel());

       System.out.println("hangSanFamily再看电视节目");

       zhangSanFamily.seeTV();       

    }

}

运行结果:

实验三:

public class Village {

    static int waterAmount;   //模拟水井的水量

    int peopleNumber;        //村庄的人数

    String name;            //村庄的名字

    Village(String s) {

        name = s;

    }

    static void setWaterAmount(int m) {

       if(m>0)

         waterAmount = m;

    }

    void drinkWater(int n){

       if( waterAmount-n>=0) {

         waterAmount = waterAmount-n;

         System.out.println(name+"喝了"+n+"升水");

       }

       else

         waterAmount = 0;

    }

    static int lookWaterAmount() {

       return waterAmount;

    }

    void setPeopleNumber(int n) {

       peopleNumber = n;

    }

    int getPeopleNumber() {

       return peopleNumber;

    }

}

public class Land {  

   public static void main(String args[]) {

      Village.setWaterAmount(200); //用类名调用setWaterAmount(int m),并向参数传值200

       int leftWater =Village.waterAmount; //用Village类的类名访问waterAmount

       System.out.println("水井中有 "+leftWater+" 升水");

       Village zhaoZhuang,maJiaHeZhi;

       zhaoZhuang = new Village("赵庄");

       maJiaHeZhi = new Village("马家河子");

       zhaoZhuang.setPeopleNumber(80);

       maJiaHeZhi.setPeopleNumber(120);

       zhaoZhuang.drinkWater(50);//zhaoZhuang调用drinkWater(int n),并向参数传值50

       leftWater = maJiaHeZhi.lookWaterAmount();//maJiaHeZhi调用lookWaterAmount()方法

       String name=maJiaHeZhi.name;

       System.out.println(name+"发现水井中有 "+leftWater+" 升水");

       maJiaHeZhi.drinkWater(100);

       leftWater = zhaoZhuang.lookWaterAmount();//zhaoZhuang调用lookWaterAmount()方法

       name=zhaoZhuang.name;

       System.out.println(name+"发现水井中有 "+leftWater+" 升水");

       int peopleNumber = zhaoZhuang.getPeopleNumber();

       System.out.println("赵庄的人口:"+peopleNumber);

       peopleNumber = maJiaHeZhi.getPeopleNumber();

       System.out.println("马家河子的人口:"+peopleNumber);

    }

}

运行结果:

实验四:

public class SquareEquation {

    double a,b,c;

    double root1,root2;

    boolean boo;

    public SquareEquation(double a,double b,double c) {

       this.a=a;

       this.b=b;

       this.c=c;

       if(a!=0)

          boo=true;

       else

         boo=false;

    }

    public void getRoots() {

       if(boo) {

          System.out.println("是一元2次方程");

          double disk=b*b-4*a*c;

          if(disk>=0) {

             root1=(-b+Math.sqrt(disk))/(2*a);

             root2=(-b-Math.sqrt(disk))/(2*a);

             System.out.printf("方程的根:%f,%f\n",root1,root2);

          }

          else {

             System.out.printf("方程没有实根\n");

          }

       }

       else {

         System.out.println("不是一元2次方程");

       }

    }

    public void setCoefficient(double a,double b,double c) {

       this.a=a;

       this.b=b;

       this.c=c;

       if(a!=0)

          boo=true;

       else

          boo=false;

   }    

}

package tom.jiafei;

import tom.jiafei.*;

public class SunRise {

   public static void main(String args[]) {

      SquareEquation equation=new SquareEquation(4,5,1);

      equation.getRoots();

      equation.setCoefficient(-3,4,5);

      equation.getRoots();

   }

}

运行结果:

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值