Professional Documents
Culture Documents
HARIS KAYANI
SP18-BSE-062 || III-B
1. Write a program that declares two classes. The parent class is called Simple that has
two data members num1 and num2 to store two numbers. It also has four member
functions.
The add() function adds two numbers and displays the result.
The sub() function subtracts two numbers and displays the result.
The mul() function multiplies two numbers and displays the result.
The div() function divides two numbers and displays the result.
The child class is called VerifiedSimple that overrides all four functions. Each function
in the child class checks the value of data members. It calls the corresponding member
function in the parent class if the values are greater than 0. Otherwise it displays error
message.
Super Class :
package runner2;
public Simple(){};
num1=n1;
num2=n2;
System.out.println(num1+num2);
System.out.println(num1-num2);
}
System.out.println(num1/num2);
System.out.println(num1*num2);
}}
Child Class :
package runner2;
public VerifiedSimple(){
super();
super(n1,n2);
if(num1+num2>0)
super.add();
else
System.out.println("Error");}
public void subtract(){
if(num1-num2>0)
super.subtract();
else
System.out.println("Error");}
if(num1/num2>0)
super.divide();
else
System.out.println("Error");}
if(num1*num2>0)
super.multiply();
else
System.out.println("Error");
}}
RUNNER:
package runner2;
sp.add();
System.out.println("\n");
sp.subtract();
System.out.println("\n");
sp.multiply();
System.out.println("\n");
sp.divide();
System.out.println("\n");
sp1.add();
System.out.println("\n");
sp1.subtract();
System.out.println("\n");
sp1.multiply();
System.out.println("\n");
sp1.divide();
}}
2. Define a class named Document that contains an instance variable of type String
named text that stores any textual content for the document. Create a method named
toString that returns the text field and also include a method to set this value. Create
a method listed below in this class
return false ;
}
Next, define a class for Email that is derived from Document and includes instance
variables for the sender , recipient , and title of an email message. Implement
appropriate accessor and mutator methods. The body of the email message should be
stored in the inherited variable text . Redefine the toString method to concatenate all
text fields.
Similarly, define a class for File that is derived from Document and includes an instance
variable for the pathname . The textual contents of the file should be stored in the
inherited variable text . Redefine the toString method to concatenate all text fields.
package Question2;
String s1="I Just Want To Say Thank You For The Present";
Document[]now=new Document[3];
now[1]=new Email(s2,"Bilal","Sharjeel","ASAP");
now[2]=new File(s3,"112342");
for(int i=0;i<now.length;i++){
System.out.println(now[i].ToString());
System.out.println("\n");}
System.out.println("\n");
System.out.println(ContainsKeyword(now[0],"Kite"));
System.out.println(ContainsKeyword(now[0],"Say"));
}
package Question2;
public File(){super();};
super(txt);
pathname=pn;}
}
package Question2;
public Email(){super();}
super(txt);
sender=send;
reciepent=rep;
TitleofEmail=toe;
package Question2;
public Document(){};
text=txt;
if (docObject.toString().indexOf(keyword,0) >= 0)
return true ;
else
return false ;
}
}
3. Consider a class Person that contains Name (type String), Age (type int), as data members.
This class contains argument constructor which initializes all data members.
Now Create a class Vehicle that has the manufacturer’s name (String), number of cylinders in the
engine (int ), and owner (type Person). Include argument Constructor to initialize all data
members.
Then, create a class called Truck that is derived from Vehicle and has the following additional
properties: the load capacity in tons (type double ) and towing capacity in pounds (type int).
Include argument constructor to initialize all data members. This class should have a display
method that displays the “load capacity of truck”, “towing Capacity” , “manufactor’s name of
truck”, number of cylinders in the engine”, “name of the owner of truck” and “Age of the owner
of the truck”.
package assignment;
public Person(){};
Name=n;
age=a;
};
}
package assignment;
public Truck(){super();}
super(mn,noc,own);
loadcapacity=lc;
towingcapacity=tc;
}
package assignment;
public Vehicle(){};
manfacturername=mn;
numberofcylinders=noc;
owner=own;
}
RUNNER:
package assignment;
Nib.display();
}}
4. Create the classes shown below:
Data: Data:
Data:
Private String model; Private String name;
Private Car c;
Private String number; Private int age;
Private Customer ct;
Private String rentalID; Methods:
Methods:
Methods: Argument Constructor
Argument Constructor
Argument Constructor Display
Display
CheckValidityOfcustomer Display
Customer
Data:
Private String
LisenceNumber;
private String
RegistrationID;
Methods:
Argument Constructor
In the runner book two car and call display function. Display
RUNNER :
package Class;
System.out.println("\n");
}}
package Class;
private Car c;
c=coco;
cu=mo;
c.Display();
cu.Display();
}
if(cu.age>=18)
return true;
else
return false;
package Class;
public Person(){};
public Person(String n,int a ){
Name=n;
age=a;}
package Class;
public class Customer extends Person {
public Customer(){};
public Customer(String n,int a,String lc,String rg){
super(n,a);
LisenceNumber=lc;
RegistrationID=rg;
}
package Class;
public Car(){};
public Car(String md,String nb,String ID){
model=md;
number=nb;
rentalID=ID;
}
5. Create a class hierarchy that performs conversions from one system of units to another.
Your program should perform the following conversions,
The Super class convert declares two variables, val1 and val2, which hold the initial and
converted values, respectively. The class contains one argument constructor, set function
for val1 and get functions for val1 and val2. It also contains an abstract function
“compute()”.
The function that will actually perform the conversion, compute() must be defined by the
classes derived from convert. The specific nature of compute() will be determined by what
type of conversion is taking place.
Three classes will be derived from convert to perform conversions of Liters to Gallons
(l_to_g), Fahrenheit to Celsius (f_to_c) and Feet to Meters (f_to_m), respectively. Each
derived class implements compute() in its own way to perform the desired conversion.
Test these classes from main() to demonstrate that even though the actual conversion
differs between l_to_g, f_to_c, and f_to_m, the interface remains constant
(Polymorphism).
RUNNER :
package Conversion;
for(int i=0;i<cro.length;i++)
cro[i].compute();
}}
package Conversion;
public convert(){};
public convert(int v1,int v2){
val1=v1;
val2=v2;
}
package Conversion;
public LiterToGallon(){super();}
public LiterToGallon(int v1,int v2){super(v1,v2);}
}
}
package Conversion;
public FeetToMeters(){super();}
public FeetToMeters(int v1,int v2){super(v1,v2);}
public void compute(){
System.out.println(super.val1+" Feets "+super.val2+ " = "+super.val1*.3048+" Metres
"+super.val2*.3048);
package Conversion;
public class FahrenheitToCelsius extends convert {
public FahrenheitToCelsius(){super();}
public FahrenheitToCelsius(int v1,int v2){super(v1,v2);}
public void compute(){
System.out.println(super.val1+" Farehiet "+super.val2+ " = "+(super.val1-32)*.5556+" Celcius
"+(super.val2-32)*.5556);