Java程序设计精编教程(5.14上机实践)
2022/5/12 20:27:34
本文主要是介绍Java程序设计精编教程(5.14上机实践),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
实验目的
本实验的目的是让学生掌握类变量与实例变量,以及类方法与实例方法的区别
实验要求
编写程序模拟两个村庄共同拥有一片森林。编写一个Village类,该类有一个静态的int型成员变量treeAmount用于模拟森林中树木的数量。在主类MainClass的main方法中创建两个村庄,一个村庄改变了treeAmount的值,另一个村庄查看treeAmount的值。
程序代码
Village.java
public class Village { static int treeAmount; //模拟树林中树木的数量 int peopleNumber; //村庄的人数 String name; //村庄的名字 Village(String s){ name = s; } void treePlanting(int n) { treeAmount = treeAmount + n; System.out.println(name+"植树"+n+"棵"); } void fellTree(int n) { if (treeAmount-n >= 0) { treeAmount = treeAmount - n; System.out.println(name+"伐树"+n+"棵"); } else { System.out.println("无树木可伐"); } } static int lookTreeAmount() { return treeAmount; } void addPeopleNumber(int n) { peopleNumber = peopleNumber + n; System.out.println(name+"增加了"+n+"人"); } }
MainClass.java
public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub Village zhaoZhuang,majiaHeZi; zhaoZhuang = new Village("赵庄"); majiaHeZi = new Village("马家河子"); zhaoZhuang.peopleNumber = 100; majiaHeZi.peopleNumber = 150; //用类名Village访问treeAmount,并赋值200 Village.treeAmount = 200; int leftTree = Village.treeAmount; System.out.println("森林中有"+leftTree+"棵树"); //zhaoZhuang调用treePlanting(int n),并传值50 zhaoZhuang.treePlanting(50); //majiaHezi调用lookTreeAmount()方法得到树木的数量 leftTree = Village.lookTreeAmount(); System.out.println("森林中有"+leftTree+"棵树"); //majiaHeZi调用fellTree(int n)并向参数传值70 majiaHeZi.fellTree(70); leftTree = Village.lookTreeAmount(); System.out.println("森林中有"+leftTree+"棵树"); System.out.println("赵庄的人口:"+zhaoZhuang.peopleNumber); zhaoZhuang.addPeopleNumber(12); System.out.println("赵庄的人口:"+zhaoZhuang.peopleNumber); System.out.println("马家河子的人口:"+majiaHeZi.peopleNumber); majiaHeZi.addPeopleNumber(10); System.out.println("马家河子的人口:"+majiaHeZi.peopleNumber); } }
也可以合并到一起
public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub Village zhaoZhuang,majiaHeZi; zhaoZhuang = new Village("赵庄"); majiaHeZi = new Village("马家河子"); zhaoZhuang.peopleNumber = 100; majiaHeZi.peopleNumber = 150; //用类名Village访问treeAmount,并赋值200 Village.treeAmount = 200; int leftTree = Village.treeAmount; System.out.println("森林中有"+leftTree+"棵树"); //zhaoZhuang调用treePlanting(int n),并传值50 zhaoZhuang.treePlanting(50); //majiaHezi调用lookTreeAmount()方法得到树木的数量 leftTree = Village.lookTreeAmount(); System.out.println("森林中有"+leftTree+"棵树"); //majiaHeZi调用fellTree(int n)并向参数传值70 majiaHeZi.fellTree(70); leftTree = Village.lookTreeAmount(); System.out.println("森林中有"+leftTree+"棵树"); System.out.println("赵庄的人口:"+zhaoZhuang.peopleNumber); zhaoZhuang.addPeopleNumber(12); System.out.println("赵庄的人口:"+zhaoZhuang.peopleNumber); System.out.println("马家河子的人口:"+majiaHeZi.peopleNumber); majiaHeZi.addPeopleNumber(10); System.out.println("马家河子的人口:"+majiaHeZi.peopleNumber); } } class Village { static int treeAmount; //模拟树林中树木的数量 int peopleNumber; //村庄的人数 String name; //村庄的名字 Village(String s){ name = s; } void treePlanting(int n) { treeAmount = treeAmount + n; System.out.println(name+"植树"+n+"棵"); } void fellTree(int n) { if (treeAmount-n >= 0) { treeAmount = treeAmount - n; System.out.println(name+"伐树"+n+"棵"); } else { System.out.println("无树木可伐"); } } static int lookTreeAmount() { return treeAmount; } void addPeopleNumber(int n) { peopleNumber = peopleNumber + n; System.out.println(name+"增加了"+n+"人"); } }
运行结果
这篇关于Java程序设计精编教程(5.14上机实践)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-11cursor试用出现:Too many free trial accounts used on this machine 的解决方法
- 2025-01-11百万架构师第十四课:源码分析:Spring 源码分析:深入分析IOC那些鲜为人知的细节|JavaGuide
- 2025-01-11不得不了解的高效AI办公工具API
- 2025-01-102025 蛇年,J 人直播带货内容审核团队必备的办公软件有哪 6 款?
- 2025-01-10高效运营背后的支柱:文档管理优化指南
- 2025-01-10年末压力山大?试试优化你的文档管理
- 2025-01-10跨部门协作中的进度追踪重要性解析
- 2025-01-10总结 JavaScript 中的变体函数调用方式
- 2025-01-10HR团队如何通过数据驱动提升管理效率?6个策略
- 2025-01-10WBS实战指南:如何一步步构建高效项目管理框架?