0% found this document useful (0 votes)
5 views9 pages

Java Polymorphism and Array Lists Guide

The document outlines lab objectives focusing on polymorphism and array lists in Java. It includes various questions and exercises related to class methods, inheritance, syntax errors, and the functionality of ArrayLists. Additionally, it provides programming tasks involving class definitions, method overrides, and output expectations for Java programs.

Uploaded by

ayaanwarabd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Java Polymorphism and Array Lists Guide

The document outlines lab objectives focusing on polymorphism and array lists in Java. It includes various questions and exercises related to class methods, inheritance, syntax errors, and the functionality of ArrayLists. Additionally, it provides programming tasks involving class definitions, method overrides, and output expectations for Java programs.

Uploaded by

ayaanwarabd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab Objectives

1- Polymorphism
2- Array Lists
Part 1: In Lab (Discuss with students)
Question 1. Does every class have a toString method and an equals method?
Where do they come from? How are they used? Is it appropriate to override these
methods?
Question 2. Which of the following methods would be legal if added (individually)
at line 4 in the following code ? (Choose all that apply.)
1. public class Test1 {
2. public float aMethod (float a, float b) { return 0;
3.}
4.
5.}
A. public int aMethod(int a, int b) { return 0;}
B. public float aMethod(float c, float d) { return 0; }
C. public int aMethod (float c, float d) { return 0; }
D. private float aMethod(int a, int b, int c) { return 0:}
Question 3. Consider the following classes:
public class Vehicle {... }
public class Car extends Vehicle {... }
public class SUV extends Car {...}
Which of the following are legal statements?
a) Vehicle v = new Car):
b) Vehicle v= new SUV0:
c) Car c = new SUV0;
d) SUV S= new Car):
e) Car c = new Vehicle):

Page l of 9
Question 4. Find the output of the follawing Java programs; if the program has no
output, write None"
Answer
class Foo {
public void method10 {
[Link] ("foo 1"); }
public void method2 (0{
[Link] ("foo 2"):
public String toString0 {return "foo":
class Bar extends Foo {
public void method2 0 {
[Link] ("bar 2"):
}
class Baz extends Foo {
public void method10 {
[Link] ("baz1"); }
public String toString) { return "baz"; }
class Mumble extends Baz {
public void method20 {
[Link]("mumble 2");

public class Test {


publicstatic void main(Stringl] arg) {
Foo[] pity = {new Baz), new Bar), new
Mumble), new Foo)}:
for (int i = 0; i< [Link]; i++){
[Link](pityli):
pity[i].method1):
pityli).method20;
[Link] 0:

Page 2 of 9
Question 5. Find the syntax errors in the following Java programs.
If the program contains syntax errors, for each error,
write the line number and indicate the error.
If the program doesn't contain syntax errors, write "None".
Question l. Answer
1. class Fruit {}
2. class Orange extends Fruit
3. {
4. public void makeOrangeJuice0{}
5. }
6. class Apple extends Fruit {o
7. class GoldenDelicious extends
Apple {}
8. public class Test
9. {
10. public static void main(Stringll args)
11.{
12. GoldenDelicious fruit = new
GoldenDelicious0;
13. Orange orange =new Orange):
14. fruit.makeOrangeJuice0:
15. [Link]);
16. }
17. }
Question 2. Answer
1 class Fruit {}
2 class Orange extends Fruit
3 {
4 public void makeOrangeJuice0{}
6 class Apple extends Fruit {
7 class GoldenDelicious extends
Apple )
public class Test

Page 3 of 9
10 public static void main(Stringll
args)
11 {
12 GoldenDelicious fruit = new
GoldenDelicious0:
13 Orange orange = new Orange):
14 Apple applel = fruit;
15 Apple apple2 = (Apple) orange:
16 }
17 }
Question 3. Answer
1 class Fruit {}
2 class Orange extends Fruit
3
4 public void makeOrangeJuice0{ }
5
6 class Apple extends Fruit {
7 class GoldenDelicious extends Apple
8 publicclass Test
10 public static void main(String|] args)
11 {
12 Fruit fruit = new Fruit);:
13 [Link]):
14 fruit.makeJuice0:
15 }
16 }
Question6. What is an ArrayList in Java, and how does it differ from arrays?

Page 4 of 9
Question 7. Write a program that has the following
" Class Circle which has the following
o A double variable for radius
o Setter and getter methods for radius
o Constructors (0-arg and 1-arg)
o Method getArea), that return the area of the circle
o Method getCir), that return the circumference of the circle
o Override the toString0 method to return the information of the circle
object
o Override the equals) method to compare two circles
Create Test class that has the following method
o Main method
Declare array list of circles
Read unspecific number of circle'sradius, then insert them to
the created array list
o A method display 0 that print all circles data
o A method getSumAreas) that return the average of all circles areas
that stored in Array list
The output like

Enter radius (0 or Negative value: to exit):5


Enter radius (0 or Negative value: to exit):25
Enter radius (0 or Negative value: to exit):14
Enter radius (0 or Negative value: to exit):0
Circles Information:
Radius = 5.0 Area=78.53981633974483 Circumference= 31.41592653589793
Radius = 25.0 Area=1963.4954084936207 Circumference= 157.07963267948966
Radius = 14.0 Area=615.7521601035994 Circumference= 87.96459430051421
Sum of circles areas = 2657.7873849369653

Page 5 of 9
Part 2: Students IN LAB.
Question 1. Find the output of the following Java programs; if the program has no
output, write None"
|Answer
class Fruit {
public void freeze ) {
[Link]("Freezing Fruit");
public void makeJuice) {
[Link]("Fruit Juice"):
public void eat) {
[Link] ("EatingFruit"):

class Apple extends Fruit {


public void makeJuice0 {
[Link]("Apple Juice"):
public void eat) {
[Link]("Eating Apple"):

class Macintosh extends Apple {


public void eat) {
[Link]("EatingMacintosh"):

public class Test {


public static void main (Stringl] args) {
Fruit mac = new Macintosh 0:
mac.eat0;
[Link]):
[Link]);
}}

Page 6 of9
Part 3: HOMEWORK.

Question 1. Suppose that Fruit, Apple, Orange, GoldenDelicious, and


Macintosh are declared, as shown
Assume that the following declaration is given:
Fruit fruit = new GoldenDelicious0:
Orange orange = new Orange):
Answer the following questions:
(1) Is fruit instanceof Fruit?
(2) Is fruit instanceof Orange?
(3) Is fruit instanceof Apple?
(4) Is fruit instanceof GoldenDelicious?
(5) Is fruit instanceof Macintosh?
(6) Is orange instanceof Orange?
(7) Is orange instanceof Fruit?
(8) Is orange instanceof Apple?
(9) Suppose the method makeAppleCider is defined in the Apple class. Can
fruit invoke this method? Can orange invoke this method?
(10) Suppose the method makeOrange Juice is defined in the Orange class.
Can orange invoke this method? Can fruit invoke this method?
(11) Is the statement Orange p = new Apple0legal?
(12) Is the statement Macintosh p = new Apple) legal?
(13) Is the statement Apple p = new Macintosh) legal?

Page 7 of 9
Question 2. What will be the exact output when the following Java code is executed?

I/ Base class (parent class)


class Shape {
public void draw) {
[Link]("Drawing a generic shape.");:

I/Subclass 1 (child class)


class Circle extends Shape {
@Override
public void draw) {
[Link]("Drawing a circle.");
}

I/ Subclass 2 (child class)


class Rectangle extends Shape {
@Override
public void draw) {
[Link]("Drawing a rectangle.");
}

I|Subclass 3 (child class)


class Triangle extends Shape {
@Override
public void draw0 {
[Link]("Drawing a triangle. "):
}
public class PolymorphismQuestion {
public static void main(Stringl] args) {
Shape[] shapes = new Shape[3):
shapes[0]= new Circle):
Page &of 9
shapes[1] = new Rectangle );:
shapes[2] = new Triangle):
for (Shape shape : shapes) {
shape.draw0:
}

Page 9 of 9

You might also like