- 编写“人”类及“测试类”。
源代码:
“人”类:
package aaa;
public class person {
private String name;
private int age;
public person(String name,int age){//构造方法:初始化姓名和年龄
this.name=name;
this.age=age;
}
public person(String name){//构造方法:初始化姓名
this.name=name;
}
public void setName(String name){
this.name=name;//设置姓名
}
public String getName(){
return name;//获取姓名
}
public void setAge(int age){
if(age<0)
this.age=0;
else
this.age=age;
}
public int getAge(){
return age;
}
public void printfMessage(){
System.out.println("name="+name+",age="+age);
}
public void addAge(){
age++;
}
}
“测试类”:
package aaa;
public class testperson {
public static void main(String[] args) {
person a[]=new person[2];
a[0]=new person("小明",16);
a[0].addAge();
a[0].printfMessage();
a[1]=new person("小红");
a[1].setAge(20);
a[1].addAge();
a[1].printfMessage();
int j=0;
for(int i=0;i<2;i++) {
if(a[i].getAge()>18)
j++;
}
System.out.println("成年的人数:"+j);
}
}
运行结果:
2.编写“电费管理类”及其测试类。
第一步 编写“电费管理”类
1)私有属性:上月电表读数、本月电表读数
2)构造方法:无参、2个参数
3)成员方法:getXXX()方法、setXXX()方法
4)成员方法:显示上月、本月电表读数
第二步 编写测试类
1)创建对象一:上月电表读数为1000,本月电表读数为1200。
要求:调用无参构造方法创建对象;
调用setXXX()方法初始化对象;
假设每度电的价格为1.2元,计算并显示本月电费。
2)创建对象二:上月电表读数1200,本月电表读数为1450。
要求:调用2个参数的构造方法创建并初始化对象;
调用setXXX()方法修改本月电表读数为1500(模拟读错了需修改);
假设每度电的价格为1.2元,计算并显示本月电费。
源代码:
“电费管理”类:
package aaa;
public class electricity {
private int premonth;
private int curmonth;
public electricity(){//无参构造方法
}
public electricity(int premonth,int curmonth){//两个参数
this.premonth=premonth;
this.curmonth=curmonth;
}
public void setpremonth(int premonth){
this.premonth=premonth;
}
public int getpremonth(){
return premonth;
}
public void setcurmonth(int curmonth){
this.curmonth=curmonth;
}
public int getcurmonth(){
return curmonth;
}
public void myprint() {
System.out.println("上月电表读数:"+premonth+"本月电表读数:"+curmonth);
}
}
测试类:
package aaa;
public class testelectricity {
public static void main(String[] args) {
electricity s1=new electricity();
s1.setpremonth(1000);
s1.setcurmonth(1200);
s1.myprint();
System.out.println("本月电费:"+(s1.getcurmonth()-s1.getpremonth())*1.2);
electricity s2=new electricity(1200,1450);
s2.setcurmonth(1500);
s2.myprint();
System.out.println("本月电费:"+(s2.getcurmonth()-s2.getpremonth())*1.2);
}
}
运行结果:
3、编写“圆柱体”类及其测试类。
3.1 “圆柱体”类
私有属性:圆底半径、高,
构造方法:带两个参数
方法1:计算底面积
方法2:计算体积
方法3:打印圆底半径、高、底面积和体积。
3.2 测试类
创建2个对象,并调用方法
源代码:
“圆柱体”类:
package aaa;
public class cylinder {
final float PI=3.14f;
private double r,h;//私有属性
public cylinder(double r,double h) {//带两个参数
this.r=r;
this.h=h;
}
public void s() {
System.out.println("底面积:"+(PI*r*r));
}
public void v() {
System.out.println("体积:"+((PI*r*r)*h));
}
public void myprint() {
System.out.println("圆底半径:"+r+"高:"+h);
System.out.println("底面积:"+(PI*r*r));
System.out.println("体积:"+((PI*r*r)*h));
}
}
测试类:
package aaa;
public class testcylinder {
public static void main(String[] args) {
cylinder s1=new cylinder(1.0,2.0);
s1.s();
s1.v();
s1.myprint();
System.out.println();
cylinder s2=new cylinder(2.0,1.0);
s2.s();
s2.v();
s2.myprint();
}
}
运行结果:
4、编写“四则运算类”及其测试类。
4.1 应用场景
计算器。能实现简单的四则运算,要求:只进行一次运算。
4.1 “四则运算”类
私有属性:操作数一、操作数二、操作符
构造方法:带两个参数
构造方法:带三个参数
方法一:对两个操作数做加运算
方法二:对两个操作数做减运算
方法三:对两个操作数做乘运算
方法四:对两个操作数做除运算
4.2 测试类
从键盘输入两个操作数和一个操作符,计算之后,输出运算结果。
源代码:
“四则运算”类:
package aaa;
public class number {
private int a1,a2;
private char b;
public number(int a1,int a2) {
this.a1=a1;
this.a2=a2;
}
public number(int a1,int a2,char b) {
this.a1=a1;
this.a2=a2;
this.b=b;
}
public void add() {
System.out.println(a1+a2);
}
public void reduce() {
System.out.println(a1-a2);
}
public void multiplication() {
System.out.println(a1*a2);
}
public void division() {
System.out.println(a1/a2);
}
}
测试类:
package aaa;
import java.util.*;
public class testnumber {
public static void main(String[] args) {
int a1 = 0,a2 = 0;
char b = 0;
Scanner rd=new Scanner(System.in);
System.out.println("请输入两个操作数和一个操作符:");
a1=rd.nextInt();
a2=rd.nextInt();
b=rd.next().charAt(0);
number s=new number(a1,a2,b);
if(b=='+')
s.add();
else if(b=='-')
s.reduce();
else if(b=='*')
s.multiplication();
else if(b=='/')
s.division();
}
}
运行结果:



来源:https://www.cnblogs.com/leiyaxi/p/10760975.html