SlideShare a Scribd company logo
Note: Submit by 4-5-2015
UTA005 - Assignment 2
1 Consider the following two classes:
public class ClassA {
public void methodOne(int i) {
}
public void methodTwo(int i) {
}
public static void methodThree(int i) {
}
public static void methodFour(int i) {
}
}
public class ClassB extends ClassA {
public static void methodOne(int i) {
}
public void methodTwo(int i) {
}
public void methodThree(int i) {
}
public static void methodFour(int i) {
}
}
a. Which method overrides a method in the superclass?
b. Which method hides a method in the superclass?
c. What do the other methods do?
2 Create a class named Square that contains data fields for height, weight, and surfaceArea,
and a method named computeSurfaceArea(). Create a child class named Cube. Cube
contains an additional data field named depth, and a computeSurfaceArea() method that
overrides the parent method. Write a program called DemoSquare that instantiates a
Square object and a Cube object and displays the surface areas of the objects.
3 Write a program called CarRental that computes the cost of renting a car for a day based
on the size of the car: economy, medium, or full-size. Include a constructor that requires
the car size. Add a subclass called CarPhone to add the option of a car phone. Write a
program called UseCarRentalAndPhone to use these classes.
4 Consider the following program:
class Horse {
public void Print() {
System.out.println("Horse");
}
}
class RaceHorse extends Horse {
public void Print() {
System.out.println("RaceHorse");
}
}
class Test {
public static void DoPrint( Horse h ) {
h.Print();
}
public static void main(String[] args) {
Horse x = new Horse();
Horse y = new RaceHorse();
Racehorse z = new RaceHorse()
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}
What is printed when this program is executed?
Consider the following program:
interface Employee {
public void RaiseSalary();
}
interface Musician {
public Play();
}
class Test implements Employee, Musician {
public void RaiseSalary() {
System.out.println("raising");
}
public void Play() {
System.out.println("playing");
}
public static void main(String[] args) {
Test x = new Test();
x.RaiseSalary();
x.Play();
}
}
True or false:
1. An attempt to compile this program fails because class Test tries to implement two
interfaces at once ______.
2. An attempt to compile this program fails because class Test only implements
interfaces, it does not extend any class ______.
3. An attempt to compile this program fails because the definitions of the two
interfaces and the class are all in the same file ______.
4. It is optional for class Test to implement the methods RaiseSalary and Play ______.
5 . Consider the following classes B and D:
class B {
public B() { System.out.println("B()"); }
public B(int n) { System.out.println("B(" + n + ")"); }
}
class D extends B {
private B b;
public D() { System.out.println("D()"); }
public D(int n) {
super(n); // this gets executed first [2] and note
// that the corresponding constructor from the superclass
// will be called and B(3) is printed since n == 3
b = new B(-n); // this comes next [3]
// when a new object is created the same constructor will
// be invoked, albeit for a different object, and B(-3)
// will be printed since n == 3 and -n == -3
System.out.println("D(" + n + ")"); // [3] and
// finally this line prints D(3) and the original
// invocation is finished and our simple program ends
}
}
What does the following program print and why?
public static void main(String[] args) {
D d = new D(3); // it starts here [1]
}
Trace the program by hand to explain the mechanism and its output.
6 A super class Record has been defined to store the names and ranks of 50 students. Define
a sub class Rank to find the highest rank along with the name. The details of both classes
are given below:
Class name : Record
Data Members / instance variables:
name[ ] : to store the names of students
rnk[ ] : to store the ranks of students
Member functions:
Record() : constructor to initialize data members
void readvalues() : to store names and ranks
void display() : displays the names and the corresponding ranks
Class name : Rank
Data Members / instance variables:
index : integer to store the index of the topmost rank
Member functions
Rank() : constructor to invoke the base class constructor and to initialize index to 0.
void highest() : finds the index location of the topmost rank and stores it in index without
sorting the array 6
void display() : displays the name and ranks along with the name having the topmost rank.
Specify the class Record giving details of the constructor(), void readvalues(), void
display(). Using the concept of inheritance, specify the class Rank giving details of
constructor(), void highest() and void display().
[Note: Write the main() function also in this program so as to familiarize the students on
how to run programs based on the concept of inheritance.]

More Related Content

PDF
Xamarin: Namespace and Classes
Eng Teong Cheah
 
PPT
9781439035665 ppt ch08
Terry Yoast
 
PPTX
Method overloading
Lovely Professional University
 
DOCX
Comp 328 final guide
krtioplal
 
PPTX
Lecture 4. mte 407
rumanatasnim415
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPTX
Functions, classes & objects in c++
ThamizhselviKrishnam
 
PPTX
Overloading
Jaya Kumari
 
Xamarin: Namespace and Classes
Eng Teong Cheah
 
9781439035665 ppt ch08
Terry Yoast
 
Method overloading
Lovely Professional University
 
Comp 328 final guide
krtioplal
 
Lecture 4. mte 407
rumanatasnim415
 
classes and objects in C++
HalaiHansaika
 
Functions, classes & objects in c++
ThamizhselviKrishnam
 
Overloading
Jaya Kumari
 

What's hot (19)

PPT
6 Inheritance
Praveen M Jigajinni
 
PPTX
Presentation 1st
Connex
 
PPTX
Classes and Objects in C#
Adeel Rasheed
 
PPTX
C# Types of classes
Prem Kumar Badri
 
PPT
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
PDF
Intake 38 data access 3
Mahmoud Ouf
 
PPTX
oop lecture 3
Atif Khan
 
PPT
vb.net Constructor and destructor
suraj pandey
 
PPTX
OBJECT ORIENTED PROGRAMING IN C++
Dev Chauhan
 
DOCX
Lecture22.23.07.2014
vishal choudhary
 
PPTX
Classes and objects
Shailendra Veeru
 
PPTX
Learn Concept of Class and Object in C# Part 3
C# Learning Classes
 
PPT
4 Classes & Objects
Praveen M Jigajinni
 
PPTX
Constructors and destructors
Vineeta Garg
 
PPTX
C++ And Object in lecture3
UniSoftCorner Pvt Ltd India.
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
DOCX
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
PDF
Object Oriented Programming with C#
SyedUmairAli9
 
6 Inheritance
Praveen M Jigajinni
 
Presentation 1st
Connex
 
Classes and Objects in C#
Adeel Rasheed
 
C# Types of classes
Prem Kumar Badri
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Intake 38 data access 3
Mahmoud Ouf
 
oop lecture 3
Atif Khan
 
vb.net Constructor and destructor
suraj pandey
 
OBJECT ORIENTED PROGRAMING IN C++
Dev Chauhan
 
Lecture22.23.07.2014
vishal choudhary
 
Classes and objects
Shailendra Veeru
 
Learn Concept of Class and Object in C# Part 3
C# Learning Classes
 
4 Classes & Objects
Praveen M Jigajinni
 
Constructors and destructors
Vineeta Garg
 
C++ And Object in lecture3
UniSoftCorner Pvt Ltd India.
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
Object Oriented Programming with C#
SyedUmairAli9
 
Ad

Similar to Uta005 (20)

PPTX
Inheritance & interface ppt Inheritance
narikamalliy
 
PPTX
‫Chapter3 inheritance
Mahmoud Alfarra
 
PPTX
Inheritance
Mavoori Soshmitha
 
PPT
Java Inheritance
Kapish Joshi
 
PPTX
classes object fgfhdfgfdgfgfgfgfdoop.pptx
arjun431527
 
PDF
C sharp chap5
Mukesh Tekwani
 
PDF
OOPs & Inheritance Notes
Shalabh Chaudhary
 
PPT
java tutorial 3
Tushar Desarda
 
PPT
inheritance of java...basics of java in ppt
ssuserec53e73
 
DOCX
Indicate whether each of the following statements is true or false.docx
migdalialyle
 
PPTX
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
 
PDF
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
 
PDF
Advanced java jee material by KV Rao sir
AVINASH KUMAR
 
PPTX
abstract,final,interface (1).pptx upload
dashpayal697
 
PDF
Starting Out with Java From Control Structures through Data Structures 3rd Ed...
anpebinthi21
 
PPTX
Chap-3 Inheritance.pptx
chetanpatilcp783
 
PDF
Advance java kvr -satya
Satya Johnny
 
PDF
Adv kvr -satya
Jyothsna Sree
 
DOCX
Java execise
Keneth miles
 
PDF
Class and object in C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
Inheritance & interface ppt Inheritance
narikamalliy
 
‫Chapter3 inheritance
Mahmoud Alfarra
 
Inheritance
Mavoori Soshmitha
 
Java Inheritance
Kapish Joshi
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
arjun431527
 
C sharp chap5
Mukesh Tekwani
 
OOPs & Inheritance Notes
Shalabh Chaudhary
 
java tutorial 3
Tushar Desarda
 
inheritance of java...basics of java in ppt
ssuserec53e73
 
Indicate whether each of the following statements is true or false.docx
migdalialyle
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
 
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
 
Advanced java jee material by KV Rao sir
AVINASH KUMAR
 
abstract,final,interface (1).pptx upload
dashpayal697
 
Starting Out with Java From Control Structures through Data Structures 3rd Ed...
anpebinthi21
 
Chap-3 Inheritance.pptx
chetanpatilcp783
 
Advance java kvr -satya
Satya Johnny
 
Adv kvr -satya
Jyothsna Sree
 
Java execise
Keneth miles
 
Class and object in C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
Ad

More from Daman Toor (15)

DOCX
Lab exam 5_5_15
Daman Toor
 
DOCX
Lab exam setb_5_5_2015
Daman Toor
 
PPT
String slide
Daman Toor
 
PPTX
Nested class
Daman Toor
 
PPTX
Inheritance1
Daman Toor
 
PPTX
Inheritance
Daman Toor
 
DOC
Lab exp (creating classes and objects)
Daman Toor
 
DOCX
Practice
Daman Toor
 
DOCX
Parking ticket simulator program
Daman Toor
 
DOC
Lab exp (declaring classes)
Daman Toor
 
DOC
Lab exp declaring arrays)
Daman Toor
 
DOCX
Java assignment 1
Daman Toor
 
PPT
Operators
Daman Toor
 
PPTX
Identifiers, keywords and types
Daman Toor
 
PPTX
Classes & object
Daman Toor
 
Lab exam 5_5_15
Daman Toor
 
Lab exam setb_5_5_2015
Daman Toor
 
String slide
Daman Toor
 
Nested class
Daman Toor
 
Inheritance1
Daman Toor
 
Inheritance
Daman Toor
 
Lab exp (creating classes and objects)
Daman Toor
 
Practice
Daman Toor
 
Parking ticket simulator program
Daman Toor
 
Lab exp (declaring classes)
Daman Toor
 
Lab exp declaring arrays)
Daman Toor
 
Java assignment 1
Daman Toor
 
Operators
Daman Toor
 
Identifiers, keywords and types
Daman Toor
 
Classes & object
Daman Toor
 

Recently uploaded (20)

PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Basics and rules of probability with real-life uses
ravatkaran694
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 

Uta005

  • 1. Note: Submit by 4-5-2015 UTA005 - Assignment 2 1 Consider the following two classes: public class ClassA { public void methodOne(int i) { } public void methodTwo(int i) { } public static void methodThree(int i) { } public static void methodFour(int i) { } } public class ClassB extends ClassA { public static void methodOne(int i) { } public void methodTwo(int i) { } public void methodThree(int i) { } public static void methodFour(int i) { } } a. Which method overrides a method in the superclass? b. Which method hides a method in the superclass? c. What do the other methods do? 2 Create a class named Square that contains data fields for height, weight, and surfaceArea, and a method named computeSurfaceArea(). Create a child class named Cube. Cube contains an additional data field named depth, and a computeSurfaceArea() method that overrides the parent method. Write a program called DemoSquare that instantiates a Square object and a Cube object and displays the surface areas of the objects. 3 Write a program called CarRental that computes the cost of renting a car for a day based on the size of the car: economy, medium, or full-size. Include a constructor that requires the car size. Add a subclass called CarPhone to add the option of a car phone. Write a program called UseCarRentalAndPhone to use these classes. 4 Consider the following program: class Horse { public void Print() { System.out.println("Horse"); } } class RaceHorse extends Horse { public void Print() { System.out.println("RaceHorse"); } }
  • 2. class Test { public static void DoPrint( Horse h ) { h.Print(); } public static void main(String[] args) { Horse x = new Horse(); Horse y = new RaceHorse(); Racehorse z = new RaceHorse() DoPrint(x); DoPrint(y); DoPrint(z); } } What is printed when this program is executed? Consider the following program: interface Employee { public void RaiseSalary(); } interface Musician { public Play(); } class Test implements Employee, Musician { public void RaiseSalary() { System.out.println("raising"); } public void Play() { System.out.println("playing"); } public static void main(String[] args) { Test x = new Test(); x.RaiseSalary(); x.Play(); } } True or false: 1. An attempt to compile this program fails because class Test tries to implement two interfaces at once ______. 2. An attempt to compile this program fails because class Test only implements interfaces, it does not extend any class ______. 3. An attempt to compile this program fails because the definitions of the two interfaces and the class are all in the same file ______. 4. It is optional for class Test to implement the methods RaiseSalary and Play ______.
  • 3. 5 . Consider the following classes B and D: class B { public B() { System.out.println("B()"); } public B(int n) { System.out.println("B(" + n + ")"); } } class D extends B { private B b; public D() { System.out.println("D()"); } public D(int n) { super(n); // this gets executed first [2] and note // that the corresponding constructor from the superclass // will be called and B(3) is printed since n == 3 b = new B(-n); // this comes next [3] // when a new object is created the same constructor will // be invoked, albeit for a different object, and B(-3) // will be printed since n == 3 and -n == -3 System.out.println("D(" + n + ")"); // [3] and // finally this line prints D(3) and the original // invocation is finished and our simple program ends } } What does the following program print and why? public static void main(String[] args) { D d = new D(3); // it starts here [1] } Trace the program by hand to explain the mechanism and its output. 6 A super class Record has been defined to store the names and ranks of 50 students. Define a sub class Rank to find the highest rank along with the name. The details of both classes are given below: Class name : Record Data Members / instance variables: name[ ] : to store the names of students rnk[ ] : to store the ranks of students Member functions: Record() : constructor to initialize data members void readvalues() : to store names and ranks void display() : displays the names and the corresponding ranks Class name : Rank Data Members / instance variables: index : integer to store the index of the topmost rank Member functions
  • 4. Rank() : constructor to invoke the base class constructor and to initialize index to 0. void highest() : finds the index location of the topmost rank and stores it in index without sorting the array 6 void display() : displays the name and ranks along with the name having the topmost rank. Specify the class Record giving details of the constructor(), void readvalues(), void display(). Using the concept of inheritance, specify the class Rank giving details of constructor(), void highest() and void display(). [Note: Write the main() function also in this program so as to familiarize the students on how to run programs based on the concept of inheritance.]