机动车
public class User {public static void main(String[] args) {Vehicle v = new Vehicle();System.out.println("设置功率大小:");v.setPower(36);System.out.println("功率大小为:" + v.getPower() + "kw");v.speedUp(80);v.speedDown();}
}
public class Vehicle {double speed;int power;public void speedUp(int s) {speed = s;System.out.println("加速至" + s + "km/h");}public void speedDown() {System.out.println("减速");}public double getSpeed() {return speed;}public int getPower() {return power;}public void setPower(int p) {this.power = p;}
电视
public class TV {int channel;void setChannel(int m) {if (m > 0) {channel = m;}}int getChannel() {return channel;}void show() {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;}void remoteControl(int m) {homeTV.setChannel(m);}void seeTV() {homeTV.show();}}
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();}
}
饮水。。。。
求根
package tom.jiafei;public class SquareEquation {double a,b,c;double root1,root2;boolean flag;public SquareEquation(double a, double b,double c){this.a = a;this.b = b;this.c = c;if(a!=0)flag=true;elseflag=false;}public void getRoots(){if(flag){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.println("方程的根:"+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)flag=true;elseflag=false;}
}
package testqiugen.com;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();}
}