Question 1
Define a class named Circle with the following properties:
â?¢ An integer data field named radius with protected access modifier, and a String data field named colour with private access modifier. Both data fields specify the radius and the colour of the Circle object respectively. Set the default values of radius to 1 and the colour as â??redâ??. â?¢ A private static constant named PI with a fixed value of 3.142 that represents the value of pi. â?¢ A default constructor. â?¢ A constructor that creates Circle objects with the given radius value as the argument. â?¢ A public method to display the Circle objectâ??s data fields. â?¢ A public method named changeColour(String) to change the value of the Circle objectâ??s colour based on the value in the method argument. â?¢ A public method named getSurfaceArea() that calculates and returns the surface area of the Circle object. Use the constant PI value for the calculation. â?¢ A public method named getCircumference() that calculates and returns the Circle objectâ??s circumference. Use the constant PI value for the calculation.
Write another class named TestCircle to test the Circle program by creating two Circle objects. The first object must be created using the default constructor, while the second object must be created with the radius value of 8. Display the properties of both objects and also their surface area and circumference. (15 marks)
Question 2:
Define a class named Sphere that inherits the properties of the class Circle in Question 1. The class also contains:
â?¢ A private data field named weight of type double with the default value 1.0. â?¢ A default Sphere constructor. â?¢ A constructor that initializes the Sphere objectâ??s radius and weight through the constructor arguments. â?¢ A public method to display the Sphere objectâ??s data. â?¢ A public method named getSurfaceArea() that calculates and returns the surface area of the Sphere object. â?¢ A public method named getVolume() that calculates and returns the Sphere objectâ??s volume.
Write another class to test Circle and Sphere class. The class will create a Circle object and a Sphere object based on the given input from the user. Use the proper JOptionPane method to get the input from the user. Display the properties of both created objects. Display surface area and circumference of the Circle object and the surface are and volume of the Sphere object. (15 marks)
Question 3:
Write a Java program that will declare an array of THREE (3) Circle objects based on the Circle class definition in Question 1. Using for or while loop and the suitable method from the JOptionPane class, get the input from user to create TWO (2) Circle objects and reference the objects to the first two elements of the array. Finally, use another loop to display the data of all the objects in the array. (10 marks)
Question 4:
i. Name the keyword that set an identifier as a constant. (1 mark)
ii. Identify and list all the instance variables and the class variable of the object Circle. (3 marks)
iii. List all the instance variables of the object Sphere. (2 marks)
iv. Name the data field in class Circle that is not inherited by class Sphere. Provide the reason why the property is not inherited? (2 marks)
v. What is the value of the third element in the array in Question 3? Provide the explanation for your answer. (2 marks)
Hi Friend,
Try the following code:
1)
class Circle{ protected double radius=1; private String color="red"; private static double pi=3.142; Circle(){} Circle(double radius){ this.radius=radius; } public void display(){ System.out.println("Radius:"+radius+"\nColor:"+color); } public void changeColor(String col) { color = col; } public double getSurfaceArea(){ double area=pi*radius*radius; return area; } public double getCircumference(){ double circumference=2*pi*radius; return circumference; } } public class TestCircle{ public static void main(String[] args){ Circle c1=new Circle(); c1.display(); double sa1=c1.getSurfaceArea(); System.out.println("Surface Area1: "+sa1); double cf1=c1.getCircumference(); System.out.println("Circumference1: "+cf1); System.out.println(); Circle c2=new Circle(8); c2.changeColor("Pink"); c2.display(); double sa2=c2.getSurfaceArea(); System.out.println("Surface Area2: "+sa2); double cf2=c2.getCircumference(); System.out.println("Circumference2: "+cf2); } }
Thanks
Hi Friend,
2)Try the following code:
import javax.swing.*; class Circle{ protected double radius=1; String color="red"; private static double pi=3.142; Circle(){} Circle(double radius){ this.radius=radius; } public void display(){ System.out.println("Radius:"+radius+"\nColor:"+color); } public void changeColor(String col) { color = col; } public double getSurfaceArea(){ double area=pi*radius*radius; return area; } public double getCircumference(){ double circumference=2*pi*radius; return circumference; } } class Sphere extends Circle{ private double weight=1; private static double pi=3.142; Sphere(){} Sphere(double r,double weight){ super.radius = r; this.weight=weight; } public void changeColor(String col) { super.color = col; } public void display(){ System.out.println("Radius:"+super.radius+"\nWeight:"+weight+"\nColor:"+super.color); } public double getSurfaceArea(){ double radius=super.radius; double area=4*pi*radius*radius*radius; return area/3; } public double getVolume(){ double vol=4*pi*radius*radius; return vol; } } public class TestBoth { public static void main(String[]args){ String st1=JOptionPane.showInputDialog(null,"Enter radius"); double r1=Double.parseDouble(st1); Circle c=new Circle(r1); c.changeColor("Pink"); c.display(); double saOFCircle=c.getSurfaceArea(); System.out.println("Surface Area Of Circle: "+saOFCircle); double cf=c.getCircumference(); System.out.println("Circumference: "+cf); System.out.println(); String st2=JOptionPane.showInputDialog(null,"Enter radius"); double r2=Double.parseDouble(st2); Sphere s=new Sphere(r2,5); c.changeColor("Blue"); s.display(); double saOFSphere=s.getSurfaceArea(); System.out.println("Surface Area Of Sphere: "+saOFSphere); double voloume=s.getCircumference(); System.out.println("Volume: "+voloume); } }
Thanks
Hi Friend,
3)Try the following code:
import javax.swing.*; class Circle{ protected double radius=1; private String color="red"; private static double pi=3.142; Circle(){} Circle(double radius){ this.radius=radius; } public void display(){ System.out.println("Radius:"+radius+"\nColor:"+color); } public void changeColor(String col) { color = col; } public double getSurfaceArea(){ double area=pi*radius*radius; return area; } public double getCircumference(){ double circumference=2*pi*radius; return circumference; } } public class ArrayOFCircle{ public static void main(String[] args){ Circle c[]=new Circle[3]; c[0]=new Circle(); for(int i=1;i<c.length;i++){ double r=Double.parseDouble(JOptionPane.showInputDialog(null,"Enter radius")); c[i]=new Circle(r); } for(int i=0;i<c.length;i++){ int j=i+1; c[i].changeColor("Pink"); c[i].display(); double sa=c[i].getSurfaceArea(); System.out.println("Surface Area "+j+": "+sa); double cf1=c[i].getCircumference(); System.out.println("Circumference "+j+": "+cf1); } } }
Thanks
Ads