当前位置:   article > 正文

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

java家庭看电视问题

实验 1 机动车

  1. package four;
  2. public class Vehicle {
  3. private double speed;
  4. private int power;
  5. Vehicle(){}
  6. void speedUp(int s){
  7. this.speed = this.speed+s;
  8. }
  9. void speedDown(int d){
  10. this.speed = this.speed-d;
  11. }
  12. void setPower(int p){
  13. this.power = p;
  14. }
  15. int getPower(){
  16. return power;
  17. }
  18. double getSpeed(){
  19. return speed;
  20. }
  21. }
  1. package four;
  2. public class User {
  3. public static void main(String[] args) {
  4. Vehicle car1, car2;
  5. car1=new Vehicle();
  6. car2=new Vehicle();
  7. car1.setPower(128);
  8. car2.setPower(76);
  9. System.out.println("car1的功率是:"+car1.getPower());
  10. System.out.println("car2的功率是:"+car2.getPower());
  11. car1.speedUp(80);
  12. car2.speedUp(80);
  13. System.out.println("car1目前的速度:"+car1.getSpeed());
  14. System.out.println("car2目前的速度:"+car2.getSpeed());
  15. car1.speedDown(10);
  16. car2.speedDown(20);
  17. System.out.println("car1目前的速度:"+car1.getSpeed());
  18. System.out.println("car2目前的速度:"+car2.getSpeed());
  19. }
  20. }


实验 2 家中的电视

  1. package four;
  2. public class TV {
  3. int channel;
  4. void setChannel(int m){
  5. if(m>=1){
  6. channel=m;
  7. }
  8. }
  9. int getChannel(){
  10. return channel;
  11. }
  12. void showProgram(){
  13. switch (channel){
  14. case 1: System.out.println("综合频道");
  15. break;
  16. case 2: System.out.println("经济频道");
  17. break;
  18. case 3: System.out.println("文艺频道");
  19. break;
  20. case 4: System.out.println("国际频道");
  21. break;
  22. case 5: System.out.println("体育频道");
  23. break;
  24. default: System.out.println("不能收看"+channel+"频道");
  25. }
  26. }
  27. }
  1. package four;
  2. public class Family {
  3. TV homeTv;
  4. void buyTv(TV tv){
  5. tv = new TV();
  6. homeTv=tv;
  7. }
  8. void remoteControl(int m){
  9. homeTv.setChannel(m);
  10. }
  11. void seeTV(){
  12. homeTv.showProgram();
  13. }
  14. }
  1. package four;
  2. public class MainClass {
  3. public static void main(String[] args) {
  4. TV haierTV = new TV();
  5. haierTV.setChannel(5);
  6. System.out.println("haierTV 的频道是"+haierTV.getChannel());
  7. Family zhangSanFamily = new Family();
  8. zhangSanFamily.buyTv(haierTV);
  9. System.out.println("zhangSanFamily开始看电视节目");
  10. zhangSanFamily.seeTV();
  11. int m=2;
  12. System.out.println("zhangSanFamily将电视更换到"+m+"频道");
  13. zhangSanFamily.remoteControl(m);
  14. System.out.println("haierTV的频道是"+haierTV.getChannel());
  15. System.out.println("zhangSanFamily在看电视节目");
  16. zhangSanFamily.seeTV();
  17. }
  18. }

实验 3 共饮同井水

  1. package four;
  2. public class Village {
  3. static int waterAmount;
  4. int peopleNumber;
  5. String name;
  6. Village(String s){
  7. name = s;
  8. }
  9. static void setWaterAmount(int m){
  10. if(m>0)
  11. waterAmount = m;
  12. }
  13. void drinkWater(int n){
  14. if(waterAmount-n>=0){
  15. waterAmount = waterAmount-n;
  16. System.out.println(name+"喝了"+n+"升水");
  17. }
  18. else
  19. waterAmount = 0;
  20. }
  21. static int lookWaterAmount(){
  22. return waterAmount;
  23. }
  24. void setPeopleNumber(int n){
  25. peopleNumber = n;
  26. }
  27. int getPeopleNumber(){
  28. return peopleNumber;
  29. }
  30. }
  1. package four;
  2. public class Land {
  3. public static void main(String[] args) {
  4. Village.setWaterAmount(200);
  5. int leftWater = Village.waterAmount;
  6. System.out.println("水井中由"+leftWater+"升水");
  7. Village zhaoZhuang,maJiaHeZhi;
  8. zhaoZhuang =new Village("赵庄");
  9. maJiaHeZhi = new Village("马家河子");
  10. zhaoZhuang.setPeopleNumber(80);
  11. maJiaHeZhi.setPeopleNumber(120);
  12. zhaoZhuang.drinkWater(50);
  13. leftWater = maJiaHeZhi.lookWaterAmount();
  14. String name=maJiaHeZhi.name;
  15. System.out.println(name+"发现水井中有"+leftWater+"升水");
  16. maJiaHeZhi.drinkWater(100);
  17. leftWater = zhaoZhuang.lookWaterAmount();
  18. name=zhaoZhuang.name;
  19. System.out.println(name+"发现水井中有"+leftWater+"升水");
  20. int peopleNumber = zhaoZhuang.getPeopleNumber();
  21. System.out.println("赵庄的人口"+peopleNumber);
  22. peopleNumber = maJiaHeZhi.getPeopleNumber();
  23. System.out.println("马家河子的人口"+peopleNumber);
  24. }
  25. }

 实验 4 求方程的根

  1. package four;
  2. import tom.jiafei.*;
  3. public class SunRise {
  4. public static void main(String[] args) {
  5. SquareEquation equation=new SquareEquation(4,5,1);
  6. equation.getRoots();
  7. equation.setCoefficient(-3,4,5);
  8. equation.getRoots();
  9. }
  10. }

  1. package tom.jiafei;
  2. public class SquareEquation {
  3. double a,b,c;
  4. double root1,root2;
  5. boolean boo;
  6. public SquareEquation(double a, double b,double c){
  7. this.a = a;
  8. this.b = b;
  9. this.c = c;
  10. if(a!=0)
  11. boo=true;
  12. else
  13. boo=false;
  14. }
  15. public void getRoots(){
  16. if(boo){
  17. System.out.println("是一元2次方程");
  18. double disk = b*b-4*a*c;
  19. if(disk>=0){
  20. root1=(-b+Math.sqrt(disk))/(2*a);
  21. root2=(-b-Math.sqrt(disk))/(2*a);
  22. System.out.printf("方程的根:%f,%f",root1,root2);
  23. }
  24. else {
  25. System.out.printf("方程没有实根\n");
  26. }
  27. }
  28. else {
  29. System.out.println("不是一元2次方程");
  30. }
  31. }
  32. public void setCoefficient(double a, double b, double c){
  33. this.a=a;
  34. this.b=b;
  35. this.c=c;
  36. if(a!=0)
  37. boo=true;
  38. else
  39. boo=false;
  40. }
  41. }

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

import java.util.Random;

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/475539
推荐阅读
相关标签
  

闽ICP备14008679号