Chapter 7 Class As A User Defined Data Type
Chapter 7 Class As A User Defined Data Type
Chapter 7 Class As A User Defined Data Type
Note
Variables declared inside the class – Data members
Methods inside the class – Member methods
A Class contains – Data members, Member Methods and Constructors.
Question 1
Design a class employee as below:
Data members or Instance variable
name
age
sal
Member Methods
read( ) – For getting the details of an employee
display ( ) – For displaying details of an employee
Write a main method to create object of the class and call all above
methods.
Answer
import java.io.*;
Public class employee
{
String name;
int age;
float sal;
public void read( ) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println(“Enter the name ,age and salary”);
name=br.readLine();
age= Integer.parseInt(br.readLine());
sal=Float.parseFloat(br.readLine());
}
public void display( )
{
System.out.println(“Name\tAge\tSalary”);
System.out.println(name+”\t”+age+”\t”+sal);
}
public static void main(String args[])
{ employee ob= new employee();
ob.read( );
ob.dispay( );
}
}
Question 2
Design a class student as below:
Data members or Instance variable
name,age,m1,m2,m3,sum,avg
Member Methods
A parameterized constructor to initialize the data members.
void read( ) – To accept the details of the student
void compute( )- To compute the sum and average
void display ( ) – For displaying details of the student.
Write a main method to create object of the class and call all above methods.
Answer
import java.io.*;
Public class student
{
String name;
int age;
float m1,m2,m3,sum,avg;
public student( )
{
age=0;
name=“ “;
m1=0.0;
m2=0.0;
m3=0.0;
}
public student( int a, string n, float r1, float r2, float r3)
{
age=a;
name=n;
m1=r1;
m2=r2;
m3=r3;
}
public void read( ) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println(“Enter the name ,age and the three marks”);
name=br.readLine();
age= Integer.parseInt(br.readLine());
m1=Float.parseFloat(br.readLine());
m2=Float.parseFloat(br.readLine());
m3=Float.parseFloat(br.readLine());
}
Units Amount/Unit
Protected
The protected members (both methods and data members) are accessible
inside all the classes in their own package, as well as in all sub classes of
their class.
Private
The private members(both methods and data members ) are accessible
only inside the class by the class methods.
Question 4
Design a class fruitjuice as below:
Data members or Instance variable
int prod_code,pack_size,prod_price
String flavour,pack_type
Member Methods
Fruitjuice( ) –A default constructor
void input( ) – To read the details of the product
void discount( )- To reduce the product price by 10 %
void display ( ) – For displaying details
Write a main method to create object of the class and call all above methods.
import java.io.*;
Public class fruitjuice
{
String flavour,pack_type;
int prod_code,pack_size,prod_price;
float disc;
public fruitjuice( )
{
prod_code=0;
pack_size=0;
prod_price=0;
flavour = “ “;
pack_type = “ “;
}