(1)编写一个类Book,代表教材,并编写测试类完成信息输出。
1)具有属性:名称(title)、页数(pageNum)、种类(type)。
2)具有两个带参的构造方法:第一个构造方法中,设置教材种类为“计算机”(固定),其余属性的值由参数给定;第二个构造方法中,所有属性的值都由参数给定。
3)具有方法:detail,用来在控制台输出每本教材的名称、页数和种类。其中页数不能少于200页,否则输出错误信息。
4)编写测试类,输入教材的名称、页数和种类,并打印输出教材的具体信息。
(2)自定义一个求最大值的类。
求三个数中最大值的方法,并实现方法重载,要求能够分别比较三个整数中的最大值,三个小数中的最大值,以及任意三个数中的最大值。
思路:根据题目一句一句编写,这样很容易就学会了,按照题目编号编写;
首先定义一个Book类,其次编写一个BookTest类:
/**
* @author user
* @describle (1)编写一个类Book,代表教材,并编写测试类完成信息输出。
* <p>
* 1)具有属性:名称(title)、页数(pageNum)、种类(type)。
* <p>
* 2)具有两个带参的构造方法:第一个构造方法中,设置教材种类为“计算机”(固定),其余属性的值由参数给定;第二个构造方法中,所有属性的值都由参数给定。
* <p>
* 3)具有方法:detail,用来在控制台输出每本教材的名称、页数和种类。其中页数不能少于200页,否则输出错误信息。
* <p>
* 4)编写测试类,输入教材的名称、页数和种类,并打印输出教材的具体信息。
* @since 2021/10/29
*/
public class Book {
/**
* 1)具有属性:名称(title)、页数(pageNum)、种类(type)。
*/
private String title;
private Integer pageNum;
private String type;
/**
* 2)第一个构造方法中,设置教材种类为“计算机”(固定),其余属性的值由参数给定;
*
* @param title 名称
* @param pageNum 页数
*/
public Book(String title, Integer pageNum) {
// 设置教材种类为“计算机”(固定)
this.type = "计算机";
this.title = title;
this.pageNum = pageNum;
}
/**
* 2) 第二个构造方法中,所有属性的值都由参数给定。
*
* @param title 名称
* @param pageNum 页数
* @param type 种类
*/
public Book(String title, Integer pageNum, String type) {
this.title = title;
this.pageNum = pageNum;
this.type = type;
}
/**
* 3)具有方法:detail,用来在控制台输出每本教材的名称、页数和种类。其中页数不能少于200页,否则输出错误信息。
*
*/
public void detail() throws Exception {
if (pageNum < 200) {
throw new Exception("教材页数不能少于200页");
} else {
System.out.println(toString());
}
}
@Override
public String toString() {
return "教材信息:{" +
"名称: '" + title + '\'' +
", 页数: " + pageNum +
", 种类: '" + type + '\'' +
'}';
}
}
BookTest类:
/**
* 4)编写测试类,输入教材的名称、页数和种类,并打印输出教材的具体信息。
*
* @author user
* @since 2021/10/29
*/
public class BookTest {
public static void main(String[] args) throws Exception {
Book book = new Book("c语言程序设计",500);
book.detail();
Book book2 = new Book("会计学",400, "金融类");
book2.detail();
Book book3 = new Book("建筑设计",150, "建筑类");
book3.detail();
}
}
运行结果:
家庭作业需要自己完成。
(2)自定义一个求最大值的类。
求三个数中最大值的方法,并实现方法重载,要求能够分别比较三个整数中的最大值,三个小数中的最大值,以及任意三个数中的最大值。
建议:先去了解方法的重载:定义理解好了,写代码就不成问题了。
方法重载:
方法重载是指在一个类中,多个方法的方法名相同,但是参数列表不同。参数列表不同指的是参数个数、参数类型或者参数的顺序不同。
方法重载规则:
1)被重载的方法必须改变参数列表;
2)被重载的方法可以改变返回类型;
3)被重载的方法可以改变访问修饰符;
4)被重载的方法可以声明新的或更广的检查异常;
5)方法能够在同一个类中或者在一个子类中被重载。
/**
* 自定义一个求最大值的类
*
* @author user
* @since 2021/11/02
*/
public class MaxValue {
/**
* 求三个数中最大值的方法,并实现方法重载,要求能够分别比较三个整数中的最大值,三个小数中的最大值,以及任意三个数中的最大值。
*/
public int compareMax(int x, int y, int z) {
int max = Math.max(x, y);
// suppose x is max then compare x with z to find max number
if (max > y) {
max = Math.max(x, z);
} else { //if y is max then compare y with z to find max number
max = Math.max(y, z);
}
return max;
}
public float compareMax(float x, float y, float z) {
float max = Math.max(x, y);
// suppose x is max then compare x with z to find max number
if (max > y) {
max = Math.max(x, z);
} else { //if y is max then compare y with z to find max number
max = Math.max(y, z);
}
return max;
}
public double compareMax(double x, double y, double z) {
double max = Math.max(x, y);
// suppose x is max then compare x with z to find max number
if (max > y) {
max = Math.max(x, z);
} else { //if y is max then compare y with z to find max number
max = Math.max(y, z);
}
return max;
}
public static void main(String[] args) {
MaxValue maxValue = new MaxValue();
int max = maxValue.compareMax(5, 3, 6);
System.out.println("整数比较:max="+max);
float max2 = maxValue.compareMax(2.3f, 3.7f, 1.0f);
System.out.println("小数数比较:max="+max2);
double max3 = maxValue.compareMax(8, 3.5f, 7.2);
System.out.println("任意数比较:max="+max3);
}
}