【发布时间】:2021-08-15 05:07:33
【问题描述】:
我正在解决 OOP (JAVA) 练习任务。在学习作文主题时,我遇到了一个练习任务,这让我非常难受。请检查我的代码并提出解决方案。我将不胜感激:) 练习题是:
对于下面描述的每个属性,选择适当的数据类型。每个类的所有属性都应该是私有的,并通过 get/set 方法公开。还要定义至少一个允许初始化对象的 2-3 个属性的构造函数。 用 courseCode 和 courseTitle 实例变量定义一个类 Course。 使用 countryCode、cityCode 和 lineNumber 属性定义 PhoneNumber 类。 使用 streetAddress、town、city、country 和 phoneNumber 属性定义类 Address。 phoneNumber 应为 PhoneNumber 类型。 定义具有姓名、电子邮件、CNIC、课程1、课程2 和地址属性的学生类。其中 course1 和 course2 应该是 Course 类型,并且 address 应该是 Address 类型。在 Student 类中定义一个构造函数,该构造函数只接受 CNIC、名称和地址。 创建一个 StudentTest 类。在其 main 方法中,创建一个名为 student1 的 Student 对象。完全初始化其所有属性。 CNIC、名称和地址应使用构造函数进行初始化。其他属性应使用 setter 方法进行初始化。所有属性的值应由用户给出。对象完全初始化后,使用 get 方法打印所有属性值。 制作另一个对象student2,假设用户也和student1住在同一个地址。重用student1的地址对象初始化student2地址。您不需要从 student2 对象的用户输入中获取属性。打印所有实例变量的值。
我的代码截图:
我得到的错误:
我的代码副本:
(学生测试班)
public class StudentTest {
public static void main(String[] args) {
PhoneNumber phoneNumber = new PhoneNumber(92, 042, 354);
Address address = new Address("Street No.502", "HT", "Lahore", "Pakistan", phoneNumber);
Student student1 = new Student("Muaz", 34603, address);
student1.setEmail("testEmail@java.com");
System.out.println(student1.getEmail());
student1.getCourse1().setCourseCode(101);
student1.getCourse1().setCourseTitle("OOP");
student1.getCourse2().setCourseCode(102);
student1.getCourse2().setCourseTitle("EMO");
System.out.println(student1.getCourse1().getCourseCode());
System.out.println(student1.getCourse1().getCourseTitle());
System.out.println(student1.getCourse2().getCourseCode());
System.out.println(student1.getCourse2().getCourseTitle());
}
}
(学生班)
public class Student {
//Variables
private String name;
private int cnic;
private Address address;
private String email;
private Course course1;
private Course course2;
//Constructor
public Student(String name, int cnic, Address address){ setName(name); setCnic(cnic); setAddress(address); }
//Set and Get Methods
public void setName(String name){ this.name = name; }
public String getName(){ return name; }
public void setCnic(int cnic){ this.cnic = cnic; }
public int getCnic(){ return cnic; }
public void setAddress(Address address){ this.address = address; }
public Address getAddress(){ return address; }
public void setEmail(String email){ this.email = email; }
public String getEmail(){ return email; }
public void setCourse1(Course course1){ this.course1 = course1; }
public Course getCourse1(){ return course1; }
public void setCourse2(Course course2){ this.course2 = course2; }
public Course getCourse2(){ return course2; }
}
(地址类)
public class Address {
private String streetAddress;
private String town;
private String city;
private String country;
private PhoneNumber phoneNumber;
public Address(String streetAddress, String town, String city, String country, PhoneNumber phoneNumber){
this.streetAddress = streetAddress;
this.town = town;
this.city = city;
this.country = country;
this.phoneNumber = phoneNumber;
}
}
(电话号码类)
public class PhoneNumber {
private int countryCode;
private int cityCode;
private int lineNumber;
public PhoneNumber(int countryCode, int cityCode, int lineNumber){
this.countryCode = countryCode;
this.cityCode = cityCode;
this.lineNumber = lineNumber;
}
}
(课程班)
public class Course {
private int courseCode;
private String courseTitle;
public void setCourseCode(int courseCode){
this.courseCode = courseCode;
}
public int getCourseCode(){
return courseCode;
}
public void setCourseTitle(String courseTitle){
this.courseTitle = courseTitle;
}
public String getCourseTitle(){
return courseTitle;
}
}
错误:
Exception in thread "main" java.lang.NullPointerException
at StudentTest.main(StudentTest.java:14)
Process finished with exit code 1
【问题讨论】:
-
嗨,欢迎来到 StackOverflow(SO)。请注意,将代码和错误粘贴到问题中,而不是在问题中添加屏幕截图。这样,来自 SO 的任何人都可以自己检查。
-
请将您的代码添加到您的问题中,而不是发布屏幕截图
-
您需要将 course1 和 course2 初始化为非空值。
标签: java oop getter-setter composition