赞
踩
//定义Vehicle类 class Vehicle { double speed; // 机车速度 int power;// 机车功率 // 机动车有加速度功能 public void speedUp(int a) { speed += a; System.out.println("机车加速了!"); System.out.println("机车目前速度是" + speed + "km/h"); } // 机动车有减速功能 public void speedDown(int b) { speed -= b; System.out.println("机车减速了!"); System.out.println("机车目前速度是" + speed + "km/h"); } // 获取机动车功率 public int getPower() { return power; } // 设置机动车功率 public void setPower(int c) { power = c; } double getspeed() { return speed; } void setspeed(double s) { speed = s; } } public class User1 { public static void main(String[] args) { // TODO Auto-generated method stub Vehicle V = new Vehicle(); V.setPower(60); System.out.println("机车目前的功率是:" + V.getPower()); V.setspeed(100); System.out.println("机车目前的速度是:" + V.getspeed() + "km/h"); V.speedDown(10); V.speedUp(20); } }
class TV { String name1 = "长虹"; String name2 = "小米"; void sonsay() { System.out.println("son说这个牌子太老了!"); } void mumsay() { System.out.println("mum说都别争了,就买小米!"); } } class Family { String buytv; TV sontv = new TV(); TV mumtv = new TV(); void say(String buy1, String n1) { System.out.println(n1 + "说买" + buy1 + "牌的电视吧."); if (buy1 == "长虹") { sontv.sonsay(); } if (buy1 == "小米") { mumtv.mumsay(); } } } public class MainClass { public static void main(String[] args) { TV tv = new TV(); Family p = new Family(); p.buytv = tv.name1; p.say(p.buytv, "dad"); p.buytv = tv.name2; p.say(p.buytv, "son"); } }
```java class Village { static int waterAmount = 100; static int dringwater; String name; int people; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPeople() { return people; } public void setPeople(int people) { this.people = people; } void dringking(int w) { dringwater = w; System.out.println("王庄的人喝了" + w + "升水."); } void seewater() { System.out.println("赵庄的人发现还剩下" + (waterAmount -= dringwater) + "升水."); } } public class Land { public static void main(String[] args) { // TODO Auto-generated method stub Village v1 = new Village(); Village v2 = new Village(); v1.setName("赵庄"); v2.setName("王庄"); v1.setPeople(100); v2.setPeople(50); System.out.println( v1.getName() + "共有" + v1.getPeople() + "人," + "" + v2.getName() + "共有" + v2.getPeople() + "人."); System.out.println("现在井中共有" + Village.waterAmount + "升的水."); v2.dringking(50); v1.seewater(); } }
SquareEquation类: package tom.jiefei; import java.lang.Math; //math头文件 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("它是一元二次方程!"); double disk=b*b-4*a*c; if(disk>=0) { root1=(-b+Math.sqrt(disk))/(2*a); root1=(-b-Math.sqrt(disk))/(2*a); System.out.println("方程的实根是"+root1+"和"+root2); } else { System.out.println("方程没有实根!"); } } else { System.out.println("它不是一元二次方程!"); } } }
SunRise的主类:
package HomeWork;
import tom.jiefei.*;
public class SunRise {
public static void main(String[] args) {
// TODO Auto-generated method stub
SquareEquation A =new SquareEquation(1,2,1);
A.getRoots();
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。