Chapter 7 Class As A User Defined Data Type

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

CHAPTER 7

CLASS AS A USER DEFINED DATA TYPE

Class as a composite data type


# The data type that are based on fundamental or primitive data types are
known as composite data types. Because its data members are generally
of fundamental or primitive data types.
# It defines itself as a new data type through which many objects can be
defined.
Class as a user defined data type
# A class can contain data elements as well as the member functions. We
can use a class as a data type to declare variables of the class.
# The variables of the class are known as objects.
Primitive data types User defined data types
# They are built in data types. # They are created by the user
Java provides these data types. # The size of these data types are
# The size of these data types are variable as their size depends
fixed. up on their constituent
# These data types are available members.
in all parts of the java program # The availability of these data
types depends up on their
scope.

Creating and using objects


1. Declaring an object
Syntax
Classname objectname;
Eg: student ob;
2. Instantiating an object
# The new operator instantiates a class by allocating memory for an object,
and returing a reference to that memory.
# The new operator also invokes the object’s constructor.
# The name of the class provides the name of the constructor to instantiate.
3. Initializing an object
# Initial values are passed as constructor’s arguments.
# Eg: public class point
{
int x,y;
public point(int a, int b)
{
x=a;
y=b;
}
}
Point ob = new point (5,6);
This statement provides 5 and 6 as the initial value of instance variables x and y.
4. Referencing an object variable
# The dot (.) operator is used to refer to member of an object.
# Syntax
objectname.variablename;
Eg: ob.x;
# It refers to the data member x of the object ob.

5. Calling an object’s method


# By using the dot(.) operator.
# Syntax
object name.method name();
Eg: ob.area();

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

public void compute()


{
sum=m1+m2+m3;
avg=sum/3;
}
public void display( )
{
System.out.println(“Name\tAge\tSum\tAverage”);
System.out.println(name+”\t”+age+”\t”+sum+”\t”+avg);
}

public static void main(String args[])


{ student ob= new student();
student ob1=new student(9,”anu”,89.5,34,56.4);
ob.read( );
ob.compute();
ob.dispay( );
ob1.compute();
ob1.display();
}
}
What is the difference between a constructor defined with private or
protected access from the one defined with public access?
 If a constructor is defined with public access, the object of the class can
invoke the constructor irrespective of the class where the object is being
created.(ie, the object can be created inside or outside the class.)
 If a constructor is defined with private or protected access the constructor
can not be invoked during object creation in another class.(ie, object can
be created only inside the same class)
Question3
Design a class electricity as below:
Data members or Instance variable
name,conno(ConsumerNumber),units(Units Consumed),charge
Member Methods
 A parameterized constructor to initialize the data members.
 void read( ) – To accept the details
 void compute( )- To compute the electricity bill

Units Amount/Unit

Up to 100 units 80 paise

Next 100 units Rs 1

For more than 200 units Rs 1.25

 void display ( ) – For displaying details


Write a main method to create object of the class and call all above
methods.
Answer
import java.io.*;
Public class electricity
{
String name;
int conno,units;
float charge;
public electricity( string n, int c, int u)
{
name=n;
conno=c;
units=u;
}
public void read( ) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println(“Enter the name ,consumer number and units
consumed”);
name=br.readLine();
conno= Integer.parseInt(br.readLine());
units= Integer.parseInt(br.readLine());
}
public void compute()
{
if(units<=100)
charge=units*80/100;
else if(units>100 && units<=200)
charge=(100*80/100)+(units-100)*1;
else
charge=(100*80/100)+100*1+(units-200)*125/100;
}
public void display( )
{
System.out.println(“Name\tConsumerNumber\tUnits\tCharge”);
System.out.println(name+”\t”+conno+”\t”+units+”\t”+charge);
}
public static void main(String args[])
{
electricity ob= new electricity(“John”,7,10);
ob.read( );
ob.compute();
ob.dispay( );
}
}
Access Specifiers or Visibility modes
There are 3 visibility modes:
 Public
 Protected
 private
Public
The public members (both methods and data members) are accessible
everywhere, that is from, within the class as well as outside the even outside the
package.

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

public void input( ) throws IOException


{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println(“Enter the product code, pack size , price, flavour,
pack type”);
prod_code= Integer.parseInt(br.readLine());
pack_size= Integer.parseInt(br.readLine());
prod_price= Float.parseFloat(br.readLine());
flavour=br.readLine();
pack_type=br.readLine();
}
public void discount()
{
disc= prod_price*10/100;
prod_price = prod_price – disc;
}
public void display( )
{
System.out.println(“Product Code\tProduct Price”);
System.out.println(prod_code+”\t”+prod_price);
}
public static void main(String args[])
{
fruitjuice ob= new fruitjuice();
ob.input( );
ob.discount();
ob.dispay( );
}
}

You might also like