-2

I have created a constructor that has many parameters. I want it to take inputs through user. However, when I run the program it does not take all inputs and jumps the last STRING input. How to Solve this issue?

The practice problem that I am solving is: For each attributes described below, choose appropriate data type. All the attributes of each class should be private and exposed via get/set methods. Also define at least one constructor that allows to initialize 2-3 attributes of object. Define a class Course with courseCode and courseTitle instance variable. Define PhoneNumber class with countryCode, cityCode and lineNumber attributes. Define a class Address with streetAddress, town, city, country and phoneNumber attributes. The phoneNumber shall be of type PhoneNumber. Define a class Student with name, email, CNIC, course1, course2 and address attributes. Where course1 and course2 should be of type Course and address shall be of type Address. Define a constructor in Student class that shall take CNIC, name and address only. Create a StudentTest class. In its main method, create a Student object named student1. Fully initialize its all attributes. CNIC, name and address shall be initialized using constructor. Other attributes shall be initialized using setter methods. All attributes' values shall be given by user. After the object is fully initialized, print all attribute values using get methods. Make another object student2, assume the user also live at same address as student1. Reuse the address object of student1 to initialze student2 address. You don't need to take attributes from user input for student2 object. Print the values of all instance variables.

MY CODE:

StudentTest Class

import java.util.Scanner;

public class StudentTest {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter Country Code, City Code and Line Number (SEPARATELY): ");
        PhoneNumber phoneNumber = new PhoneNumber(sc.nextInt(), sc.nextInt(), sc.nextInt());

        System.out.println("Enter Street Address, Town, City and Country (SEPARATELY): ");
        Address address = new Address(sc.nextLine(), sc.nextLine(), sc.nextLine(), sc.nextLine(), phoneNumber);

        System.out.println("Enter Name and CNIC Number of Student 1 (SEPARATELY): ");
        Student student1 = new Student(sc.nextLine(), sc.nextLine(), address);

        System.out.println("Enter Email Address for Student 1: ");
        student1.setEmail(sc.nextLine());

        System.out.println("Course 1: Enter Course Title: ");
        student1.getCourse1().setCourseTitle(sc.nextLine());
        System.out.println("Course 1: Enter Course Code: ");
        student1.getCourse1().setCourseCode(sc.nextInt());

        System.out.println("Course 2: Enter Course Title: ");
        sc.nextLine(); //Dummy Input for String
        student1.getCourse2().setCourseTitle(sc.nextLine());
        System.out.println("Course 2: Enter Course Code: ");
        student1.getCourse2().setCourseCode(sc.nextInt());


        System.out.println("--------------------------------------------------------------------------------");
        System.out.println("Student 1 Details:");
        System.out.println("Name: "+student1.getName());
        System.out.println("CNIC: "+student1.getCnic());
        System.out.println("Address: " + student1.getAddress().getStreetAddress() + " , " + student1.getAddress().getTown()
                            + " , " + student1.getAddress().getCity() + " , " + student1.getAddress().getCountry()
                            + " , " + student1.getAddress().getPhoneNumber().getCountryCode()
                            + student1.getAddress().getPhoneNumber().getCityCode() + student1.getAddress().getPhoneNumber().getLineNumber());
        System.out.println("Email: "+student1.getEmail());
        System.out.println("Course 1:   Course Title: "+student1.getCourse1().getCourseTitle() + " | "
                                        + "Course Code: " + student1.getCourse1().getCourseCode());
        System.out.println("Course 2:   Course Title: "+student1.getCourse2().getCourseTitle() + " | "
                + "Course Code: " + student1.getCourse2().getCourseCode());

        System.out.println("--------------------------------------------------------------------------------");

        Student student2 = new Student("John", "35202", address);

        student2.setEmail("Student_02_Email@java.com");
        student2.getCourse1().setCourseTitle("EMO");
        student2.getCourse1().setCourseCode(105);
        student2.getCourse2().setCourseTitle("LA");
        student2.getCourse2().setCourseCode(102);


        System.out.println("--------------------------------------------------------------------------------");
        System.out.println("Student 2 Details:");
        System.out.println("Name: "+student2.getName());
        System.out.println("CNIC: "+student2.getCnic());
        System.out.println("Address: "+student2.getAddress().getStreetAddress() + " , " + student2.getAddress().getTown()
                + " , " + student2.getAddress().getCity() + " , " + student2.getAddress().getCountry()
                + " , " + student2.getAddress().getPhoneNumber().getCountryCode()
                + student2.getAddress().getPhoneNumber().getCityCode() + student2.getAddress().getPhoneNumber().getLineNumber());
        System.out.println("Email: "+student2.getEmail());
        System.out.println("Course 1:   Course Title: "+student2.getCourse1().getCourseTitle() + " | "
                + "Course Code: " + student2.getCourse1().getCourseCode());
        System.out.println("Course 2:   Course Title: "+student2.getCourse2().getCourseTitle() + " | "
                + "Course Code: " + student2.getCourse2().getCourseCode());

        System.out.println("--------------------------------------------------------------------------------");
    }
}

Student Class


public class Student {

    private String name; //Initialize using Constructor
    private String email;
    private String cnic; //Initialize using Constructor
    private Address address; //Initialize using Constructor
    
    private Course course1 = new Course();
    private Course course2 = new Course();


    public Student(String name, String cnic, Address address){
        setName(name);
        setCnic(cnic);
        setAddress(address);
    }

    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setCnic(String cnic){
        this.cnic = cnic;
    }
    public String 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;
    }
}

Address Class

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){
        setStreetAddress(streetAddress);
        setTown(town);
        setCity(city);
        setCountry(country);
        setPhoneNumber(phoneNumber);
    }

    public void setStreetAddress(String streetAddress){
        this.streetAddress = streetAddress;
    }
    public String getStreetAddress(){
        return streetAddress;
    }
    public void setTown(String town){
        this.town = town;
    }
    public String getTown(){
        return town;
    }
    public void setCity(String city){
        this.city = city;
    }
    public String getCity(){
        return city;
    }
    public void setCountry(String country){
        this.country = country;
    }
    public String getCountry(){
        return country;
    }
    public void setPhoneNumber(PhoneNumber phoneNumber){
        this.phoneNumber = phoneNumber;
    }
    public PhoneNumber getPhoneNumber(){
        return phoneNumber;
    }
}

PhoneNumber Class

public class PhoneNumber {

    private int countryCode;
    private int cityCode;
    private int lineNumber;

    public PhoneNumber(int countryCode, int cityCode, int lineNumber){
        setCountryCode(countryCode);
        setCityCode(cityCode);
        setLineNumber(lineNumber);
    }

    public void setCountryCode(int countryCode){
        this.countryCode = countryCode;
    }
    public int getCountryCode() {
        return countryCode;
    }
    public void setCityCode(int cityCode){
        this.cityCode = cityCode;
    }
    public int getCityCode(){
        return cityCode;
    }
    public void setLineNumber(int lineNumber){
        this.lineNumber = lineNumber;
    }
    public int getLineNumber(){
        return lineNumber;
    }
}

Course Class

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;
    }
}

Kaspersky
  • 1
  • 3

0 Answers0