实验 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;