How to Handle Exceptions In Java

Vu Old Solved Programming Based Assignments Codes are Available in this Section.
Post Reply
User avatar
ALI RAZA
★★★★★★★★★★★★★
★★★★★★★★★★★★★
Posts: 177
Joined: Fri Dec 22, 2017 8:10 am
Location: Lahore
Has thanked: 6 times
Been thanked: 7 times
Contact:
Status: Offline

How to Handle Exceptions In Java

Post by ALI RAZA » Wed Oct 03, 2018 7:28 am

You are required to write a program in java that will have two classes i.e. Person and Student.

1. Person will be the super / parent class and it will have four data members i.e. CNIC, Name, Date of Birth and Address. Person class will have a parameterized constructor (that will initialize all the data members with the given values) along with getters and setters for each data member that will get and set the values of data members of the class.

2. Student will be the child class and it will be inherited from Person class. It will have three data members i.e. Student ID, Study Program and Email. Student class will also have a parameterized constructor (that will initialize all the data members with the given values), getters and setters for each data member (that will get and set the values of data members of the class) and a main method. In main method, you’ll will show following options to user and take user choice by using JOptionPane.showInputDialog.

• Press 1 to add a new student.
• Press 2 to display all students.
• Press 3 to exit.

If user enters 1 then take the values of CNIC, Name, Date of Birth, Address, Student ID, Study Program and Email using JOptionPane.showInputDialog and create an instance of Student class by using its parameterized constructor. Now, store all this information in a text file (students.txt) separated by commas ( , ) in a single line. Whenever, user enters a new record then it should not affect the previous records in text file. Note that you have to enter your own information while entering first record.

If user enters 2 then display record of all the students available in students.txt file. Your output should be displayed as given below:




Student 1:

CNIC : 12345-123456-1
Name: Ali
Date of Birth : May 1
Address: Lahore
Student ID: BC00000000 ( Must be same as your own VU ID)
Program: BSCS
Email: bc00000000@vu.edu.pk

Student 2:

CNIC : 12345-123456-2
Name: Ahmad
Date of Birth : March 1
Address: Lahore
Student ID: MC00000000
Program: MCS
Email: mc00000000@vu.edu.pk

Student 3
.
.
.

And lastly, if user enters 3 then exit from the program.

You also have to handle exceptions properly in your program.
Answer:

Person.java

Code: Select all

public class Person 
{

    String cnic;

    String name;

    String dob;
    
    String address;

// parametrized constructor

    public Person (String cnic, String name, String dob, String address) 
    {

        this.cnic = cnic;

        this.name = name;

        this.dob = dob;
        
        this.address = address;

    }

    // setter and getter fucations

    public void setCnic (String cnic) 
    {

          this.cnic = cnic;

    }

    public void setName (String name) 
    {

        this.name = name;

    }
    
    public void setDob (String dob) 
    {

        this.dob = dob;

    }

    public void setAddress (String address) 
    {

        this.address = address;

    }    
    // getter fucations

    public String getCnic() 
    {

        return cnic;

    }


    public String getName () 
    {

        return name; 

    }

    public String getDob () 
    {

        return dob;

    }

    public String getAddress () 
    {

        return address;

    }

} // end of class
Student.java

Code: Select all

import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.*;
public class Student extends Person 
{

    String stdId;

    String program;

    String email;

    // parameterized construtor

    public Student (String cnic, String name, String dob, String address, String stdId, String program, String email) 
    {
        // call superclass constructor

        super (cnic,name,dob, address);

        this.stdId = stdId;

        this.program = program;

        this.email = email;
    }

    // Setters

    public void setStdId (String stdId) 
    {
        this.stdId = stdId;
    }

    public void setProgram (String program) 
    {

        this.program = program;

    }

    public void setEmail (String email) 
    {

        this.email = email;

    }

    // Getters

    public String getstdId () 
    {

        return stdId;

    }

    public String getProgram () 
    {

        return program;

    }

    public String getEmail () 
    {

        return email;

    }

    public static void addRecord(Student s)
    {
        try {

            String line;
            File file = new File("C:\\students.txt");
            FileWriter fw = new FileWriter(file, true);
            PrintWriter pw = new PrintWriter(fw);

            line = s.cnic +","+ s.name +","+ s.dob +","+s.address+","+s.stdId+","+s.program+","+s.email;

            pw.println(line);
            pw.flush();
            pw.close();
            fw.close();
        }catch(IOException ioEx)
        {    
            System.out.println(ioEx);
        }
    }
     
    public static void showRecords()
    {
    
            String tokens[] = null;
            int counter=1;
            try {
                File file = new File("C:\\students.txt");
                FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                String line = br.readLine();

                while ( line != null ) {
                    tokens = line.split(",");
                    System.out.println(" Student "+ counter +" \n");
                    System.out.println(" CNIC : "+ tokens[0] +" \n");
                    System.out.println(" Name : "+ tokens[1] +" \n");
                    System.out.println(" Date of Birth : "+ tokens[2] +" \n");
                    System.out.println(" Address : "+ tokens[3] +" \n");
                    System.out.println(" Student ID : "+ tokens[4] +" \n");                    
                    System.out.println(" Study Program : "+ tokens[5] +" \n");                    
                    System.out.println(" Email : "+ tokens[6] +" \n");                    
                  
                    counter++;
                    line = br.readLine();
                }
                
                br.close();
                fr.close();
            }catch(IOException ioEx){
            System.out.println(ioEx);
            }
    
    }
            
    
public static void main(String args[])
{

    int choice= 1;
    String input;
    
    while (choice!=3)
    {
        input = JOptionPane.showInputDialog("Enter 1 to add a new student" + 
                "\n Enter 2 to display all students" + 
                "\n Enter 3 to Exit");
        choice = Integer.parseInt(input);
        
        if(choice == 1)
        {
            String nic = JOptionPane.showInputDialog("Enter CNIC : ");
            String sName = JOptionPane.showInputDialog("Enter Name : ");
            String  sDob= JOptionPane.showInputDialog("Enter Date of Birth : ");
            String  sAddress= JOptionPane.showInputDialog("Enter Address : ");
            String  sID= JOptionPane.showInputDialog("Enter Sudent ID : ");
            String  sProgram= JOptionPane.showInputDialog("Enter Study Program : ");
            String  sEmail= JOptionPane.showInputDialog("Enter Email Address : ");
            
            Student s = new Student(nic,sName,sDob,sAddress,sID,sProgram,sEmail);
            addRecord(s);
        }

        if(choice == 2)
        {
            showRecords();
        }
    }
        
        
}
} // end of class



Post Reply

Last 10 Members Who Visited This Topic

  • Similar Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 1 guest