云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > java2实用教程第四版实验 清华大学出版社4-3,4-4(北华大学吕磊)

java2实用教程第四版实验 清华大学出版社4-3,4-4(北华大学吕磊)

  • 62 次阅读
  • 3 次下载
  • 2026/4/24 11:22:00

4-3共饮同井水 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; } }

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-4方程的根

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

void getRoots(){ if(boo){

System.out.println(\是一元二次方程\); 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\\n\,root1,root2); }

else{

System.out.printf(\方程没有实根\\n\); } }

else{

System.out.println(\不是一元二次方程\); } }

void setCoefficient(double a,double b,double this.a=a; this.b=b; this.c=c; if(a!=0) boo=true;

c){ else

boo=false; } }

class SunRise{

public static void main(String args[]){ SquareEquation equation=new SquareEquation(4,5,1); equation.getRoots();

equation.setCoefficient(-3,4,5); equation.getRoots(); } }

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

4-3共饮同井水 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 loo

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com