You are on page 1of 24

ASSIGNMENT

Submitted To : Maam Saneeha Aamir

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.

Test the VerifiedSimple class in runner.

Super Class :

package runner2;

public class Simple {

protected int num1;

protected int num2;

public Simple(){};

public Simple(int n1,int n2){

num1=n1;

num2=n2;

public void add(){

System.out.println(num1+num2);

public void subtract(){

System.out.println(num1-num2);
}

public void divide(){

System.out.println(num1/num2);

public void multiply(){

System.out.println(num1*num2);

}}

Child Class :

package runner2;

public class VerifiedSimple extends Simple {

public VerifiedSimple(){

super();

public VerifiedSimple(int n1,int n2){

super(n1,n2);

public void add(){

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

public void divide(){

if(num1/num2>0)

super.divide();

else

System.out.println("Error");}

public void multiply(){

if(num1*num2>0)

super.multiply();

else

System.out.println("Error");

}}
RUNNER:

package runner2;

public class Runner2 {

public static void main(String[] args) {

Simple sp=new Simple(8,5);

sp.add();

System.out.println("\n");

sp.subtract();

System.out.println("\n");

sp.multiply();

System.out.println("\n");

sp.divide();

System.out.println("\n");

VerifiedSimple sp1=new VerifiedSimple(8,5);

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

public static boolean ContainsKeyword(Document docObject, String keyword)


{
if (docObject.toString().indexOf(keyword,0) >= 0)
return true ;
else

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.

In the runner class; call the tostring() method polymorphically.


Also, create sample objects of type Email and File. Test your objects by passing them to
the function “ContainsKeyword” of document class.
RUNNER :

package Question2;

import static Question2.Document.ContainsKeyword;

public class RUNNER {

public static void main(String[] args) {

String s1="I Just Want To Say Thank You For The Present";

String s2="Please Report As Soon As Possible";

String s3="In Meeting.";

Document nw=new Document("Ihtesham is a Noob.");

Document[]now=new Document[3];

now[0]=new Email(s1,"Hurrira","Najam","Thank You!");

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 class File extends Document {

private String pathname;

public File(){super();};

public File(String txt,String pn){

super(txt);

pathname=pn;}

public String getText(){return super.ToString();}

public String ToString(){

return "Path name : "+pathname+"\n"+"-----"+"\n"+super.ToString();

}
package Question2;

public class Email extends Document {

private String sender;

private String reciepent;

private String TitleofEmail;

public Email(){super();}

public Email(String txt,String send,String rep,String toe){

super(txt);

sender=send;

reciepent=rep;

TitleofEmail=toe;

public void setSender(String s){sender=s;}

public void setReciepent(String r){reciepent=r;}

public void setTitleofEmail(String to){TitleofEmail=to;}

public String getSender(){return sender;}

public String getReciepent(){return reciepent;}

public String getTitleofEmail(){return TitleofEmail;}

public String getText(){return super.ToString();}

public String ToString(){

return "From : "+sender+"\n"+"To : "+reciepent+"\n"+"Title of Email :


"+TitleofEmail+"\n"+"-------"+"\n"+super.ToString();
}

package Question2;

public class Document {

protected String text;

public Document(){};

public Document(String txt){

text=txt;

public void setText(String txt){text=txt;}

public String getText(){return text;}

public String ToString(){return text;}

public static boolean ContainsKeyword(Document docObject, String keyword)

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 class Person {

protected String Name;

protected int age;

public Person(){};

public Person(String n,int a ){

Name=n;

age=a;

};

}
package assignment;

public class Truck extends Vehicle {

private double loadcapacity;

private int towingcapacity;

public Truck(){super();}

public Truck(String mn,int noc,Person own,double lc,int tc){

super(mn,noc,own);

loadcapacity=lc;

towingcapacity=tc;

public void display(){

System.out.println("Name Of The Owner : "+super.getOwner().Name);

System.out.println("Age of The Owner : "+super.getOwner().age);

System.out.println("Manufacturer Of Truck : "+super.getManufacturerName());

System.out.println("Number of Cylinders In the Truck : "+super.getNumberofCylinders());

System.out.println("Loading Capacity of Truck "+ loadcapacity+" TONS");

System.out.println("Towing Capacity of Truck "+ towingcapacity+" TONS");

}
package assignment;

public class Vehicle {

private String manfacturername;

private int numberofcylinders;

private Person owner;

public Vehicle(){};

public Vehicle(String mn,int noc,Person own){

manfacturername=mn;

numberofcylinders=noc;

owner=own;

public String getManufacturerName(){return manfacturername;}

public int getNumberofCylinders(){return numberofcylinders;}

public Person getOwner(){return owner;}

}
RUNNER:

package assignment;

public class RUNNER {

public static void main(String[] args) {

Person Now=new Person("Nouman",22);

Truck Nib=new Truck("Dogde",12,Now,44.44,44);

Nib.display();

}}
4. Create the classes shown below:

RentedCar Car Person

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;

public class RUNNER {


public static void main(String[] args) {
Customer pro=new Customer("Khalid",33,"409","1112342");
Car noob=new Car("XLI","2008","13421");
Rented_Car now=new Rented_Car(noob,pro);
now.Display();

System.out.println("\n");

Customer pro1=new Customer("Shujah",29,"401","1132342");


Car noob1=new Car("GLI","2016","13321");
Rented_Car now1=new Rented_Car(noob1,pro1);
now1.Display();

}}

package Class;

public class Rented_Car {

private Car c;

private Customer cu;

public Rented_Car(Car coco,Customer mo){

c=coco;

cu=mo;

public void Display(){

c.Display();

cu.Display();
}

public boolean checkValidity(){

if(cu.age>=18)

return true;

else

return false;

package Class;

public class Person {


protected String Name;
protected int age;

public Person(){};
public Person(String n,int a ){
Name=n;
age=a;}

package Class;
public class Customer extends Person {

private String LisenceNumber;


private String RegistrationID;

public Customer(){};
public Customer(String n,int a,String lc,String rg){
super(n,a);
LisenceNumber=lc;
RegistrationID=rg;
}

public void Display(){


System.out.println("Name of The Person is "+super.Name);
System.out.println("Age of The Person is "+super.age);
System.out.println("License Number : "+LisenceNumber);
System.out.println("Registration ID : "+RegistrationID);

package Class;

public class Car {

private String model;


private String number;
private String rentalID;

public Car(){};
public Car(String md,String nb,String ID){
model=md;
number=nb;
rentalID=ID;
}

public void Display(){


System.out.println("Model of Car : "+model);
System.out.println("Number of Car : "+number);
System.out.println("Rental ID of Car/Cars : "+rentalID);
}
}

5. Create a class hierarchy that performs conversions from one system of units to another.
Your program should perform the following conversions,

i. Liters to Gallons, ii. Fahrenheit to Celsius and iii. Feet to Meters

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;

public class RUNNER {

public static void main(String[] args) {

convert []cro=new convert[3];


cro[0]=new LiterToGallon(2,3);
cro[1]=new FahrenheitToCelsius(2,3);
cro[2]=new FeetToMeters(2,3);

for(int i=0;i<cro.length;i++)
cro[i].compute();

}}

package Conversion;

public abstract class convert {

protected int val1;


protected int val2;

public convert(){};
public convert(int v1,int v2){
val1=v1;
val2=v2;
}

public void setValue1(int n1){val1=n1;}


public void setValue2(int n2){val2=n2;}
public int getValue1(){return val1;}
public int getValu2(){return val2;}

public abstract void compute();


}

package Conversion;

public class LiterToGallon extends convert {

public LiterToGallon(){super();}
public LiterToGallon(int v1,int v2){super(v1,v2);}

public void compute(){


System.out.println(super.val1 +" Litres + "+super.val2+ " = "+ super.val1*0.264172+" Gallons
+ "+super.val2*0.264172);

}
}
package Conversion;

public class FeetToMeters extends convert {

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

You might also like