help please?

help please?

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.

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.

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.

View Answers

January 8, 2011 at 11:07 AM

Hi Friend,

Try the following codes: 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);
    }
}

2)

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);
    }

}

January 8, 2011 at 11:07 AM

continue..

3)

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









Related Tutorials/Questions & Answers:
help
help
Advertisements
help
HELP
Help...
Help
Help!
help
Help
help
help!!!!!!!!!!!!!!!!!!
Regular Expression Help Help HELP!!!!
Help me
help me
help me
Help Me
help
help
help
help
Help
Help
help
help
help
help
Help
Help
Timeseries help
help me
help me
help me
help me
help me..
Help me
Help in Struts2
help pleas? -_-
help me
help us
java help!
Help with this program
Help With Java...
Plz Help
help me...
help me..
help me
Help with this program
help me...
help in java
help in java

Ads