JAVA实验(机动车/家中的电视/共饮同井水/求方程的根)

本文档展示了两个编程实验,一个是机动车类的控制,通过Vehicle类实现速度调整和功率设置;另一个是家庭电视控制,通过TV和Family类实现频道切换和节目播放。这些实例演示了基础的面向对象编程和控制流程。
摘要由CSDN通过智能技术生成

实验 1 机动车

package four;

public class Vehicle {
    private double speed;
    private int power;
    Vehicle(){}
    void speedUp(int s){
        this.speed = this.speed+s;
    }
    void speedDown(int d){
        this.speed = this.speed-d;
    }
    void setPower(int p){
        this.power = p;
    }
    int getPower(){
        return power;
    }
    double getSpeed(){
        return speed;
    }
}
package four;

public class User {
    public static void main(String[] args) {
        Vehicle car1, car2;
        car1=new Vehicle();
        car2=new Vehicle();
        car1.setPower(128);
        car2.setPower(76);
        System.out.println("car1的功率是:"+car1.getPower());
        System.out.println("car2的功率是:"+car2.getPower());
        car1.speedUp(80);
        car2.speedUp(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());
    }
}


实验 2 家中的电视

package four;

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+"频道");
        }
    }
}
package four;

public class Family {
    TV homeTv;
    void buyTv(TV tv){
        tv = new TV();
        homeTv=tv;
    }
    void remoteControl(int m){
        homeTv.setChannel(m);
    }
    void seeTV(){
        homeTv.showProgram();
    }
}
package four;

public class MainClass {
    public static void main(String[] args) {
        TV haierTV = new TV();
        haierTV.setChannel(5);
        System.out.println("haierTV 的频道是"+haierTV.getChannel());
        Family zhangSanFamily = new Family();
        zhangSanFamily.buyTv(haierTV);
        System.out.println("zhangSanFamily开始看电视节目");
        zhangSanFamily.seeTV();
        int m=2;
        System.out.println("zhangSanFamily将电视更换到"+m+"频道");
        zhangSanFamily.remoteControl(m);
        System.out.println("haierTV的频道是"+haierTV.getChannel());
        System.out.println("zhangSanFamily在看电视节目");
        zhangSanFamily.seeTV();
    }
}

实验 3 共饮同井水

package four;

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;
    }
}
package four;

public class Land {
    public static void main(String[] args) {
        Village.setWaterAmount(200);
        int leftWater = Village.waterAmount;
        System.out.println("水井中由"+leftWater+"升水");
        Village zhaoZhuang,maJiaHeZhi;
        zhaoZhuang =new Village("赵庄");
        maJiaHeZhi = new Village("马家河子");
        zhaoZhuang.setPeopleNumber(80);
        maJiaHeZhi.setPeopleNumber(120);
        zhaoZhuang.drinkWater(50);
        leftWater = maJiaHeZhi.lookWaterAmount();
        String name=maJiaHeZhi.name;
        System.out.println(name+"发现水井中有"+leftWater+"升水");
        maJiaHeZhi.drinkWater(100);
        leftWater = 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);
    }
}

 实验 4 求方程的根

package four;

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();
    }
}

package tom.jiafei;

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",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;
    }
}

PS:Idea自动导包,当需要那个包,例如Random xxx,就会自动导入

import java.util.Random;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云上成理

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值