0% found this document useful (0 votes)
10 views

Test2 2022

Uploaded by

Mazwe Hlafuna
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)
10 views

Test2 2022

Uploaded by

Mazwe Hlafuna
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
You are on page 1/ 6

INTERNAL TEST 2

SEMESTER 2, 2022 DATE: 14/10/2022


TIME: 110 min
SUBJECT: PROGRAMMING FOR ENGINEERS
TOTAL MARKS: 100
EXAMINER: PROF. PY TABAKOV FULL MARKS: 100
MODERATOR: PROF. HC MYBURGH

Design a Java application that uses method overriding. Create a superclass called Circle.

This class defines methods called getRadius(), getColor() and getArea(). It also contains
two private instance variables: radius (of type double) and color (of type String), with
default values 1.0 and “red”, respectively. The superclass must have two overloaded
constructors: 1) constructor which sets both radius and color to default; 2) constructor
with given radius and color.

Create classes for different geometrical shapes called Cylinder, Cone and Torus. These
classes must contain two constructors each, – one with default values and another one
with given values. Use the following inheritance structure:

public class Circle


getRadius();
getColor();
getArea();

public class Cylinder


getHeight();
getVolume();
public class Torus
getMainRadius();
getVolume();
getSurfaceArea();
public class Cone
getVolume();

Create the driver class containing the main() method and call it TestShapes. The
application must produce an output similar to the given on the next page (calculations
done with default and given values, respectively).

i
Circle: radius = 1.00000; colour = red; area = 3.14159
Circle: radius = 12.0000; color = blue; area = 452.389;

Cylinder: radius = 1.00000; colour = red; height = 1.00000; volume = 3.14159


Cylinder: radius = 8.00000; colour = yellow; height = 9.00000; volume = 1809.56

Cone: radius = 1.00000; colour = red; height = 1.00000; volume = 1.04720


Cone: radius = 10.0000; colour = magenta; height = 7.00000; volume = 733.038

Torus: radius = 1.00000; colour = red; main radius = 1.00000; volume = 19.7392;
surface area = 39.4784
Torus: radius = 10.0000; colour = orange; main radius = 20.0000;
volume = 39478.4; surface area = 7895.68

ii
Model answer

Super class Circle:


1 public class Circle {
2

3 private double radius ;


4 private String color ;
5

6 // first constructor
7 public Circle () {
8 radius = 1.0;
9 color = " red " ;
10 }
11

12 // second constructor
13 public Circle ( double r , String c ) {
14 radius = r ;
15 color = c ;
16 }
17

18 public double getRadius () {


19 return radius ;
20 }
21

22 public String getColor ( ) {


23 return color ;
24 }
25

26 public double getArea () {


27 return Math . PI * radius * radius ;
28 } }

Subclass Cylinder:
1

2 public class Cylinder extends Circle {


3

4 private double height ;


5

6 // constructor with default values but given height


7 public Cylinder () {
8 super () ;
9 height = 1.0;
10 }
11 // constructor with given values
12 public Cylinder ( double radius , String color , double h ) {
13 super ( radius , color ) ;
14 height = h ;
15 }

iii
16

17 // retrieve height
18 public double getHeight () {
19 return height ;
20 }
21 // calculate the volume
22 public double getVolume () {
23 return getArea () * height ;
24 } }

Subclass Cone:
1

2 public class Cone extends Cylinder {


3

4 // constructor with default values


5 public Cone () {
6 super () ;
7 }
8 // constructor with given values
9 public Cone ( double radius , String color , double hight ) {
10 super ( radius , color , hight ) ;
11 }
12

13 // calculate the volume


14 public double getVolume () {
15 return getArea () * getHeight () /3.0;
16 } }

Subclass Torus:
1 public class Torus extends Circle {
2 private double torusMainRadius ;
3 // constructor with default values but given height
4 public Torus () {
5 super () ;
6 torusMainRadius = 1.0;
7 }
8 // constructor with given values
9 public Torus ( double radius , String color , double mr ) {
10 super ( radius , color ) ;
11 torusMainRadius = mr ;
12 }
13

14 // retrieve main radius


15 public double getMainRadius () {
16 return torusMainRadius ;
17 }
18 // calculate the volume
19 public double getVolume () {

iv
20 return getArea () * 2.0 * Math . PI * getMainRadius () ;
21 }
22 public double getSurfaceArea () {
23 return 4.0 * Math . PI * Math . PI * getRadius () * getMainRadius () ;
24 } }

Driver class TestShapes:


1 import java . util . Locale ;
2

3 public class TestShapes {


4

5 public static void main ( String [] args ) {


6 Locale . setDefault ( Locale . US ) ;
7

8 Circle c1 = new Circle () ;


9 System . out . printf ( " Circle : radius = % g ; colour = % s ;
10 area = % g \ n " , c1 . getRadius () , c1 . getColor () , c1 . getArea () ) ;
11

12 Circle c2 = new Circle (12.0 , " blue " ) ;


13 System . out . printf ( " Circle : radius = % g ; color = % s ;
14 area = % g ;\ n \ n " , c2 . getRadius () , c2 . getColor () ,
15 c2 . getArea () ) ;
16

17 Cylinder cl1 = new Cylinder () ;


18 System . out . printf ( " Cylinder : radius = % g ; colour = % s ;
19 height = % g ; volume = % g \ n " ,
20 cl1 . getRadius () , cl1 . getColor () , cl1 . getHeight () ,
21 cl1 . getVolume () ) ;
22

23 Cylinder cl2 = new Cylinder (8.0 , " yellow " , 9.0) ;


24 System . out . printf ( " Cylinder : radius = % g ; colour = % s ;
25 height = % g ; volume = % g \ n \ n " , cl2 . getRadius () ,
26 cl2 . getColor () , cl2 . getHeight () ,
27 cl2 . getVolume () ) ;
28

29 Cone cn1 = new Cone () ;


30 System . out . printf ( " Cone : radius = % g ; colour = % s ; height = % g ;
31 volume = % g \ n " , cn1 . getRadius () , cn1 . getColor () ,
32 cn1 . getHeight () , cn1 . getVolume () ) ;
33

34 Cone cn2 = new Cone (10.0 , " magenta " , 7.0) ;


35 System . out . printf ( " Cone : radius = % g ; colour = % s ; height = % g ;
volume = % g \ n \ n " , cn2 . getRadius () ,
36 cn2 . getColor () , cn2 . getHeight () , cn2 . getVolume () ) ;
37

38 Torus t1 = new Torus () ;


39 System . out . printf ( " Torus : radius = % g ; colour = % s ;
40 main radius = % g ; volume = % g ; surface area = % g \ n " ,
41 t1 . getRadius () , t1 . getColor () , t1 . getMainRadius () ,

v
42 t1 . getVolume () , t1 . getSurfaceArea () ) ;
43

44 Torus t2 = new Torus (10.0 , " orange " , 20.0) ;


45 System . out . printf ( " Torus : radius = % g ; colour = % s ;
46 main radius = % g ; volume = % g ; surface area = % g \ n " ,
47 t2 . getRadius () , t2 . getColor () , t2 . getMainRadius () ,
48 t2 . getVolume () , t2 . getSurfaceArea () ) ;
49 } }

Output:

Circle: radius = 1.00000; colour = red; area = 3.14159


Circle: radius = 12.0000; color = blue; area = 452.389;

Cylinder: radius = 1.00000; colour = red; height = 1.00000; volume = 3.14159


Cylinder: radius = 8.00000; colour = yellow; height = 9.00000; volume = 1809.56

Cone: radius = 1.00000; colour = red; height = 1.00000; volume = 1.04720


Cone: radius = 10.0000; colour = magenta; height = 7.00000; volume = 733.038

Torus: radius = 1.00000; colour = red; main radius = 1.00000; volume = 19.7392;
surface area = 39.4784
Torus: radius = 10.0000; colour = orange; main radius = 20.0000;
volume = 39478.4; surface area = 7895.68

vi

You might also like