Tuesday, 9 July 2019

Published July 09, 2019 by with 0 comment

BCSL043 June 2013 Set 1 Solution




Question 1-Write a Java program to create student class. Define proper constructor to initialize  student class object. Define methods to get details of any student (the details may include student name, address, program of study, age). You need to take care of exceptions handling in this program.

solution -


import java.util.Scanner;

import java.util.*;

class student

{

String name,address,programOfStudy;

int age;

Scanner sc = new Scanner(System.in);

student()

{
System.out.println("Welcome to student information system\n");
getDetails();
}
void getDetails()
{
try
{
System.out.println("Enter you name:");
name = sc.nextLine();
System.out.println("Enter you Addresss:");
address = sc.nextLine();
System.out.println("Enter you program Of Study:");
programOfStudy = sc.nextLine();
System.out.println("Enter you age:");
age = sc.nextInt();
    }
    catch(InputMismatchException e){
             System.out.println("Please enter your age in numeric value");
             System.exit(0);
    }
}
void showDetails()
{
System.out.println("\n\n===========Details============");
System.out.println("Name:"+name);
System.out.println("Addresss:"+address);
System.out.println("Program of Study:"+programOfStudy);
System.out.println("Age:"+age);
}
}

public class text
{
public static void main(String[] args) {
student obj = new student();
obj.showDetails();
}
}

output:

Enter you name:
Ignoulia
Enter you Addresss:
Delhi,India
Enter you program Of Study:
BCA
Enter you age:
21
===========Details============
Name:Ignoulia
Addresss:Delhi,India
Program of Study:BCA
Age:21
Enter you name:
Ignoulia
Enter you Addresss:
Delhi,India
Enter you program Of Study:
BCA
Enter you age:
Twenty one
Please enter your age in numeric value
 


Scanner class is used to take the input from the console. nextLine(), nextInt() method of scanner class is used to take string, and integer repectively from the user.

if you have any doubt related to this post, you can ask in comment section and if you want solution of any previous year questions. please post that question in section we will try to solve that

Thank You




0 comments:

Post a Comment