assignment

Ads
 

assignment

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)

View Answers

January 7, 2011 at 11:53 AM

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


January 7, 2011 at 1:12 PM

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


January 7, 2011 at 1:30 PM

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









Related Tutorials/Questions & Answers:
Assignment
Assignment  any one can help with a java assignment
Assignment problem
Assignment problem  hello, I am designing a program partaining job assingment using hungarian algorithm,but i am totally confused, please help me out
Advertisements
my assignment
...this is my assignment....plz
Programming Assignment
Programming Assignment  Hey there, We were given an assignment to write a program to read 10 student marks from the keyboard without using an Array (only using switch statements, if statements or loops). As each mark is input
assignment
Assignment
assignment
assignment
ModuleNotFoundError: No module named 'assignment'
ModuleNotFoundError: No module named 'assignment'  Hi, My Python... 'assignment' How to remove the ModuleNotFoundError: No module named 'assignment' error? Thanks   Hi, In your python environment you
Java with OOP assignment (Eclipse)
Java with OOP assignment (Eclipse)  How do i control the length of characters in an input? E.g.: Enter your name > Hi * Name too short
java assignment - Java Beginners
java assignment  I got assignment in "design a vehicle class hierarchy in java,write a test program to demonstrate polymorphism".please send me the necessary codings and algorithm.  Hi Friend, Try the following code
Assignment Question on java programming
Assignment Question on java programming  Can somone help me on this . Thx You are required to write a complete Java program for a simple grading system. At the beginning of the system, you need to input number of students
Urgent programming assignment
Urgent programming assignment   Hi, I am an Indian student in USA.I have been able to write all the java codes successfully , however this one is giving me tremors.Any help would be highly appreciated.I am attaching
Assignment operator in java
Assignment operator in java Assignment operator is very common to almost all... the value to the variable lying on the left side. Java assignment operator... to be assigned to the variable to the left side. Assignment operator is also used
ModuleNotFoundError: No module named 'acs_examine_student_assignment'
ModuleNotFoundError: No module named 'acs_examine_student_assignment' ...: No module named 'acs_examine_student_assignment' How to remove the ModuleNotFoundError: No module named 'acs_examine_student_assignment' error
ModuleNotFoundError: No module named 'distributions-udacity-assignment'
ModuleNotFoundError: No module named 'distributions-udacity-assignment' ...: ModuleNotFoundError: No module named 'distributions-udacity-assignment' How to remove the ModuleNotFoundError: No module named 'distributions-udacity-assignment'
ModuleNotFoundError: No module named 'distributions-udacity-assignment'
ModuleNotFoundError: No module named 'distributions-udacity-assignment' ...: ModuleNotFoundError: No module named 'distributions-udacity-assignment' How to remove the ModuleNotFoundError: No module named 'distributions-udacity-assignment'
ModuleNotFoundError: No module named 'django-assignment-desk'
ModuleNotFoundError: No module named 'django-assignment-desk'  Hi...: No module named 'django-assignment-desk' How to remove the ModuleNotFoundError: No module named 'django-assignment-desk' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-assignment-desk'
ModuleNotFoundError: No module named 'django-assignment-desk'  Hi...: No module named 'django-assignment-desk' How to remove the ModuleNotFoundError: No module named 'django-assignment-desk' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-assignment-desk'
ModuleNotFoundError: No module named 'django-assignment-desk'  Hi...: No module named 'django-assignment-desk' How to remove the ModuleNotFoundError: No module named 'django-assignment-desk' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-assignment-desk'
ModuleNotFoundError: No module named 'django-assignment-desk'  Hi...: No module named 'django-assignment-desk' How to remove the ModuleNotFoundError: No module named 'django-assignment-desk' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-assignment-desk'
ModuleNotFoundError: No module named 'django-assignment-desk'  Hi...: No module named 'django-assignment-desk' How to remove the ModuleNotFoundError: No module named 'django-assignment-desk' error? Thanks   Hi
ModuleNotFoundError: No module named 'keystone-json-assignment'
ModuleNotFoundError: No module named 'keystone-json-assignment'  Hi...: No module named 'keystone-json-assignment' How to remove the ModuleNotFoundError: No module named 'keystone-json-assignment' error? Thanks  
ModuleNotFoundError: No module named 'odoo9-addon-openeducat-assignment'
ModuleNotFoundError: No module named 'odoo9-addon-openeducat-assignment' ...: ModuleNotFoundError: No module named 'odoo9-addon-openeducat-assignment' How to remove the ModuleNotFoundError: No module named 'odoo9-addon-openeducat-assignment'
Assignment Operators in java 7
Assignment Operators in java 7 In this section you will learn about the Assignment Operators. This is one type of operators. Assignment Operators... to the variable by using any assignment operator. Your variables are placed
Simple Assignment Operator
Simple Assignment Operator       Assignment operator... lying to the left side of the assignment operator. But, If the value already
JAVA assignment
java assignment
ModuleNotFoundError: No module named 'django-validate-model-attribute-assignment'
-assignment'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'django-validate-model-attribute-assignment...-model-attribute-assignment' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-validate-model-attribute-assignment'
-assignment'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'django-validate-model-attribute-assignment...-model-attribute-assignment' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-validate-model-attribute-assignment'
-assignment'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'django-validate-model-attribute-assignment...-model-attribute-assignment' error? Thanks   Hi, In your python

Ads