Beruflich Dokumente
Kultur Dokumente
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.
public Simple() {
num1 = 0.0;
num2 = 0.0;
}
@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();
}
}
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.
public Document() {
text = " ";
}
public Email(){
super();
sender = " ";
recipient = " ";
emailTitle = " ";
}
public Email(String Text, String sen, String rec, String title){
super(Text);
sender = sen;
recipient = rec;
emailTitle = title;
}
@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);
}
}
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 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());
}
}
QUESTION # 4
1. 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
Display
In the runner book two car and call display function.
public Car() {
model = " ";
number = " ";
rentalID = " ";
}
}
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);
}
}
}
else
System.out.println("Customer is invalid");
}
}
}
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 double getVAL1(){
return val1;
}
public void setVAL2(double v2){
val1 = v2;
}
public double getVAL2(){
return val2;
}
public abstract double compute();
}
@Override
public double compute()
{
super.val2 = super.val1 / 3.7854;
return super.val2;
}