0% found this document useful (0 votes)
3 views27 pages

Constuctor 1

The document provides an overview of constructors in Java, detailing their features, types, and the concept of constructor overloading. It includes examples of various classes such as Student, BookFair, movieMagic, Mobike, ElectricBill, RailwayTicket, and ParkingLot, each demonstrating the use of constructors and member methods for specific functionalities. Additionally, it explains how to calculate values based on user input and includes sample code for each class.

Uploaded by

codingmaster2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views27 pages

Constuctor 1

The document provides an overview of constructors in Java, detailing their features, types, and the concept of constructor overloading. It includes examples of various classes such as Student, BookFair, movieMagic, Mobike, ElectricBill, RailwayTicket, and ParkingLot, each demonstrating the use of constructors and member methods for specific functionalities. Additionally, it explains how to calculate values based on user input and includes sample code for each class.

Uploaded by

codingmaster2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Constructor

volume obj =new volume();


Features
1.Initializes instance variable and object.
2.It has same name as class name
3.It will never return any value
4.It is always public
5.It gets called when object is created
Types
1.Default
It does not take any parameter and initialize instance variable to there default value.
2.Parameterized takes parameters and initializes it to instance variables.
eg
class a
{ int a1;
String b;
public a( ) //default
{ a1=0;
b=“”;
}
public a( int x , Sting y) // parametrised
{ a1=x; b=y;
}}
Constructor Functions
• Same name as class name • No same name as class

• No return type • It may or may not return


value.
• It is always public. • It can be public, private,
protected
• It gets called when object is • It is called when object calls
created. function.
Define a class Student with following data members

int grno; String name; int m1,m2,m3; double avg;


Member Methods are
public Student() default constuctor
void read() to read details of students
void calc() to calculate avg and display name and grade as follows.
Avg grade
80 to 100 A
60 to 79 B
40 to 59 C
Less than 40 D
import java.util.*; void read()
class Student { Scanner sc=new Scanner(System.in);
Sopln(“Enter the name , grno,m1,m2m3”);
{ int grno; name=sc.nextLine();
String name; grno=sc.nextInt();
int m1; int m2; int m3; m1=sc.nextInt();
double avg; m2=sc.nextInt();
public Student() m3=sc.nextInt(); }
void calc()
{ grno=0; { avg=m1+m2+m3/3.0;
m1=0; if( avg>=80 && avg<=100) Sopln(name +”A grade”);
m2=0; else
m3=0; if( avg >=60 && avg<=79) Sopln(name +” B grade”);
name=“”; else
if ( avg>=40 && avg <=59) Sopln(name +”C grade”);
avg=0.0d;} else
Sopln(name +”D grade”); }
psvm()
{ Student obj=new Student();
obj.read();
obj.calc(); } }
Define a class named BookFair with the following
description

String bname stores the name of the book


double price stores the price of book
Member methods
1.BookFair() default constructor
2. void Input() to input and store name and price
3. void calculate() to calculate the price after discount .
Price Discount
Less than or equal to 1000 2 % of price
More than 1000 less than 3000 10 % of price
More than 3000 15 % of price
4.Void display() – to print bookname and price to be paid.
Write a main method to create the object of class and call the above member
method methods.
import java.util.*;
class BookFair
{
String bname;
double price;
double dis;

public BookFair()//default constructor


{
bname="";
price=0.0d;
dis=0.0d;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name,price ");
bname=sc.next();
price=sc.nextDouble();
}
void calculate()
{
if (price >0 && price <=1000)
dis=(2.0*price)/100;
else
If( price >1000 && price <=3000)
dis=(10.0*price)/100;
else
dis=(15.0*price)/100;
}
void display()
{
System.out.printiln(“Bookname”+bname);
System.out.println(“Final Price”+ ( price-dis));
}

public static void main()


{
BookFair obj=new BookFair();
obj.input();
obj.calculate();
obj.display();
}}
Define a class named movieMagic with the following description

int year to store the year of the movie


String title to store the title of movie
float rating to store the popularity of movie
Member methods
1 .movieMagic () the default constructor
2. void accept () to input the title and year of the movie and the rating
3. void display() to display the title of the movie based upon the rating
Rating Result
0.0 to 2.0 flop
2.1 t0 3.4 semi hit
3.5 to 4.5 hit
4.6 to 5.0 superhit
Write a main method to create the object of class and call the above member method methods
import java.util.*;
class movieMagic
{
int year; float rating; String title;
public movieMagic()
{
year=0;
rating=0.0f;
title="";
}
void accept()
{Scanner sc=new Scanner(System.in);
System.out.println("Enter year of release");
year= sc.nextInt();
System.out.println("Enter title");
title=sc.next();
System.out.println("Enter rating (minimum 0.0 and maximum 5.0)");
rating= sc.nextFloat();
}
void display()
{
if (rating >0.0 && rating <=2.0)
System.out.println("Flop“+ title);
else if (rating>=2.1 && rating<=3.4)
System.out.println("SemiHit“+ title);
else if (rating>=3.5 && rating<=4.5)
System.out.println("Hit“+title);
else
System.out.println("Super Hit“+title);
}
public static void main()
{
movieMagic object = new movieMagic();
object.accept();
object.display();
}
}
Define a class Mobike with the following data member
int bno-bike no
int phno-phone no
String name-name of customer
int days- no of days the bike is taken on rent
int charge-to calculate the charge
Member methods are
Public Mobike()- default constructor
void input()-to store details of customer
void compute()-to find the charge as follows
Days rate/day
First 5 days 500/-
Next 5 days 400/-
Rest of the days 200/-
void display()-to print the details as follows
Bike No Phno Name Days Charge
Xxx xxxx xxxx xxxx xxxxx
Write a main method to create the object of class and call the above member method methods
import java.util.*;
class Mobike
{
String name;
int bno;
int phno;
int days;
int charge;
public Mobike()//default constructor
{
name="";
phno=0;
days=0;
charge=0;
bno=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name,days, phno, bno ");
name=sc.next();
phno=sc.nextInt();
days=sc.nextInt();
bno=sc.nextInt(); }
void compute()
{
if (days>=1 && days <=5)
charge=days*500;
else
If(days>=6 && days <=10)
charge=(5*500)+(days-5)*400;
else
charge=(5*500)+(5*400)+(days-10)*200;
}
void display()
{
System.out.printiln(“name phno bno days charge “);
System.out.println(name +”/t”+phno+”/t”+bno+”/t”+days+”/t”+charge);
}

public static void main()


{
Mobike obj=new Mobike();
obj.input();
obj.compute();
obj.display();
}}
Define a class ElectricBill with the instance variables
String n-name of customer
int units- no of units consumed
double bill=to store the bill
Member methods are
public ElectricBill()-default constructor
void accept()-to store name and unit
void cal()- to calculate the bill as follows
Units rate/unit
First 100 2/-
Next 200 3/-
Above 300 5/-
A surcharge of 2.5 % charged if units consumed is above 300 units.
void print()- to print all the details
Write a main method to create the object of class and call the above member method
methods
import java.util.*;
public class ElectricBill {
int units;
String n;
double bill;
public ElectricBill(){
units=0;n=“”;bill=0.0d;}
void accept() {
Scanner sc = new Scanner (System.in);
System.out.println("Enter Name of the customer");
n=sc.next();
System.out.println("Enter Number of units consumed");
units=sc.nextInt(); }
void cal() {
if (units<=100)
bill=units*2;
else
if (units >100 && units <=300)
bill=100*2+(units-100)*3;
else{
bill=100*2+200*3+(units-300)*5;
bill=bill+2.5/100*bill; }}
void print() {
System.out.println("Name of the customer:"+n);
System.out.println("Number of Units consumed :"+units);
System.out.println("Bill Amount :"+bill); }
public static void main() {
ElectricBill obj=new ElectricBill();
obj.accept(); obj.cal(); obj.print(); } }
Design a class RailwayTicket with following description:
Instance variables/data members :
String name : To store the name of the customer
String coach : To store the type of coach customer wants to travel
long mobno : To store customer’s mobile number
int amt : To store basic amount of ticket
int totalamt : To store the amount to be paid after updating the original amount
Member methods :
void accept () – To take input for name, coach, mobile number and amount.
void update () – To update the amount as per the coach selected
(extra amount to be added in the amount as follows)
Type of Coaches Amount

First_AC 700
Second_AC 500
Third_AC 250
sleeper None
void display () – To display all details of a customer such as name, coach, total
amount and mobile number.
Write a main method to create an object of the class and call the above member
methods.
import java.util.*;
class RailwayTicket
{
String name, coach;
long mobno;
int amt ,totalamt;

void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("enter name , coach and mobile no and amount");
name=sc.next();
coach=sc.next();
mobno=sc.nextLong();
amt=sc.nextInt(); }
void update()
{
if(coach. equalsIgnoreCase ("First_AC"))
totalamt=amt+700;
else if(coach. equalsIgnoreCase ("Second_AC"))
totalamt=amt+500;
else if(coach. equalsIgnoreCase ("Third_AC"))
totalamt=amt+250;
else
totalamt=amt;
}
void display() {
System.out.println("name is :"+name);
System.out.println("mobile no is :"+mobno);
System.out.println("coach is :"+ coach);
System.out.println("total amount is :"+totalamt); }
public static void main ()
{
RailwayTicket ob =new RailwayTicket();
ob.accept();
ob.update();
Define a class called ParkingLot with the following description :
Instance variables/data members :
int vno - To store the vehicle number
int hours - To store the number of hours the vehicle is parked in the
parking lot
double bill - To store the bill amount
Member methods :
void input() - To input and store the vno and hours.
void calculate() - To compute the parking charge at the rate of 3 for
the first hour or part thereof,
and 1.50 for each additional hour or part thereof.
void display() - To display the detail
Write a main method to create an object of the class and call the
above methods
import java.util.*;
class ParkingLot
{
int vno;
int hours;
double bill;
void input()
{Scanner sc=new Scanner(System.in);
System.out.println("Enter the vehicle number and number of hours");
vno=sc.nextInt();
hours=sc.nextInt();
}
void calculate()
{
if(hours<=1)
bill= 3;
else
bill=3+(hours-1)*1.50;
}
void display()
{
System.out.println("Vechicle Number:"+vno);
System.out.println("Hours:"+hours);
System.out.println("Bill:"+bill);
}

public static void main()


{
ParkingLot pl=new ParkingLot();
pl.input();
pl.calculate();
pl.display();
}
}
Constructor Overloading:
Using more than one constructor in the same class.
Eg:
class Xyz
{ int a1;
String b;
public Xyz() //default
{ a1=0;
b=“”;
}
public Xyz( int p , String q)
{ a1=p;
b=q;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy