Jump to content

Please Help

- - - - -

  • Please log in to reply
2 replies to this topic

#1
lina

lina

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
Hi all,
I need help to Write a Circle class that implements the Comparable interface. (We compare Circles by their radii.)
[B][COLOR="DarkGreen"]public class Circle

{

private Point center;

private int radius;

// Direct constructor

public Circle(Point p, int d)

{

center = p;

radius = d;

}

}[/COLOR][/B]

This one too please:
Lots of Java statements can cause exceptions to be thrown. For example, any method called on an object (e.g., bee.foo()) will throw a NullPointerException if bee is null. Rewrite the method below to catch the exception.

[COLOR="DarkGreen"][B]public void whatever()throws NullPointerException

{

bee.foo();

}[/B][/COLOR]

Edited by Roger, 09 December 2010 - 09:57 AM.
added code tags


#2
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts

public class Circle implements Comparable{

    private Point center;

    private int radius;


    public Circle(Point p, int d){

        center = p;

        radius = d;

    }


    int compareTo(Object o){


        if(!object.getClass().equals(getClass()))

            throw new ClassCastException("Argument is not a circle");


        Circle x=(Circle)o;


        if(x.radius==radius)

           return 0;


        if(x.radius<radius)

           return 1;


        return -1;


    }

}




The second one is really trivial. Don't they teach you anything in class?

public void whatever(){

    

    try{

        bee.foo();

    } catch(NullPointerException e){

        //handle exception here

    }


}


#3
lina

lina

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
thanks a lot




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users