1.编写 Java 程序,请按照以下要求,在Example06类中进行测试。。
(1) 定义一个Student类,该类有一个age属性,在类中定义一个有参数的构造方法,该参数用于为age属性赋值。
(2) 编写一个测试类Example06,在main()方法中通过有参构造方法创建一个对象。
(3) 打印该对象age属性的值。
1.代码
// 定义Student类
class Student {
int age;
// 定义带参数的构造方法
public Student(int age) {
this.age = age;
}
}
// 定义测试类Example06
public class Example06 {
public static void main(String[] args) {
// 通过有参构造方法创建一个Student对象
Student student = new Student(20);
// 打印该对象age属性的值
System.out.println("学生年龄:" + student.age);
}
}
2.运行效果:
学生年龄:20
2.编写 Java 程序,请按照以下要求,在Example07类中进行测试。
(1) 对Student1类进行修改,在类中定义三个重载的构造方法,包括无参的构造方法,接收一个String类型参数的构造方法,接收String类型和int类型两个参数的构造方法。
(2) 编写测试类Example07,在main()方法中,分别使用三个重载的构造方法创建三个Student1对象。
1.代码
public class Example07 {
public static void main(String[] args) {
// 使用无参构造方法创建对象
Student1 student1 = new Student1();
System.out.println("学生1:" + student1.getName() + ",年龄:" + student1.getAge());
// 使用接收一个String类型参数的构造方法创建对象
Student1 student2 = new Student1("张三");
System.out.println("学生2:" + student2.getName() + ",年龄:" + student2.getAge());
// 使用接收String类型和int类型两个参数的构造方法创建对象
Student1 student3 = new Student1("李四", 20);
System.out.println("学生3:" + student3.getName() + ",年龄:" + student3.getAge());
}
}
class Student1 {
private String name;
private int age;
// 无参构造方法
public Student1() {
}
// 接收一个String类型参数的构造方法
public Student1(String name) {
this.name = name;
}
// 接收String类型和int类型两个参数的构造方法
public Student1(String name, int age) {
this.name = name;
this.age = age;
}
// 获取姓名的方法
public String getName() {
return name;
}
// 获取年龄的方法
public int getAge() {
return age;
}
}
2.运行效果:
学生1:null,年龄:0
学生2:张三,年龄:0
学生3:李四,年龄:20
3.编写 Java 程序,请按照以下要求,在Example08类中进行测试。
(1) 在Student2类中创建多个重载的构造方法,包括无参的构造方法和一个参数的构造方法,以及两个参数的构造方法。
(2) 在一个参数的构造方法中使用this关键字调用无参构造方法,在两个参数的构造方法中调用一个参数的构造方法。
(3) 编写测试类Example08,在main()方法中,调用两个参数的构造方法创建对象,演示构造方法的执行顺序。
1.代码
public class Example08 {
public static void main(String[] args) {
// 使用无参构造方法创建对象
Student2 student1 = new Student2();
System.out.println("学生1:" + student1.getName() + ",年龄:" + student1.getAge());
// 使用接收一个String类型参数的构造方法创建对象
Student2 student2 = new Student2("张三");
System.out.println("学生2:" + student2.getName() + ",年龄:" + student2.getAge());
// 使用接收String类型和int类型两个参数的构造方法创建对象
Student2 student3 = new Student2("李四", 20);
System.out.println("学生3:" + student3.getName() + ",年龄:" + student3.getAge());
}
}
class Student2 {
private String name;
private int age;
// 无参构造方法
public Student2() {
this("", 0);
}
// 接收一个String类型参数的构造方法
public Student2(String name) {
this(name, 0);
}
// 接收String类型和int类型两个参数的构造方法
public Student2(String name, int age) {
this.name = name;
this.age = age;
}
// 获取姓名的方法
public String getName() {
return name;
}
// 获取年龄的方法
public int getAge() {
return age;
}
}
2.运行效果:
学生1:,年龄:0
学生2:张三,年龄:0
学生3:李四,年龄:20
4.编写 Java 程序,定义一个Father和Child类,并进行测试。要求如下:
(1) Father类为外部类,类中定义一个私有的String类型的属性name,name的值为“zhangjun”。
(2) Child类为Father类的内部类,其中定义一个introFather()方法,方法中调用Father类的name属性。
(3) 定义一个测试类Test,在Test类的main()方法中,创建Child对象,并调用introFather ()方法。
1.代码
public class Father {
private String name = "zhangjun";
public class Child {
public void introFather() {
System.out.println("Father's name is: " + name);
}
}
}
class Test {
public static void main(String[] args) {
Father.Child child = new Father().new Child();
child.introFather();
}
}
2.运行效果:
Father's name is: zhangjun
5. 编写 Java 程序,定义一个圆类——Circle,在类的内部提供2个属性:半径(r) 和圆心Point point,同时提供两个方法: 计算面积(getArea() ) 和计算周长(getPerimeter()) 。 通过两个方法计算圆的周长和面积并且对计算结果进行输出。最后定义一个测试类(Test2)对 Circle 类进行使用。
1.代码
// 定义圆类
public class Circle {
// 定义半径和圆心点属性
private double r;
private Point point;
// 构造方法
public Circle(double r, Point point) {
this.r = r;
this.point = point;
}
// 计算面积的方法
public double getArea() {
return Math.PI * r * r;
}
// 计算周长的方法
public double getPerimeter() {
return 2 * Math.PI * r;
}
}
// 定义点类
class Point {
private double x;
private double y;
// 构造方法
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
// 定义测试类
class Test2 {
public static void main(String[] args) {
// 创建圆心点对象
Point center = new Point(0, 0);
// 创建半径为5的圆对象
Circle circle = new Circle(5, center);
// 输出圆的面积和周长
System.out.println("圆的面积:" + circle.getArea());
System.out.println("圆的周长:" + circle.getPerimeter());
}
}
2.运行效果:
圆的面积:78.53981633974483
圆的周长:31.41592653589793
6. 编写 Java 程序,定义一个网络用户类,要处理的信息有用户ID、用户密码、 email 地址。
在建立类的实例时把以上三个信息都作为构造函数的参数输入,其中用户 ID 和用户密码是必须的,缺省时 email地址是用户ID加上字符串"@bjsxt.com"。
1.代码
public class NetworkUser {
private String userId;
private String password;
private String email;
public NetworkUser(String userId, String password) {
if (userId == null || userId.isEmpty() || password == null || password.isEmpty()) {
throw new IllegalArgumentException("User ID and password must be provided.");
}
this.userId = userId;
this.password = password;
this.email = userId + "@bjsxt.com";
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
class TestNetworkUser {
public static void main(String[] args) {
NetworkUser user = new NetworkUser("user123", "password123");
System.out.println("User ID: " + user.getUserId());
System.out.println("Password: " + user.getPassword());
System.out.println("Email: " + user.getEmail());
}
}
2.运行效果:
User ID: user123
Password: password123
Email: user123@bjsxt.com
三、应用题1,编写程序,定义Phone类,包含:品牌、颜色、尺寸属性;实例化Phone类,并调用这些属性,运行效果如下
1.代码
public class Phone {
private String brand;
private String color;
private String size;
public Phone(String brand, String color, String size) {
this.brand = brand;
this.color = color;
this.size = size;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
@Override
public String toString() {
return "Phone{" +
"brand='" + brand + '\'' +
", color='" + color + '\'' +
", size='" + size + '\'' +
'}';
}
}
class TestPhone {
public static void main(String[] args) {
Phone phone = new Phone("Apple", "Red", "6.1英寸");
System.out.println(phone);
}
}
2.运行效果:
Phone{brand='Apple', color='Red', size='6.1英寸'}
应用题2,编写程序,某班级投票竞选班干部,班级学生人数为90人,每个学生只能投一票。要求,编程实现一个投票程序,每个学生只能投一次票,投票成功提示“感谢你的投票”,若重复投票,提示“请勿重复投票”。当投票总数达到90时或者主观结束投票时,同时统计投票学生人数和投票结果。
1.代码
package learn.edu;
import java.util.HashSet;
import java.util.Set;
public class Voter {
// 属性的定义
private static final int MAX_COUNT = 90; // 最大投票数
private static int count; // 投票数
// 静态变量,存放已经投票的学生
private static Set<Voter> voters = new HashSet<Voter>();
private String name;
private String answer;
// 构造方法
public Voter(String name) {
this.name = name;
}
// 投票
public void voterFor(String answer) {
if (count == MAX_COUNT){
System.out.println("投票结束。");
return ;
}
if (voters.contains(this)){
System.out.println(name+",请勿重复投票。");
}
else {
this.answer = answer;
count ++;
voters.add(this);
System.out.println(name+" 感谢你的投票。");
}
}
// 打印投票结果
public static void printVoterResult() {
System.out.println("当前投票数为:"+count);
System.out.println("参与投票的学生和结果如下:");
for (Voter voter: voters) {
System.out.println(voter.name+" 意见: "+voter.answer);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
oter tom = new Voter("Tom");
Voter jack = new Voter("Jack");
Voter mike = new Voter("Mike");
// 学生开始投票
tom.voterFor("是");
tom.voterFor("否");
jack.voterFor("是");
mike.voterFor("是");
// 打印投票结果
Voter.printVoterResult();
}
}
2.运行效果:
Tom感谢你的投票。
Tom,请勿重复投票。
Jack感谢你的投票。
Mike感谢你的投票。
当前投票数为:3
参与投票的学生和结果如下:
Mike意见: 是
Jack意见: 是