Menu
Topics Index
...
`

Classes846


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes846
What will be the output of the program?
class ClassDataValidation
{
    public static void main(String arg[])
    {
        Student ashok= new Student("Ashok");

        ashok.print("LINE A");

        ashok.marks += 20;
        ashok.section = 'B';
        ashok.print("LINE B");

        ashok.marks += 20;                
        ashok.section = 'C';
        ashok.print("LINE C");

        ashok.setMarks(40);
        ashok.section = 'D';
        ashok.print("LINE D");    
    }
}

class Student
{
    String name;
    int marks = 20;
    char section = 'A';

    Student(String name)
    {
        this.name = name;
    }
    
    void setMarks(int marks)
    {
        if( marks >= 0 && marks <= 100)
        {
            this.marks = 35;
        }
    }
    
    void setSection(char section)
    {
        if( section == 'A' || section == 'B' || section == 'C' || section == 'D')
        {
            this.section = section;
        }
    }
    
    void setDetails(int marks, char section)
    {
        setMarks(marks);
        setSection(section);
    }

    void print(String LINEA)
    {
        System.out.println(LINEA + " : " + name + " - " + marks + " - " + section + ".");
    }

}

A. LINE A : Ashok - 20 - A.
LINE B : Ashok - 40 - B.
LINE C : Ashok - 60 - C.
LINE D : Ashok - 40 - D.
B. LINE A : Ashok - 20 - A.
LINE B : Ashok - 40 - A.
LINE C : Ashok - 80 - A.
LINE D : Ashok - 80 - A.
C. <cw>LINE A : Ashok - 35 - A.
LINE B : Ashok - 35 - B.
LINE C : Ashok - 35 - C.
LINE D : Ashok - 35 - D.
D. LINE A : Ashok - 20 - A.
LINE B : Ashok - 40 - B.
LINE C : Ashok - 60 - C.
LINE D : Ashok - 80 - D.
E. LINE A : Ashok - 20 - A.
LINE B : Ashok - 40 - B.
LINE C : Ashok - 60 - C.
LINE D : Ashok - 35 - D.
Topic: Creating A Class For Data Validation

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is E........
setter methods are basically used for validating the values and then set the values to data members..General practise is to declare the member variables as private and expose the methods(i.e declare the methods to be public) to set the values after the required validations

new Student("Ashok") will invoke the constructor which sets the name to passed in argument i.e ashok
Marks and section holds default values as they are declared which is 20 and 'A'
1st print function will print Line A : Ashok - 20 - A

Next we are setting marks = marks + 20 which assigns marks with 40
section is set to 'B'.
2nd print function will print Line B : Ashok - 40 - B

Next we are setting marks = marks + 20 which assigns marks with 60
section is set to 'C'.
2nd print function will print Line C : Ashok - 60 - C

Next we are setting marks using setter method setmarks(40) which checks if marks >= 0 and marks <= 100 which is true.
So marks is set to 35
section is set to 'D'.
2nd print function will print Line D : Ashok - 35 - D

Posted by Shashanka Mogaliraju    2015-03-03 09:32:21


ANS IS E

Here,the concept of classes and objects is used.

--Here, class student is defined with following attributes:
Name
Section
Marks

Following methods:
Setsection()
Setmarkas()
Print()
Setdetails()

And one constructor with one string parameter for accepting name of student.

--First of all,object of student class is created by passing Ashok to constructor of student class.

--After that,"Line A" is passed to print method.
So,
Line A:Ashok 20 A

--After that marks are increased by 20 and section is assigned "B" and print() method is called by passing "Line B".
So,
Line B: Ashok 40 B will get printed.

--After that marks are increased by 20 and section is assigned "C" and print() method is called by passing "Line C".
So,
Line C: Ashok 60 C will get printed.

--After that setmarks() method is called by passing 40.If marks are between 0 and 100 then marks are assigned to 35.section is assigned "D" and print() method is called by passing "Line D".
So,
Line D: Ashok 35 D will get printed.

OUTPUT:

Line A: Ashok 20 A
Line B: Ashok 40 B
Line C: Ashok 60 C
Line D: Ashok 35 D.

Posted by Mânïshå Mùlchåndânï    2015-03-03 14:00:08


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Sonu Nagdev. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-04 03:42:33


© meritcampus 2019

All Rights Reserved.

Open In App