Sie sind auf Seite 1von 20

OBJECT ORIENTED PROGRAMMING

ASSIGNMENT

QUESTION # 1
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.

public class Simple {


protected double num1;
protected double num2;

public Simple() {
num1 = 0.0;
num2 = 0.0;
}

public Simple(double Num1, double Num2) {


num1 = Num1;
num2 = Num2;
}

public void setNum1(double num) {


num1 = num;

public double getNum1() {


return num1;
}

public void setNum2(double num) {


num2 = num;

public double getNum2() {


return num2;
}

public void add() {


double sum = num1 + num2;
System.out.println("The sum of 2 numbers is: " + sum);
}

public void sub() {


double sub = num1 - num2;
System.out.println("The sub of 2 numbers is: " + sub);
}

public void mul() {


double multiply = num1 * num2;
System.out.println("Multiplication of 2 numbers is: " + multiply);
}

public void div() {


double division = num1 / num2;
System.out.println("Division of 2 numbers is: " + division);
}

public void display() {


System.out.println("Num1 is: " + num1);
System.out.println("Num2 is: " + num2);
}
}

public class VerifiedSimple extends Simple {


public VerifiedSimple() {
super();
}

public VerifiedSimple(double Num1, double Num2) {


super(Num1, Num2);
}

@Override
public void add(){
if((super.getNum1() > 0) && ( super.getNum2() > 0))
super.add();
else
System.out.println("Error");
}

@Override
public void sub(){
if((super.getNum1()>0) && (super.getNum2()>0))
super.sub();
else
System.out.println("Error");
}

@Override
public void mul(){
if((super.getNum1()>0) && (super.getNum2()>0))
super.mul();
else
System.out.println("Error");
}
@Override
public void div(){
if((super.getNum1()>0) && (super.getNum2()>0))
super.div();
else
System.out.println("Error");
}
public void display(){
super.display();
}
}

public class Runner1 {


public static void main(String[] args) {
VerifiedSimple verifiedsimple = new VerifiedSimple(20.0,30.0);
verifiedsimple.add();
verifiedsimple.sub();
verifiedsimple.setNum1(10.0);
verifiedsimple.mul();
verifiedsimple.div();
verifiedsimple.display();
}

QUESTION # 2
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.

public class Document {


protected String text;

public Document() {
text = " ";
}

public Document(String Text) {


text = Text;
}

public void setText(String Text) {


text = Text;
}

public String getText() {


return text;
}

public void display() {


System.out.println("The text is: " + 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;
}
}
}

public class Email extends Document {


private String sender;
private String recipient;
private String emailTitle;

public Email(){
super();
sender = " ";
recipient = " ";
emailTitle = " ";
}
public Email(String Text, String sen, String rec, String title){
super(Text);
sender = sen;
recipient = rec;
emailTitle = title;
}

Email(String aLi_Shaikh, String haider, String congratulations) {


throw new UnsupportedOperationException("Not supported yet."); //To change body of generated
methods, choose Tools | Templates.
}
public void setsender(String sen)
{
sender = sen;
}
public String getsender()
{
return sender;
}
public void setrecipient(String rec)
{
recipient = rec;
}
public String getrecipient()
{
return recipient;
}
public void setemailtitle(String title)
{
emailTitle = title;
}
public String getemailtitle()
{
return emailTitle;
}

@Override
public String toString(){
return ("From " + sender + "\nTo " + recipient + "\nMessage is: " + super.text);
}
public void display(){
super.display();
System.out.println("Sender is: " + sender);
System.out.println("Recipient is: " + recipient);
System.out.println("Email Title is: " + emailTitle);
}

public class File extends Document{


private String pathname;
public File(){
super();
pathname = " ";
}
public File(String Text, String pn){
super(Text);
pathname = pn;
}
public void setpathname(String pn)
{
pathname = pn;
}
public String getpathname()
{
return pathname;
}
public void display()
{
super.display();
System.out.println("Pathname is: " + pathname);
}
@Override
public String toString(){
return ("Path Name is: " + pathname + "\nMessage is: " + super.text);
}

public class runner2 {


public static void main(String[] args) {
Document[] d1 = new Document[2];
d1[0] = new Email("Congrats on Winning", "Ali", "Ahmed", "Good news message");
d1[1] = new File("you won", "abc");
String keyword = "Congratulations";
for (int i = 0; i < 2; i++) {
System.out.println(d1[i].toString());

}
System.out.println(Document.ContainsKeyword(d1[0], keyword) + "\n" +
Document.ContainsKeyword(d1[1], keyword));
}

QUESTION # 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”.

public class Person {


protected String name;
protected int age;
public Person(){
name = " ";
age = 0;

}
public Person(String Name , int Age){
name = Name;
age = Age;
}
public void setName(String Name){
name = Name;
}
public String getNAme(){
return name;
}
public void setage(int Age){
age = Age;

}
public int getage(){
return age;
}
public void display(){
System.out.println("Name is: " + name);
System.out.println("Age is: " + age);
}
}
public class Vehicle {
protected String ManfacturerName;
protected int NumofCylinders;
protected Person owner;

public Vehicle(){
ManfacturerName = " ";
NumofCylinders = 0;
owner = new Person();

}
public Vehicle(String name, int noc,Person o){
ManfacturerName = name;
NumofCylinders = noc;
owner = o;
}
public void setmanufacturername(String mn)
{
ManfacturerName = mn;
}
public String getmanufacturername()
{
return ManfacturerName;
}
public void setnumberofcylinders(int noc)
{
NumofCylinders = noc;
}
public int getnumberofcylinders()
{
return NumofCylinders;
}
public void setowner(Person o)
{
owner = o;
}
public Person getowner()
{
return owner;
}

}
public class Truck extends Vehicle{
private double LoadCap;
private int towCap;

public Truck(){
super();
LoadCap = 0.0;
towCap = 0;
}
public Truck(String name, int noc,Person o, double lc, int tc){
super(name,noc,o);
LoadCap = lc;
towCap = tc;
}
public void setLoadCap(double lc){
LoadCap = lc;
}
public double getLoadCap(){
return LoadCap;
}
public void setTowCap(int tc){
towCap = tc;
}
public int getTowcap()
{
return towCap;
}
public void display(){
System.out.println("Load Capacity is: " + LoadCap);
System.out.println("Tow Capacity is: " + towCap);
System.out.println("Manufacturer's name is: " + super.getmanufacturername());
System.out.println("Number of Cylinders is: " + super.getnumberofcylinders());
System.out.println("Name of Owner of truck is: "+ super.getowner().getNAme());
System.out.println("Age of Owner of truck is: " + super.getowner().getage());
}
}

public class Runner4 {


public static void main(String[] args) {
Person person = new Person("Ali",20);
Truck truck = new Truck("HONDA",10,person,20,29);
truck.display();
}

QUESTION # 4
1. 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

Display
In the runner book two car and call display function.

public class Car {


private String model;
private String number;
private String rentalID;

public Car() {
model = " ";
number = " ";
rentalID = " ";
}

public Car(String m, String n, String id) {


model = m;
number = n;
rentalID = id;
}

public void setmodel(String m) {


model = m;
}

public String getmodel() {


return model;
}

public void setnumber(String n) {


number = n;
}

public String getnumber() {


return number;
}

public void setrentalid(String id) {


rentalID = id;
}

public String getrentalid() {


return rentalID;
}
}

public class Person {


protected String name;
protected int age;
public Person(){
name = " ";
age = 0;

}
public Person(String Name , int Age){
name = Name;
age = Age;
}
public void setName(String Name){
name = Name;
}
public String getNAme(){
return name;
}
public void setage(int Age){
age = Age;

}
public int getage(){
return age;
}
public void display(){
System.out.println("Name is: " + name);
System.out.println("Age is: " + age);
}
}

public class Customer extends Person {


private String LicenseNumber;
private String regID;
public Customer(){
super();
LicenseNumber = " ";
regID = " ";
}
public Customer(String Name , int Age, String ln, String id){
super(Name,Age);
LicenseNumber = ln;
regID = id;
}
public void setLisenceNum(String ln)
{
LicenseNumber = ln;
}
public String getLisenceNum()
{
return LicenseNumber;
}
public void setregID(String id)
{
regID = id;
}
public String getregID()
{
return regID;
}
public void display(){
System.out.println("License Number is: " + LicenseNumber);
System.out.println("Registration ID is: " + regID);
}

public class RentedCar {


private Car c;
private Customer ct;
public RentedCar()
{
c = new Car();
ct = new Customer();
}
public RentedCar(Car car,Customer customer)
{
c = car;
ct = customer;
}
public void setcar(Car car)
{
c = car;
}
public Car getcar()
{
return c;
}
public void setcustomer(Customer customer)
{
ct = customer;
}
public Customer getcustomer()
{
return ct;
}
public void display(){
System.out.println("The model of Car is: " + c.getmodel());
System.out.println("Number of car is: " + c.getnumber());
System.out.println("Rental ID os car is: " + c.getrentalid());
System.out.println("License Number of Customer is: " + ct.getLisenceNum());
System.out.println("Registration ID of Customer is: " + ct.getregID());
}

public void CheckValidityOfCustomer(){


if(ct.getage()>18){
System.out.println("Customer is valid");

}
else
System.out.println("Customer is invalid");
}

public class Runner5 {


public static void main(String[] args) {
Car car1 = new Car("2018","A1B2","112119");
Car car2 = new Car("2019", "A2B4" , " 123213");
Customer customer1 = new Customer("Ali" , 20 , "A112L91I1" , "sp18-bse-012");
Customer customer2 = new Customer("Amna" , 15 , "A191I1" , "sp18-bse-018");
RentedCar rentedcar = new RentedCar(car1 , customer1);
RentedCar rentedcar1 = new RentedCar(car2, customer2);
rentedcar.display();
rentedcar.CheckValidityOfCustomer();
rentedcar1.display();
rentedcar1.CheckValidityOfCustomer();

}
}

QUESTION # 5
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).

public abstract class Convert {


protected double val1;
protected double val2;
public Convert(){
val1 = 0.0;
val2 = 0.0;
}
public Convert(double v1, double v2){
val1 = v1;
val2 = v2;
}
public void setVAL1(double v1){
val1 = v1;

}
public double getVAL1(){
return val1;
}
public void setVAL2(double v2){
val1 = v2;

}
public double getVAL2(){
return val2;
}
public abstract double compute();

public class FaherHeitToCelsius extends Convert{


public FaherHeitToCelsius(){
super();
}
public FaherHeitToCelsius(double v1, double v2){
super(v1,v2);
}
@Override
public double compute(){
super.val2 = ((super.val1) - 32 / 1.8);
return super.val2;
}
}

public class FeetToMeters extends Convert {


public FeetToMeters(){
super();
}
public FeetToMeters(double v1, double v2){
super(v1,v2);
}
@Override
public double compute(){
super.val2 = (super.val1 * 0.3048);
return super.val2;
}

public class LitersToGallons extends Convert{


public LitersToGallons(){
super();
}
public LitersToGallons(double v1, double v2){
super(v1,v2);

}
@Override
public double compute()
{
super.val2 = super.val1 / 3.7854;
return super.val2;
}

public class Runner6 {


public static void main(String[] args) {
Convert[] convert = new Convert[3];
convert[0] = new LitersToGallons(120.0,4.0);
convert[1] = new FaherHeitToCelsius(40.0,4.0);
convert[2] = new FeetToMeters(65.0,5.0);
for(int i = 0 ; i < convert.length ; i++ ){
System.out.println(convert[i].compute());
}
}

Das könnte Ihnen auch gefallen